/* OpenRouter's OAuth PKCE for third-party apps, as Pi's openrouter flow
* does it (pi-ai auth/oauth/openrouter.ts): the code comes back to a
* loopback address on a free port, and is exchanged for an ordinary,
* user-controlled API key, which is stored as a key. OpenRouter sends no
* state, so a random path is the redirect's secret. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "oauth.h"
#include "userdb.h"
#define OPENROUTER_SITE "https://openrouter.ai"
#define OPENROUTER_AUTHORIZE "/auth"
#define OPENROUTER_KEYS "/api/v1/auth/keys"
#define OPENROUTER_WAIT_SECONDS 300
static const char *openrouter_site(void) { return pic_oauth_seam("PIC_TEST_OPENROUTER_SITE", OPENROUTER_SITE); }
static void openrouter_free(char *s) {
if (s) {
pic_cred_wipe(s, strlen(s));
free(s);
}
}
/* The code in a pasted line: the whole redirect address, which must start
* with REDIRECT. OpenRouter sends no state, so the random path is what binds
* a code to this sign-in; a bare code (Pi takes one) would bind nothing. */
static char *openrouter_pasted(const char *pasted, const char *redirect, char *err, size_t errcap) {
size_t n = strlen(redirect);
char *code;
if (strncmp(pasted, redirect, n) != 0 || pasted[n] != '?') {
(void)snprintf(err, errcap, "paste the whole address the browser was sent to; it starts with %s", redirect);
return NULL;
}
if (!(code = pic_oauth_param(pasted + n + 1, "code"))) {
(void)snprintf(err, errcap, "the pasted address carries no code");
}
return code;
}
static int openrouter_login(const pic_oauth_io *io, const char **kind, char **secret, char *err, size_t errcap) {
char verifier[64], challenge[64], nonce[48], path[64], redirect[160], *url = NULL, *query = NULL, *pasted = NULL;
char *code = NULL, *body = NULL, *form = NULL, keys[512];
pic_oauth_callback cb;
int status = 0, rc = -1;
*secret = NULL;
*kind = "api_key";
cb.fd = -1;
if (pic_oauth_pkce(verifier, challenge) != 0 || pic_oauth_random(nonce, sizeof nonce, 24) != 0) {
(void)snprintf(err, errcap, "no entropy for the sign-in");
return -1;
}
(void)snprintf(path, sizeof path, "/oauth/callback/%s", nonce);
if (pic_oauth_listen(&cb, 0, path, NULL, err, errcap) != 0) {
return -1;
}
(void)snprintf(redirect, sizeof redirect, "http://127.0.0.1:%d%s", cb.port, path);
{
const char *pairs[] = {"callback_url", redirect, "code_challenge", challenge, "code_challenge_method", "S256", NULL};
char *q = pic_oauth_form(pairs);
size_t n = q ? strlen(openrouter_site()) + sizeof OPENROUTER_AUTHORIZE + strlen(q) + 2 : 0;
if (!q || !(url = malloc(n))) {
free(q);
(void)snprintf(err, errcap, "out of memory");
goto out;
}
(void)snprintf(url, n, "%s%s?%s", openrouter_site(), OPENROUTER_AUTHORIZE, q);
free(q);
}
pic_oauth_show_url(io, url,
"Sign in to OpenRouter in the browser. If the browser cannot reach this machine, paste the address"
" it ends on here and press Enter.");
if (pic_oauth_wait(&cb, io, OPENROUTER_WAIT_SECONDS, &query, &pasted, err, errcap) != 0) {
goto out;
}
pic_oauth_close(&cb);
code = pasted ? openrouter_pasted(pasted, redirect, err, errcap) : pic_oauth_param(query, "code");
if (!code || !*code) {
if (!pasted) {
(void)snprintf(err, errcap, "the redirect carried no code");
}
goto out;
}
pic_oauth_say(io, "Exchanging the code for an API key...");
{
sqlite3 *db = pic_userdb(err, errcap);
sqlite3_stmt *q = NULL;
if (db && sqlite3_prepare_v2(db, "SELECT json_object('code', ?1, 'code_verifier', ?2, 'code_challenge_method', 'S256')",
-1, &q, NULL) == SQLITE_OK) {
sqlite3_bind_text(q, 1, code, -1, SQLITE_STATIC);
sqlite3_bind_text(q, 2, verifier, -1, SQLITE_STATIC);
if (sqlite3_step(q) == SQLITE_ROW) {
form = strdup((const char *)sqlite3_column_text(q, 0));
}
}
sqlite3_finalize(q);
}
if (!form) {
(void)snprintf(err, errcap, "out of memory");
goto out;
}
(void)snprintf(keys, sizeof keys, "%s%s", openrouter_site(), OPENROUTER_KEYS);
if (pic_oauth_post(keys, form, 1, &status, &body, err, errcap) != 0) {
goto out;
}
if (status != 200) {
pic_oauth_error(status, body, err, errcap);
} else if (!(*secret = pic_oauth_json_text(body, "$.key")) || !**secret) {
openrouter_free(*secret);
*secret = NULL;
(void)snprintf(err, errcap, "OpenRouter's response carries no key");
} else {
rc = 0;
}
out:
pic_oauth_close(&cb);
pic_cred_wipe(verifier, sizeof verifier);
free(url);
openrouter_free(query);
openrouter_free(pasted);
openrouter_free(code);
openrouter_free(form);
openrouter_free(body);
return rc;
}
/* The key does not expire; a stored one is never handed back for refreshing. */
static int openrouter_refresh(const char *cred, char **out, char *err, size_t errcap) {
(void)cred;
*out = NULL;
(void)snprintf(err, errcap, "an OpenRouter key does not expire");
return -1;
}
const pic_oauth_flow pic_oauth_flow_openrouter = {"openrouter", "Sign in with OpenRouter", openrouter_login,
openrouter_refresh, NULL};