feat(auth-oauth2): custom parameters for authorization, token, and refresh requests (#629)

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Gregory SchierandClaude Opus 5 authored and GitHub committed 2026-09-13 09:15:25 -07:00
1 parent 7d0926c961
commit 7a6d275fdb
16 files changed
+771 -5

No files matched your search

@@ -1,6 +1,8 @@
import { createHash, randomBytes } from "node:crypto";
import type { Context } from "@yaakapp/api";
import { getRedirectUrlViaExternalBrowser } from "../callbackServer";
import type { CustomParams } from "../customParams";
import { applyQueryParams, NO_CUSTOM_PARAMS } from "../customParams";
import { fetchAccessToken } from "../fetchAccessToken";
import { getOrRefreshAccessToken } from "../getOrRefreshAccessToken";
import type { AccessToken, TokenStoreArgs } from "../store";
@@ -36,6 +38,7 @@ export async function getAuthorizationCode(
pkce,
tokenName,
externalBrowser,
customParams = NO_CUSTOM_PARAMS,
}: {
authorizationUrl: string;
accessTokenUrl: string;
@@ -52,6 +55,7 @@ export async function getAuthorizationCode(
} | null;
tokenName: "access_token" | "id_token";
externalBrowser?: ExternalBrowserOptions;
customParams?: CustomParams;
},
): Promise<AccessToken> {
const tokenArgs: TokenStoreArgs = {
@@ -68,6 +72,7 @@ export async function getAuthorizationCode(
clientSecret,
credentialsInBody,
tokenName,
custom: customParams.refresh,
});
if (token != null) {
return token;
@@ -92,6 +97,10 @@ export async function getAuthorizationCode(
authorizationUrl.searchParams.set("code_challenge_method", pkce.challengeMethod);
}
// Applied before redirect_uri, which belongs to the callback flow rather than
// to the user: overriding it would send the code somewhere nothing listens
applyQueryParams(authorizationUrl, customParams.authorizationQuery);
let code: string;
let actualRedirectUri: string | null = redirectUri;
@@ -130,6 +139,7 @@ export async function getAuthorizationCode(
...(pkce ? [{ name: "code_verifier", value: pkce.codeVerifier }] : []),
...(actualRedirectUri ? [{ name: "redirect_uri", value: actualRedirectUri }] : []),
],
custom: customParams.token,
});
return storeToken(ctx, tokenArgs, response, tokenName);
@@ -1,6 +1,8 @@
import { createPrivateKey, randomUUID } from "node:crypto";
import type { Context } from "@yaakapp/api";
import jwt, { type Algorithm } from "jsonwebtoken";
import type { CustomParams } from "../customParams";
import { NO_CUSTOM_PARAMS } from "../customParams";
import { fetchAccessToken } from "../fetchAccessToken";
import type { TokenStoreArgs } from "../store";
import { getToken, storeToken } from "../store";
@@ -109,6 +111,7 @@ export async function getClientCredentials(
clientAssertionSecretBase64,
clientCredentialsMethod,
clientAssertionAlgorithm,
customParams = NO_CUSTOM_PARAMS,
}: {
accessTokenUrl: string;
clientId: string;
@@ -120,6 +123,7 @@ export async function getClientCredentials(
clientAssertionSecretBase64: boolean;
clientCredentialsMethod: string;
clientAssertionAlgorithm: string;
customParams?: CustomParams;
},
) {
const tokenArgs: TokenStoreArgs = {
@@ -143,6 +147,7 @@ export async function getClientCredentials(
clientId,
scope,
params: [],
custom: customParams.token,
};
const fetchParams: Parameters<typeof fetchAccessToken>[1] =
@@ -1,5 +1,7 @@
import type { Context } from "@yaakapp/api";
import { getRedirectUrlViaExternalBrowser } from "../callbackServer";
import type { CustomParams } from "../customParams";
import { applyQueryParams, NO_CUSTOM_PARAMS } from "../customParams";
import type { AccessToken, AccessTokenRawResponse } from "../store";
import { getDataDirKey, getToken, storeToken } from "../store";
import { isTokenExpired } from "../util";
@@ -18,6 +20,7 @@ export async function getImplicit(
audience,
tokenName,
externalBrowser,
customParams = NO_CUSTOM_PARAMS,
}: {
authorizationUrl: string;
responseType: string;
@@ -28,6 +31,7 @@ export async function getImplicit(
audience: string | null;
tokenName: "access_token" | "id_token";
externalBrowser?: ExternalBrowserOptions;
customParams?: CustomParams;
},
): Promise<AccessToken> {
const tokenArgs = {
@@ -59,6 +63,10 @@ export async function getImplicit(
);
}
// Applied before redirect_uri, which belongs to the callback flow rather than
// to the user: overriding it would send the token somewhere nothing listens
applyQueryParams(authorizationUrl, customParams.authorizationQuery);
let newToken: AccessToken;
// Use external browser flow if enabled
@@ -1,4 +1,6 @@
import type { Context } from "@yaakapp/api";
import type { CustomParams } from "../customParams";
import { NO_CUSTOM_PARAMS } from "../customParams";
import { fetchAccessToken } from "../fetchAccessToken";
import { getOrRefreshAccessToken } from "../getOrRefreshAccessToken";
import type { AccessToken, TokenStoreArgs } from "../store";
@@ -16,6 +18,7 @@ export async function getPassword(
credentialsInBody,
audience,
scope,
customParams = NO_CUSTOM_PARAMS,
}: {
accessTokenUrl: string;
clientId: string;
@@ -25,6 +28,7 @@ export async function getPassword(
scope: string | null;
audience: string | null;
credentialsInBody: boolean;
customParams?: CustomParams;
},
): Promise<AccessToken> {
const tokenArgs: TokenStoreArgs = {
@@ -40,6 +44,7 @@ export async function getPassword(
clientId,
clientSecret,
credentialsInBody,
custom: customParams.refresh,
});
if (token != null) {
return token;
@@ -57,6 +62,7 @@ export async function getPassword(
{ name: "username", value: username },
{ name: "password", value: password },
],
custom: customParams.token,
});
return storeToken(ctx, tokenArgs, response);