Yet another srun login utility, but written in C and with ESP8266 / ESP32 support.
Users of Linux, macOS, and other Unix-like systems can build a standalone binary using CMake. The following dependencies are required:
- CMake (for building)
- OpenSSL (or Mbed TLS, see below)
- libcurl
- cJSON
- libbsd (for Linux; optional but strongly recommended, see below)
For macOS:
brew install cmake openssl cjsonFor Debian-based Linux distributions:
sudo apt install make cmake libssl-dev libcurl4-openssl-dev libcjson-dev libbsd-devBuild:
cmake -B cmake-build -DCMAKE_BUILD_TYPE=RelWithDebInfo # or Release, at your choice
cmake --build cmake-build --config RelWithDebInfoWarning
For Linux users: libbsd provides readpassphrase() used to read password from the terminal securely. If it is installed, it will be used automatically. If not, a less secure fallback implementation will be used.
macOS or BSD users can ignore this warning as readpassphrase() is provided by the system.
You can choose the crypto library to use by setting the SRUN_CRYPTO variable. Supported values are openssl, mbedtls, and self which uses a self-contained implementation of SHA-1 and HMAC-MD5 (see platform/md.c). The default is openssl. If neither OpenSSL nor Mbed TLS is found, self will be used automatically.
cmake -B cmake-build -DSRUN_CRYPTO=mbedtls # or openssl, self# login
./cmake-build/srun login -b https://auth.my.edu -a 12 -u HarumiEna -p mysupersecretpassword
# login, ask username and password interactively, guess ac_id automatically
./cmake-build/srun login -b https://auth.my.edu
# logout
./cmake-build/srun logout -b https://auth.my.edu
# help
./cmake-build/srun -hIntegrating srun-c into your own project is a bit more complicated. You need to drop a few files into your project.
- Copy
srun.c,srun.h, andplatform/compat.hto your project. - Copy one of
platform/libcurl.c,platform/esp8266_arduino_http.cpp, orplatform/espidf_http.cdepending on the HTTP library you have / want to use.- Use
libcurl.cfor Unix-like systems. - Use
espidf_http.cfor ESP32. - Use
esp8266_arduino_http.cppfor ESP8266 with Arduino framework.
- Use
- Copy one of
platform/openssl.c,platform/mbedtls.c, orplatform/md.cdepending on the crypto library you have / want to use.- For Unix-like systems, any is fine but you usually want
openssl.cormbedtls.cas they may utilize hardware acceleration. - For ESP32, use
mbedtls.cas ESP-IDF provides it. - For ESP8266 or projects with minimal dependencies, use
md.c.
- For Unix-like systems, any is fine but you usually want
- Copy one of
platform/cjson.corplatform/arduinojson.cppdepending on the JSON library you have / want to use.cjson.crequirescJSONlibrary (which ESP32 has), whilearduinojson.cppis for ArduinoJson library.
Your project structure should look like this (feel free to adjust paths or file names, or just use a flat structure):
├── include # make sure this is in your include path
│ ├── ArduinoJson.hpp
│ ├── compat.h
│ └── srun.h
└── src
├── arduinojson.cpp
├── esp8266_arduino_http.cpp
├── md.c
└── srun.c
If you wish to use other libraries, you will need to implement your own compatibility layer. See platform/compat.h and existing implementations under platform/ for a quick understanding of how to do this.
srun-c tries to follow an esp_http_client-style API. Below is a minimal example.
// login
srun_config config = {
.base_url = "https://auth.my.edu",
.username = "HarumiEna",
.password = "mysupersecretpassword",
.ac_id = 12,
};
srun_handle handle = srun_create(&config);
int ret = srun_login(handle);
if (ret != SRUN_OK) {
fprintf(stderr, "Login failed: %d\n", ret);
}
// logout
ret = srun_logout(handle);
if (ret != SRUN_OK) {
fprintf(stderr, "Logout failed: %d\n", ret);
}
srun_cleanup(handle);Login requires at least base_url, username and password. Logout requires at least base_url and username.
base_url should only contain the hostname and optionally the scheme and port, but not any path. It is required for login and logout operations.
The Srun portal requires ac_id, an integer that may vary by institution. You usually can find it in the URL of the login page. If it is not set, srun-c will try to guess it from the authentication page, but this is not guaranteed to work in all cases (also see below).
For detailed API usage, refer to the header file srun.h. For a more complete example, see main.c.
ESP8266 has very limited resources. HTTPS requires some amount of RAM, and if your project also uses much RAM, you may encounter crashes or instability.
Caution
If you encounter issues, consider using HTTP but be aware that HTTP is insecure. Srun portal uses a XXTEA-variant encryption for the login request, but it is virtually useless; if someone intercepts a full login request, they can decrypt the payload and get your credentials. This is left to you as an exercise.
Depending on your institution's setup, ac_id detection may require HTTPS, other than that it may work fine with HTTP.
- Test if
rad_user_dmis sufficient for logout (need testers!) - Support for
srun-cas a PlatformIO library - Support for statically-linked builds
This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the LICENSE file for more details.