| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- /* chat_login: native Cloudron OpenID login for the Chat client.
- *
- * The Cloudron Chat web login is a Vue SPA, which the browser cannot run, but
- * the API underneath is plain HTTP + JSON:
- *
- * 1. GET <server>/auth/login redirects to the Cloudron OIDC provider's
- * /openid/interaction/<uid> page (cookies on both hosts).
- * 2. That page embeds <script id="cloudron-config"> JSON with a relative
- * submitUrl.
- * 3. POST {"username","password","totpToken"} to submitUrl answers 401
- * {"message"}, 200 {"twoFactorRequired":true,...} or 200 {"redirectTo"}.
- * 4. Following redirectTo finishes the OIDC callback and sets the Chat
- * session cookie (connect.sid) on the Chat host.
- *
- * This module drives exactly those requests with a uhttp cookie jar; it
- * never renders the page. The password is only held by the caller and is
- * never written anywhere. The pure parsers (config/reply/config file) are
- * compiled on the host for chat_login_test.c with CHAT_LOGIN_PURE. */
- #ifndef CHAT_LOGIN_H
- #define CHAT_LOGIN_H
- #include "ujson.h"
- #ifndef CHAT_LOGIN_PURE
- #include "uhttp.h"
- #endif
- #define CHAT_SERVER_MAX 192
- #define CHAT_URL_MAX 1280
- #define CHAT_COOKIE_MAX 2048
- #define CHAT_PATH_MAX 128
- /* chat_login_* results */
- #define CHAT_LOGIN_OK 0 /* session cookie captured and validated */
- #define CHAT_LOGIN_FORM 1 /* start: username/password form found */
- #define CHAT_LOGIN_TOTP 2 /* submit: a TOTP code is needed */
- #define CHAT_LOGIN_DENIED 3 /* submit: wrong credentials (l->error) */
- #define CHAT_LOGIN_NOFORM 4 /* start: no Cloudron login API (SSO) */
- #define CHAT_LOGIN_PASSKEY 5 /* submit: account needs a passkey */
- #define CHAT_LOGIN_ERROR (-1) /* network/protocol error (l->error) */
- /* Submit reply classification (pure). */
- #define CHAT_REPLY_REDIRECT 1
- #define CHAT_REPLY_TOTP 2
- #define CHAT_REPLY_DENIED 3
- #define CHAT_REPLY_PASSKEY 4
- #define CHAT_REPLY_ERROR 5
- struct chat_login {
- char server[CHAT_SERVER_MAX]; /* https://chat.example.com */
- char submit_url[CHAT_URL_MAX]; /* absolute */
- char redirect[CHAT_URL_MAX];
- char app_name[64];
- char error[160];
- int totp_required;
- };
- /* Normalize a server entry: add https:// when no scheme is given and drop
- * trailing slashes. 0 ok, -1 invalid. */
- int chat_server_normalize(const char *in, char *out, unsigned int cap);
- /* Find the cloudron-config JSON in the login page and resolve its submitUrl
- * against base_url. Falls back to a plain "submitUrl":"..." scan. 0 when a
- * submit URL was found. */
- int chat_login_parse_page(const char *html, unsigned int len,
- const char *base_url, char *submit_url,
- unsigned int cap, char *app_name,
- unsigned int name_cap);
- /* Classify a POST reply. redirect receives redirectTo, msg the server's
- * message (both may be empty). */
- int chat_login_parse_reply(int status, const char *body, unsigned int len,
- char *redirect, unsigned int rcap, char *msg,
- unsigned int mcap);
- /* Config file: "server=...\ncookie=...\n". Unknown keys are ignored. */
- int chat_config_parse(const char *text, unsigned int len, char *server,
- unsigned int scap, char *cookie, unsigned int ccap);
- int chat_config_format(char *out, unsigned int cap, const char *server,
- const char *cookie);
- /* JSON body {"username":..,"password":..,"totpToken":..}; -1 if too long. */
- int chat_login_body(char *out, unsigned int cap, const char *user,
- const char *pass, const char *totp);
- #ifndef CHAT_LOGIN_PURE
- /* Read the whole response body of an opened request into buf (NUL
- * terminated). Returns the length, or a UHTTP_ERR_* (<0); a body larger
- * than cap-1 is UHTTP_ERR_TOOLONG. */
- int chat_http_body(struct uhttp *h, char *buf, unsigned int cap);
- /* One request: method/url/JSON body, response body into buf. Returns the
- * HTTP status (>0) or a UHTTP_ERR_*. *blen gets the body length. */
- int chat_http(struct uhttp *h, const char *method, const char *url,
- const char *json, char *buf, unsigned int cap,
- unsigned int *blen);
- /* Step 1: fetch the login page. CHAT_LOGIN_FORM, CHAT_LOGIN_OK (the
- * provider already had a session and sent us straight back),
- * CHAT_LOGIN_NOFORM or CHAT_LOGIN_ERROR. */
- int chat_login_start(struct chat_login *l, struct uhttp *h,
- struct uhttp_jar *jar, const char *server, char *buf,
- unsigned int cap);
- /* Step 2/3: submit the credentials (totp may be ""). */
- int chat_login_submit(struct chat_login *l, struct uhttp *h,
- struct uhttp_jar *jar, const char *user,
- const char *pass, const char *totp, char *buf,
- unsigned int cap);
- /* GET /api/me with the jar: 200 -> 0, 401/403 -> 1, else <0 (network). */
- int chat_session_check(struct uhttp *h, struct uhttp_jar *jar,
- const char *server, char *buf, unsigned int cap,
- unsigned int *blen);
- /* Host of a server URL ("chat.example.com"), for jar export/import. */
- int chat_server_host(const char *server, char *host, unsigned int cap,
- int *secure);
- #endif
- #endif
|