chat_login.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* chat_login: native Cloudron OpenID login for the Chat client.
  2. *
  3. * The Cloudron Chat web login is a Vue SPA, which the browser cannot run, but
  4. * the API underneath is plain HTTP + JSON:
  5. *
  6. * 1. GET <server>/auth/login redirects to the Cloudron OIDC provider's
  7. * /openid/interaction/<uid> page (cookies on both hosts).
  8. * 2. That page embeds <script id="cloudron-config"> JSON with a relative
  9. * submitUrl.
  10. * 3. POST {"username","password","totpToken"} to submitUrl answers 401
  11. * {"message"}, 200 {"twoFactorRequired":true,...} or 200 {"redirectTo"}.
  12. * 4. Following redirectTo finishes the OIDC callback and sets the Chat
  13. * session cookie (connect.sid) on the Chat host.
  14. *
  15. * This module drives exactly those requests with a uhttp cookie jar; it
  16. * never renders the page. The password is only held by the caller and is
  17. * never written anywhere. The pure parsers (config/reply/config file) are
  18. * compiled on the host for chat_login_test.c with CHAT_LOGIN_PURE. */
  19. #ifndef CHAT_LOGIN_H
  20. #define CHAT_LOGIN_H
  21. #include "ujson.h"
  22. #ifndef CHAT_LOGIN_PURE
  23. #include "uhttp.h"
  24. #endif
  25. #define CHAT_SERVER_MAX 192
  26. #define CHAT_URL_MAX 1280
  27. #define CHAT_COOKIE_MAX 2048
  28. #define CHAT_PATH_MAX 128
  29. /* chat_login_* results */
  30. #define CHAT_LOGIN_OK 0 /* session cookie captured and validated */
  31. #define CHAT_LOGIN_FORM 1 /* start: username/password form found */
  32. #define CHAT_LOGIN_TOTP 2 /* submit: a TOTP code is needed */
  33. #define CHAT_LOGIN_DENIED 3 /* submit: wrong credentials (l->error) */
  34. #define CHAT_LOGIN_NOFORM 4 /* start: no Cloudron login API (SSO) */
  35. #define CHAT_LOGIN_PASSKEY 5 /* submit: account needs a passkey */
  36. #define CHAT_LOGIN_ERROR (-1) /* network/protocol error (l->error) */
  37. /* Submit reply classification (pure). */
  38. #define CHAT_REPLY_REDIRECT 1
  39. #define CHAT_REPLY_TOTP 2
  40. #define CHAT_REPLY_DENIED 3
  41. #define CHAT_REPLY_PASSKEY 4
  42. #define CHAT_REPLY_ERROR 5
  43. struct chat_login {
  44. char server[CHAT_SERVER_MAX]; /* https://chat.example.com */
  45. char submit_url[CHAT_URL_MAX]; /* absolute */
  46. char redirect[CHAT_URL_MAX];
  47. char app_name[64];
  48. char error[160];
  49. int totp_required;
  50. };
  51. /* Normalize a server entry: add https:// when no scheme is given and drop
  52. * trailing slashes. 0 ok, -1 invalid. */
  53. int chat_server_normalize(const char *in, char *out, unsigned int cap);
  54. /* Find the cloudron-config JSON in the login page and resolve its submitUrl
  55. * against base_url. Falls back to a plain "submitUrl":"..." scan. 0 when a
  56. * submit URL was found. */
  57. int chat_login_parse_page(const char *html, unsigned int len,
  58. const char *base_url, char *submit_url,
  59. unsigned int cap, char *app_name,
  60. unsigned int name_cap);
  61. /* Classify a POST reply. redirect receives redirectTo, msg the server's
  62. * message (both may be empty). */
  63. int chat_login_parse_reply(int status, const char *body, unsigned int len,
  64. char *redirect, unsigned int rcap, char *msg,
  65. unsigned int mcap);
  66. /* Config file: "server=...\ncookie=...\n". Unknown keys are ignored. */
  67. int chat_config_parse(const char *text, unsigned int len, char *server,
  68. unsigned int scap, char *cookie, unsigned int ccap);
  69. int chat_config_format(char *out, unsigned int cap, const char *server,
  70. const char *cookie);
  71. /* JSON body {"username":..,"password":..,"totpToken":..}; -1 if too long. */
  72. int chat_login_body(char *out, unsigned int cap, const char *user,
  73. const char *pass, const char *totp);
  74. #ifndef CHAT_LOGIN_PURE
  75. /* Read the whole response body of an opened request into buf (NUL
  76. * terminated). Returns the length, or a UHTTP_ERR_* (<0); a body larger
  77. * than cap-1 is UHTTP_ERR_TOOLONG. */
  78. int chat_http_body(struct uhttp *h, char *buf, unsigned int cap);
  79. /* One request: method/url/JSON body, response body into buf. Returns the
  80. * HTTP status (>0) or a UHTTP_ERR_*. *blen gets the body length. */
  81. int chat_http(struct uhttp *h, const char *method, const char *url,
  82. const char *json, char *buf, unsigned int cap,
  83. unsigned int *blen);
  84. /* Step 1: fetch the login page. CHAT_LOGIN_FORM, CHAT_LOGIN_OK (the
  85. * provider already had a session and sent us straight back),
  86. * CHAT_LOGIN_NOFORM or CHAT_LOGIN_ERROR. */
  87. int chat_login_start(struct chat_login *l, struct uhttp *h,
  88. struct uhttp_jar *jar, const char *server, char *buf,
  89. unsigned int cap);
  90. /* Step 2/3: submit the credentials (totp may be ""). */
  91. int chat_login_submit(struct chat_login *l, struct uhttp *h,
  92. struct uhttp_jar *jar, const char *user,
  93. const char *pass, const char *totp, char *buf,
  94. unsigned int cap);
  95. /* GET /api/me with the jar: 200 -> 0, 401/403 -> 1, else <0 (network). */
  96. int chat_session_check(struct uhttp *h, struct uhttp_jar *jar,
  97. const char *server, char *buf, unsigned int cap,
  98. unsigned int *blen);
  99. /* Host of a server URL ("chat.example.com"), for jar export/import. */
  100. int chat_server_host(const char *server, char *host, unsigned int cap,
  101. int *secure);
  102. #endif
  103. #endif