/* Subscription sign-in: the OAuth machinery the provider flows share * (PKCE, the loopback callback, the device-code poll, token requests, the * browser) and the table of flows the composition root registers with the * credential store (cred.h). A flow lives in its own file (oauth_*.c) and * names its provider's endpoints; this file names none. No token is ever * printed, logged or put in an error. */ #ifndef PIC_OAUTH_H #define PIC_OAUTH_H #include #include #include "cred.h" /* The terminal a sign-in talks to: messages to out, a pasted line from the * descriptor in. */ typedef struct { FILE *out; int in; int flags; /* PIC_CRED_NO_BROWSER */ } pic_oauth_io; typedef struct { const char *provider; const char *label; /* "Sign in with ..." */ /* As pic_cred_oauth_ops.login, on IO. */ int (*login)(const pic_oauth_io *io, const char **kind, char **secret, char *err, size_t errcap); /* As pic_cred_oauth_ops.refresh. */ int (*refresh)(const char *cred, char **out, char *err, size_t errcap); } pic_oauth_flow; /* The ops to register with pic_cred_oauth_register. */ const pic_cred_oauth_ops *pic_oauth_ops(void); /* ---- what the flows share ------------------------------------------------- */ /* BYTES random bytes as base64url without padding into out (cap at least * 4 * BYTES / 3 + 2). 0, or -1 without entropy. */ int pic_oauth_random(char *out, size_t cap, size_t bytes); /* An RFC 7636 S256 pair: a 43-character verifier and its challenge. 0 or -1. */ int pic_oauth_pkce(char verifier[64], char challenge[64]); /* 1 when A and B are equal, in time that depends only on their lengths. */ int pic_oauth_same(const char *a, const char *b); /* NAME's value in an application/x-www-form-urlencoded string, decoded * (malloc'd), or NULL when it is absent. */ char *pic_oauth_param(const char *query, const char *name); /* A form body or query from NULL-terminated name, value pairs (malloc'd): * each value percent-encoded; a NULL value leaves its pair out. */ char *pic_oauth_form(const char *const *pairs); /* POSTs FORM (urlencoded) to URL: 0 with the status and the body (malloc'd, * at most 1 MB) whatever the status; -1 with err on a transport failure. The * caller wipes the form, which holds codes and tokens. */ int pic_oauth_post(const char *url, const char *form, int json, int *status, char **body, char *err, size_t errcap); /* A token endpoint's error, as "error: description" from its JSON body * (never the body itself, which could echo what was sent), into err. */ void pic_oauth_error(int status, const char *body, char *err, size_t errcap); /* PATH's string value in the JSON object JSON (malloc'd), or NULL. */ char *pic_oauth_json_text(const char *json, const char *path); /* PATH's number in JSON, or -1 when it is not a positive number. */ double pic_oauth_json_number(const char *json, const char *path); /* The loopback redirect: one listening socket on 127.0.0.1 only. */ typedef struct { int fd, port; char path[64]; const char *state; } pic_oauth_callback; /* Listens on 127.0.0.1:PORT (0: any free port) for GET PATH. 0, or -1 with * err (the port is taken, say). */ int pic_oauth_listen(pic_oauth_callback *c, int port, const char *path, const char *state, char *err, size_t errcap); /* Waits up to TIMEOUT seconds for the browser's redirect to C, or for a * pasted line on IO's terminal, whichever comes first. A redirect is taken * once: to PATH, with C's state (compared in constant time), carrying code * or error; anything else is answered and waited past. A pasted line is * returned as it is in *pasted, for the flow to read (a redirect URL or a * bare code). 0 with *query (the redirect's query string) or *pasted * (malloc'd); -1 with err. C may be NULL: the pasted line alone. */ int pic_oauth_wait(pic_oauth_callback *c, const pic_oauth_io *io, int timeout, char **query, char **pasted, char *err, size_t errcap); void pic_oauth_close(pic_oauth_callback *c); /* The query of a pasted redirect URL that starts with REDIRECT (scheme, * host, port and path), its state checked against STATE: 0 with *query * (malloc'd); -1 with err. */ int pic_oauth_pasted_query(const char *pasted, const char *redirect, const char *state, char **query, char *err, size_t errcap); /* Opens URL (http or https only) in the user's browser, unless IO says not * to; the URL is printed either way. */ void pic_oauth_show_url(const pic_oauth_io *io, const char *url, const char *instructions); /* RFC 8628's device authorization grant: asks DEVICE_URL for a code (FORM * is the request: client_id and scope), shows the user where to enter it, * and polls TOKEN_URL (with client_id) until the grant: 0 with the token * response's JSON (malloc'd); -1 with err when it is denied, expires or * fails. */ int pic_oauth_device(const pic_oauth_io *io, const char *device_url, const char *token_url, const char *form, const char *client_id, char **token, char *err, size_t errcap); /* Test seams (dev builds only): ENV's value, or FALLBACK. */ const char *pic_oauth_seam(const char *env, const char *fallback); extern const pic_oauth_flow pic_oauth_flow_openai; extern const pic_oauth_flow pic_oauth_flow_openrouter; #endif