@softure-ai/auth 0.2.5 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/README.md +113 -15
- package/dist/contract.d.ts +11 -1
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js.map +1 -1
- package/dist/index.d.ts +72 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -4
- package/dist/index.js.map +1 -1
- package/dist/messages/en.d.ts +10 -0
- package/dist/messages/en.d.ts.map +1 -1
- package/dist/messages/en.js +10 -0
- package/dist/messages/en.js.map +1 -1
- package/dist/messages/index.d.ts +28 -2
- package/dist/messages/index.d.ts.map +1 -1
- package/dist/messages/index.js +13 -3
- package/dist/messages/index.js.map +1 -1
- package/dist/next/actions.d.ts +5 -0
- package/dist/next/actions.d.ts.map +1 -1
- package/dist/next/actions.js +26 -15
- package/dist/next/actions.js.map +1 -1
- package/dist/next/index.d.ts +2 -1
- package/dist/next/index.d.ts.map +1 -1
- package/dist/next/index.js +2 -1
- package/dist/next/index.js.map +1 -1
- package/dist/next/oidc-cookie.d.ts +7 -0
- package/dist/next/oidc-cookie.d.ts.map +1 -0
- package/dist/next/oidc-cookie.js +25 -0
- package/dist/next/oidc-cookie.js.map +1 -0
- package/dist/next/oidc-login-url.d.ts +4 -0
- package/dist/next/oidc-login-url.d.ts.map +1 -0
- package/dist/next/oidc-login-url.js +13 -0
- package/dist/next/oidc-login-url.js.map +1 -0
- package/dist/next/oidc-route.d.ts +7 -0
- package/dist/next/oidc-route.d.ts.map +1 -0
- package/dist/next/oidc-route.js +48 -0
- package/dist/next/oidc-route.js.map +1 -0
- package/dist/next/pages.d.ts +5 -0
- package/dist/next/pages.d.ts.map +1 -1
- package/dist/next/pages.js +19 -4
- package/dist/next/pages.js.map +1 -1
- package/dist/next/params.d.ts +2 -0
- package/dist/next/params.d.ts.map +1 -1
- package/dist/next/params.js +2 -0
- package/dist/next/params.js.map +1 -1
- package/dist/next/previous-session.d.ts +6 -0
- package/dist/next/previous-session.d.ts.map +1 -0
- package/dist/next/previous-session.js +23 -0
- package/dist/next/previous-session.js.map +1 -0
- package/dist/options.d.ts +99 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +80 -0
- package/dist/options.js.map +1 -1
- package/dist/proxy/index.d.ts.map +1 -1
- package/dist/proxy/index.js +3 -1
- package/dist/proxy/index.js.map +1 -1
- package/dist/schema.d.ts +323 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +26 -3
- package/dist/schema.js.map +1 -1
- package/dist/server/change-password.d.ts +1 -1
- package/dist/server/change-password.d.ts.map +1 -1
- package/dist/server/change-password.js +3 -0
- package/dist/server/change-password.js.map +1 -1
- package/dist/server/health.d.ts.map +1 -1
- package/dist/server/health.js +2 -0
- package/dist/server/health.js.map +1 -1
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +1 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/login.d.ts +1 -1
- package/dist/server/login.d.ts.map +1 -1
- package/dist/server/login.js +4 -2
- package/dist/server/login.js.map +1 -1
- package/dist/server/oidc/accounts.d.ts +14 -0
- package/dist/server/oidc/accounts.d.ts.map +1 -0
- package/dist/server/oidc/accounts.js +79 -0
- package/dist/server/oidc/accounts.js.map +1 -0
- package/dist/server/oidc/context.d.ts +37 -0
- package/dist/server/oidc/context.d.ts.map +1 -0
- package/dist/server/oidc/context.js +34 -0
- package/dist/server/oidc/context.js.map +1 -0
- package/dist/server/oidc/discovery.d.ts +24 -0
- package/dist/server/oidc/discovery.d.ts.map +1 -0
- package/dist/server/oidc/discovery.js +109 -0
- package/dist/server/oidc/discovery.js.map +1 -0
- package/dist/server/oidc/flow.d.ts +49 -0
- package/dist/server/oidc/flow.d.ts.map +1 -0
- package/dist/server/oidc/flow.js +168 -0
- package/dist/server/oidc/flow.js.map +1 -0
- package/dist/server/oidc/id-token.d.ts +45 -0
- package/dist/server/oidc/id-token.d.ts.map +1 -0
- package/dist/server/oidc/id-token.js +122 -0
- package/dist/server/oidc/id-token.js.map +1 -0
- package/dist/server/oidc/index.d.ts +4 -0
- package/dist/server/oidc/index.d.ts.map +1 -0
- package/dist/server/oidc/index.js +4 -0
- package/dist/server/oidc/index.js.map +1 -0
- package/dist/server/oidc/token-request.d.ts +13 -0
- package/dist/server/oidc/token-request.d.ts.map +1 -0
- package/dist/server/oidc/token-request.js +49 -0
- package/dist/server/oidc/token-request.js.map +1 -0
- package/dist/server/password-reset.d.ts +1 -1
- package/dist/server/password-reset.d.ts.map +1 -1
- package/dist/server/password-reset.js +7 -7
- package/dist/server/password-reset.js.map +1 -1
- package/dist/server/privacy.d.ts +10 -0
- package/dist/server/privacy.d.ts.map +1 -1
- package/dist/server/privacy.js +18 -6
- package/dist/server/privacy.js.map +1 -1
- package/dist/server/rate-limits.d.ts +4 -1
- package/dist/server/rate-limits.d.ts.map +1 -1
- package/dist/server/rate-limits.js +10 -3
- package/dist/server/rate-limits.js.map +1 -1
- package/dist/server/register.js +2 -2
- package/dist/server/register.js.map +1 -1
- package/dist/server/sessions.js +2 -2
- package/dist/server/sessions.js.map +1 -1
- package/dist/server/switches.d.ts +1 -1
- package/dist/server/switches.d.ts.map +1 -1
- package/dist/server/switches.js +3 -1
- package/dist/server/switches.js.map +1 -1
- package/dist/testing/account.js +2 -2
- package/dist/testing/account.js.map +1 -1
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +3 -1
- package/dist/testing/index.js.map +1 -1
- package/dist/testing/oidc-provider.d.ts +68 -0
- package/dist/testing/oidc-provider.d.ts.map +1 -0
- package/dist/testing/oidc-provider.js +185 -0
- package/dist/testing/oidc-provider.js.map +1 -0
- package/dist/ui/index.d.ts +1 -0
- package/dist/ui/index.d.ts.map +1 -1
- package/dist/ui/index.js +1 -0
- package/dist/ui/index.js.map +1 -1
- package/dist/ui/oidc-sign-in.d.ts +25 -0
- package/dist/ui/oidc-sign-in.d.ts.map +1 -0
- package/dist/ui/oidc-sign-in.js +19 -0
- package/dist/ui/oidc-sign-in.js.map +1 -0
- package/migrations/0005_create_identities_and_oidc_states.sql +34 -0
- package/migrations/0006_add_users_display_name.sql +8 -0
- package/module.json +3 -2
- package/package.json +2 -2
- package/src/contract.ts +16 -1
- package/src/index.ts +20 -5
- package/src/messages/en.ts +10 -0
- package/src/messages/index.ts +29 -4
- package/src/next/actions.ts +23 -14
- package/src/next/index.ts +2 -0
- package/src/next/oidc-cookie.ts +31 -0
- package/src/next/oidc-login-url.ts +14 -0
- package/src/next/oidc-route.ts +48 -0
- package/src/next/pages.tsx +38 -11
- package/src/next/params.ts +3 -0
- package/src/next/previous-session.ts +23 -0
- package/src/options.ts +86 -1
- package/src/proxy/index.ts +3 -1
- package/src/schema.ts +33 -3
- package/src/server/change-password.ts +3 -0
- package/src/server/health.ts +2 -0
- package/src/server/index.ts +17 -0
- package/src/server/login.ts +3 -2
- package/src/server/oidc/accounts.ts +92 -0
- package/src/server/oidc/context.ts +56 -0
- package/src/server/oidc/discovery.ts +133 -0
- package/src/server/oidc/flow.ts +196 -0
- package/src/server/oidc/id-token.ts +143 -0
- package/src/server/oidc/index.ts +14 -0
- package/src/server/oidc/token-request.ts +59 -0
- package/src/server/password-reset.ts +7 -7
- package/src/server/privacy.ts +28 -6
- package/src/server/rate-limits.ts +11 -3
- package/src/server/register.ts +2 -2
- package/src/server/sessions.ts +2 -2
- package/src/server/switches.ts +2 -1
- package/src/testing/account.ts +2 -2
- package/src/testing/index.ts +10 -1
- package/src/testing/oidc-provider.ts +254 -0
- package/src/ui/index.ts +1 -0
- package/src/ui/oidc-sign-in.tsx +61 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
// An in-memory OpenID Connect provider for tests: discovery, JWKS, authorize (`signIn`) and the token endpoint, reached
|
|
2
|
+
// through a `fetch` the app's `auth({ oidc: { fetch } })` takes. It checks what a real provider checks (client
|
|
3
|
+
// credentials, redirect URI, PKCE verifier, one-time codes) and signs real ID tokens (RS256 or ES256), so the module's
|
|
4
|
+
// verification runs for real. A test breaks it on purpose: other claims, another key, `alg: none`, key rotation, an
|
|
5
|
+
// outage, an endpoint on plain http.
|
|
6
|
+
import { createHash, generateKeyPairSync, randomBytes, sign, type KeyObject } from "node:crypto";
|
|
7
|
+
import { systemClock, type Clock, type FetchFunction } from "@softure-ai/core";
|
|
8
|
+
import type { OidcProviderInput } from "../options.js";
|
|
9
|
+
|
|
10
|
+
export interface FakeOidcProviderOptions {
|
|
11
|
+
/** The provider id. Default `fake`. */
|
|
12
|
+
readonly id?: string;
|
|
13
|
+
/** Default `https://fake-oidc.test`. */
|
|
14
|
+
readonly issuer?: string;
|
|
15
|
+
readonly clientId?: string;
|
|
16
|
+
readonly clientSecret?: string;
|
|
17
|
+
readonly clientAuth?: "body" | "basic";
|
|
18
|
+
/** Default RS256. */
|
|
19
|
+
readonly algorithm?: "RS256" | "ES256";
|
|
20
|
+
/** The clock the tokens' `iat` and `exp` come from; pass the module context's test clock. */
|
|
21
|
+
readonly clock?: Clock;
|
|
22
|
+
/** The allowlist of the definition. Default `allowedDomains: ["example.com"]`. */
|
|
23
|
+
readonly allowedDomains?: readonly string[];
|
|
24
|
+
readonly allowedEmails?: readonly string[];
|
|
25
|
+
readonly requireHostedDomain?: boolean;
|
|
26
|
+
readonly createAccounts?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The person at the provider's sign-in screen. */
|
|
30
|
+
export interface FakeOidcUser {
|
|
31
|
+
readonly subject: string;
|
|
32
|
+
readonly email: string;
|
|
33
|
+
/** Default true. */
|
|
34
|
+
readonly emailVerified?: boolean;
|
|
35
|
+
/** Google's Workspace domain claim. */
|
|
36
|
+
readonly hostedDomain?: string;
|
|
37
|
+
/** The `name` claim (sent with the `profile` scope). */
|
|
38
|
+
readonly name?: string;
|
|
39
|
+
/** The `given_name` claim. */
|
|
40
|
+
readonly givenName?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface FakeSignInOptions {
|
|
44
|
+
/** The person refuses consent: the callback carries `error=access_denied`. */
|
|
45
|
+
readonly deny?: boolean;
|
|
46
|
+
/** Claims that replace or add to the ID token's own (`aud`, `nonce`, `exp`, …); `undefined` removes one. */
|
|
47
|
+
readonly claims?: Readonly<Record<string, unknown>>;
|
|
48
|
+
/** Signs with a key outside the JWKS, or sends an unsigned token (`alg: none`). */
|
|
49
|
+
readonly signWith?: "unknown-key" | "none";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A failure the next calls to the provider answer with, in order. */
|
|
53
|
+
export type FakeOidcFailure = "unavailable" | "invalid_grant" | "no_id_token";
|
|
54
|
+
|
|
55
|
+
export interface FakeOidcProvider {
|
|
56
|
+
/** The provider entry for `auth({ oidc: { providers: [fake.definition] } })`. */
|
|
57
|
+
readonly definition: OidcProviderInput;
|
|
58
|
+
/** The environment the definition reads its client id and secret from. */
|
|
59
|
+
readonly env: Readonly<Record<string, string>>;
|
|
60
|
+
/** The `fetch` for `auth({ oidc: { fetch } })`. */
|
|
61
|
+
readonly fetch: FetchFunction;
|
|
62
|
+
/** Checks the authorize URL as a provider does and answers the callback URL it redirects the browser to. */
|
|
63
|
+
signIn(authorizeUrl: string, user: FakeOidcUser, options?: FakeSignInOptions): URL;
|
|
64
|
+
/** A new signing key replaces the current one in the JWKS (the provider rotated its keys). */
|
|
65
|
+
rotateKeys(): void;
|
|
66
|
+
/** The next calls to the token endpoint fail like this, one per entry. */
|
|
67
|
+
failNext(...failures: FakeOidcFailure[]): void;
|
|
68
|
+
/** Overrides fields of the discovery document (`{ token_endpoint: "http://…" }`). */
|
|
69
|
+
setDiscovery(overrides: Readonly<Record<string, unknown>>): void;
|
|
70
|
+
/** What the provider saw. */
|
|
71
|
+
readonly calls: { readonly discovery: number; readonly jwks: number; readonly token: number; readonly lastForm: URLSearchParams | null };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface PendingCode {
|
|
75
|
+
readonly redirectUri: string;
|
|
76
|
+
readonly challenge: string;
|
|
77
|
+
readonly nonce: string | null;
|
|
78
|
+
readonly user: FakeOidcUser;
|
|
79
|
+
readonly options: FakeSignInOptions;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
interface SigningKeyPair {
|
|
83
|
+
readonly kid: string;
|
|
84
|
+
readonly privateKey: KeyObject;
|
|
85
|
+
readonly publicKey: KeyObject;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const random = () => randomBytes(24).toString("base64url");
|
|
89
|
+
const encode = (value: unknown) => Buffer.from(JSON.stringify(value), "utf8").toString("base64url");
|
|
90
|
+
|
|
91
|
+
export function createFakeOidcProvider(options: FakeOidcProviderOptions = {}): FakeOidcProvider {
|
|
92
|
+
const id = options.id ?? "fake";
|
|
93
|
+
const issuer = options.issuer ?? "https://fake-oidc.test";
|
|
94
|
+
const clientId = options.clientId ?? "fake-client";
|
|
95
|
+
const clientSecret = options.clientSecret ?? "fake-client-secret";
|
|
96
|
+
const algorithm = options.algorithm ?? "RS256";
|
|
97
|
+
const clock = options.clock ?? systemClock;
|
|
98
|
+
const envPrefix = `${id.toUpperCase().replaceAll("-", "_")}_OIDC`;
|
|
99
|
+
const urls = { discovery: `${issuer}/.well-known/openid-configuration`, jwks: `${issuer}/jwks`, token: `${issuer}/token`, authorize: `${issuer}/authorize` };
|
|
100
|
+
|
|
101
|
+
const createKey = (): SigningKeyPair => {
|
|
102
|
+
const pair = algorithm === "RS256" ? generateKeyPairSync("rsa", { modulusLength: 2048 }) : generateKeyPairSync("ec", { namedCurve: "P-256" });
|
|
103
|
+
return { kid: random(), ...pair };
|
|
104
|
+
};
|
|
105
|
+
let key = createKey();
|
|
106
|
+
const strangerKey = createKey();
|
|
107
|
+
const codes = new Map<string, PendingCode>();
|
|
108
|
+
const failures: FakeOidcFailure[] = [];
|
|
109
|
+
let discoveryOverrides: Readonly<Record<string, unknown>> = {};
|
|
110
|
+
const calls = { discovery: 0, jwks: 0, token: 0, lastForm: null as URLSearchParams | null };
|
|
111
|
+
|
|
112
|
+
const json = (status: number, body: unknown) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
|
|
113
|
+
|
|
114
|
+
function signToken(claims: Record<string, unknown>, signWith: FakeSignInOptions["signWith"]): string {
|
|
115
|
+
if (signWith === "none") return `${encode({ alg: "none", typ: "JWT" })}.${encode(claims)}.`;
|
|
116
|
+
const signer = signWith === "unknown-key" ? strangerKey : key;
|
|
117
|
+
const input = `${encode({ alg: algorithm, typ: "JWT", kid: signer.kid })}.${encode(claims)}`;
|
|
118
|
+
const signature =
|
|
119
|
+
algorithm === "RS256"
|
|
120
|
+
? sign("sha256", Buffer.from(input), signer.privateKey)
|
|
121
|
+
: sign("sha256", Buffer.from(input), { key: signer.privateKey, dsaEncoding: "ieee-p1363" });
|
|
122
|
+
return `${input}.${signature.toString("base64url")}`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function buildClaims(pending: PendingCode): Record<string, unknown> {
|
|
126
|
+
const now = Math.floor(clock.now().getTime() / 1000);
|
|
127
|
+
const claims: Record<string, unknown> = {
|
|
128
|
+
iss: issuer,
|
|
129
|
+
sub: pending.user.subject,
|
|
130
|
+
aud: clientId,
|
|
131
|
+
iat: now,
|
|
132
|
+
exp: now + 3600,
|
|
133
|
+
email: pending.user.email,
|
|
134
|
+
email_verified: pending.user.emailVerified ?? true,
|
|
135
|
+
};
|
|
136
|
+
if (pending.nonce !== null) claims.nonce = pending.nonce;
|
|
137
|
+
if (pending.user.hostedDomain !== undefined) claims.hd = pending.user.hostedDomain;
|
|
138
|
+
if (pending.user.name !== undefined) claims.name = pending.user.name;
|
|
139
|
+
if (pending.user.givenName !== undefined) claims.given_name = pending.user.givenName;
|
|
140
|
+
for (const [name, value] of Object.entries(pending.options.claims ?? {})) {
|
|
141
|
+
if (value === undefined) delete claims[name];
|
|
142
|
+
else claims[name] = value;
|
|
143
|
+
}
|
|
144
|
+
return claims;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function readClient(form: URLSearchParams, authorization: string | null): { id: string | null; secret: string | null } {
|
|
148
|
+
if (authorization?.startsWith("Basic ") === true) {
|
|
149
|
+
const [user, password] = Buffer.from(authorization.slice(6), "base64").toString("utf8").split(":");
|
|
150
|
+
return { id: user === undefined ? null : decodeURIComponent(user), secret: password === undefined ? null : decodeURIComponent(password) };
|
|
151
|
+
}
|
|
152
|
+
return { id: form.get("client_id"), secret: form.get("client_secret") };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function answerToken(init: RequestInit): Response {
|
|
156
|
+
calls.token += 1;
|
|
157
|
+
const form = new URLSearchParams(init.body instanceof URLSearchParams ? init.body : typeof init.body === "string" ? init.body : "");
|
|
158
|
+
calls.lastForm = form;
|
|
159
|
+
const failure = failures.shift();
|
|
160
|
+
if (failure === "unavailable") return json(503, { error: "temporarily_unavailable" });
|
|
161
|
+
if (failure === "invalid_grant") return json(400, { error: "invalid_grant" });
|
|
162
|
+
const client = readClient(form, new Headers(init.headers).get("authorization"));
|
|
163
|
+
if (client.id !== clientId || client.secret !== clientSecret) return json(401, { error: "invalid_client" });
|
|
164
|
+
if (form.get("grant_type") !== "authorization_code") return json(400, { error: "unsupported_grant_type" });
|
|
165
|
+
const code = form.get("code") ?? "";
|
|
166
|
+
const pending = codes.get(code);
|
|
167
|
+
codes.delete(code);
|
|
168
|
+
if (pending === undefined || form.get("redirect_uri") !== pending.redirectUri) return json(400, { error: "invalid_grant" });
|
|
169
|
+
const verifier = form.get("code_verifier") ?? "";
|
|
170
|
+
if (createHash("sha256").update(verifier, "ascii").digest("base64url") !== pending.challenge) return json(400, { error: "invalid_grant" });
|
|
171
|
+
if (failure === "no_id_token") return json(200, { access_token: random(), token_type: "Bearer" });
|
|
172
|
+
return json(200, {
|
|
173
|
+
access_token: random(),
|
|
174
|
+
token_type: "Bearer",
|
|
175
|
+
expires_in: 3600,
|
|
176
|
+
id_token: signToken(buildClaims(pending), pending.options.signWith),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const fetchFake: FetchFunction = (input, init) => Promise.resolve(route(input, init));
|
|
181
|
+
|
|
182
|
+
function route(input: string | URL, init: RequestInit): Response {
|
|
183
|
+
const url = new URL(typeof input === "string" ? input : input.toString());
|
|
184
|
+
const target = `${url.origin}${url.pathname}`;
|
|
185
|
+
if (target === urls.discovery) {
|
|
186
|
+
calls.discovery += 1;
|
|
187
|
+
return json(200, {
|
|
188
|
+
issuer,
|
|
189
|
+
authorization_endpoint: urls.authorize,
|
|
190
|
+
token_endpoint: urls.token,
|
|
191
|
+
jwks_uri: urls.jwks,
|
|
192
|
+
response_types_supported: ["code"],
|
|
193
|
+
id_token_signing_alg_values_supported: [algorithm],
|
|
194
|
+
...discoveryOverrides,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
if (target === urls.jwks) {
|
|
198
|
+
calls.jwks += 1;
|
|
199
|
+
return json(200, { keys: [{ ...key.publicKey.export({ format: "jwk" }), kid: key.kid, use: "sig", alg: algorithm }] });
|
|
200
|
+
}
|
|
201
|
+
if (target === urls.token && init.method === "POST") return answerToken(init);
|
|
202
|
+
return json(404, { error: "not_found" });
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return {
|
|
206
|
+
definition: {
|
|
207
|
+
id,
|
|
208
|
+
name: "Fake",
|
|
209
|
+
issuer,
|
|
210
|
+
clientIdEnv: `${envPrefix}_CLIENT_ID`,
|
|
211
|
+
clientSecretEnv: `${envPrefix}_CLIENT_SECRET`,
|
|
212
|
+
clientAuth: options.clientAuth ?? "body",
|
|
213
|
+
allowedDomains: [...(options.allowedDomains ?? (options.allowedEmails === undefined ? ["example.com"] : []))],
|
|
214
|
+
allowedEmails: [...(options.allowedEmails ?? [])],
|
|
215
|
+
requireHostedDomain: options.requireHostedDomain,
|
|
216
|
+
createAccounts: options.createAccounts,
|
|
217
|
+
},
|
|
218
|
+
env: { [`${envPrefix}_CLIENT_ID`]: clientId, [`${envPrefix}_CLIENT_SECRET`]: clientSecret },
|
|
219
|
+
fetch: fetchFake,
|
|
220
|
+
signIn(authorizeUrl, user, signInOptions = {}) {
|
|
221
|
+
const url = new URL(authorizeUrl);
|
|
222
|
+
const query = url.searchParams;
|
|
223
|
+
if (`${url.origin}${url.pathname}` !== urls.authorize) throw new Error(`fake OIDC provider: authorize URL ${url.origin}${url.pathname} is not ${urls.authorize}`);
|
|
224
|
+
if (query.get("client_id") !== clientId) throw new Error("fake OIDC provider: unknown client_id");
|
|
225
|
+
if (query.get("response_type") !== "code") throw new Error("fake OIDC provider: response_type must be code");
|
|
226
|
+
if (!(query.get("scope") ?? "").split(" ").includes("openid")) throw new Error("fake OIDC provider: scope lacks openid");
|
|
227
|
+
if (query.get("code_challenge_method") !== "S256") throw new Error("fake OIDC provider: PKCE S256 is required");
|
|
228
|
+
const redirectUri = query.get("redirect_uri");
|
|
229
|
+
const state = query.get("state");
|
|
230
|
+
const challenge = query.get("code_challenge");
|
|
231
|
+
if (redirectUri === null || state === null || challenge === null) throw new Error("fake OIDC provider: redirect_uri, state and code_challenge are required");
|
|
232
|
+
const callback = new URL(redirectUri);
|
|
233
|
+
callback.searchParams.set("state", state);
|
|
234
|
+
if (signInOptions.deny === true) {
|
|
235
|
+
callback.searchParams.set("error", "access_denied");
|
|
236
|
+
return callback;
|
|
237
|
+
}
|
|
238
|
+
const code = random();
|
|
239
|
+
codes.set(code, { redirectUri, challenge, nonce: query.get("nonce"), user, options: signInOptions });
|
|
240
|
+
callback.searchParams.set("code", code);
|
|
241
|
+
return callback;
|
|
242
|
+
},
|
|
243
|
+
rotateKeys() {
|
|
244
|
+
key = createKey();
|
|
245
|
+
},
|
|
246
|
+
failNext(...next) {
|
|
247
|
+
failures.push(...next);
|
|
248
|
+
},
|
|
249
|
+
setDiscovery(overrides) {
|
|
250
|
+
discoveryOverrides = overrides;
|
|
251
|
+
},
|
|
252
|
+
calls,
|
|
253
|
+
};
|
|
254
|
+
}
|
package/src/ui/index.ts
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { formatMessage } from "@softure-ai/core";
|
|
4
|
+
import { Button, type ButtonVariant, type ClassNames, createSlotClassGetter, FormError } from "@softure-ai/ui";
|
|
5
|
+
import { type AuthMessages, getOidcMessages } from "../messages/index.js";
|
|
6
|
+
|
|
7
|
+
// The OpenID Connect buttons: one small form per provider, posting `provider` and `next` to the start action
|
|
8
|
+
// (`oidcSignInAction` of `@softure-ai/auth/next`), so it works without JavaScript. Copy comes from messages.
|
|
9
|
+
|
|
10
|
+
export type OidcSignInSlot = "root" | "form" | "submit" | "divider";
|
|
11
|
+
|
|
12
|
+
export interface OidcSignInProvider {
|
|
13
|
+
readonly id: string;
|
|
14
|
+
/** Shown in `messages.oidc.signInWith`, e.g. Google. */
|
|
15
|
+
readonly name: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface OidcSignInProps {
|
|
19
|
+
readonly action: (formData: FormData) => void | Promise<void>;
|
|
20
|
+
readonly providers: readonly OidcSignInProvider[];
|
|
21
|
+
readonly messages: AuthMessages;
|
|
22
|
+
/** Where to go after the sign-in; checked again on the server. */
|
|
23
|
+
readonly next?: string;
|
|
24
|
+
/** A failed sign-in's message, e.g. from the login page's `?oidc_error=`. */
|
|
25
|
+
readonly error?: string;
|
|
26
|
+
/** Shows `messages.oidc.divider` under the buttons, before a password form. */
|
|
27
|
+
readonly hasDivider?: boolean;
|
|
28
|
+
readonly classNames?: ClassNames<OidcSignInSlot>;
|
|
29
|
+
readonly unstyled?: boolean;
|
|
30
|
+
/** The buttons' look; `secondary` by default. */
|
|
31
|
+
readonly submitVariant?: ButtonVariant;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const DEFAULT_CLASSES: Readonly<Record<OidcSignInSlot, string>> = {
|
|
35
|
+
root: "sft:flex sft:flex-col sft:gap-3 sft:font-sans",
|
|
36
|
+
form: "sft:flex sft:flex-col",
|
|
37
|
+
// The button styles itself; the slot only carries the app's class.
|
|
38
|
+
submit: "",
|
|
39
|
+
divider: "sft:m-0 sft:text-center sft:text-sm sft:text-muted",
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export function OidcSignIn(props: OidcSignInProps) {
|
|
43
|
+
const { action, providers, messages, next, error, hasDivider = false, classNames, unstyled, submitVariant = "secondary" } = props;
|
|
44
|
+
const slot = createSlotClassGetter({ defaults: DEFAULT_CLASSES, classNames, unstyled });
|
|
45
|
+
const copy = getOidcMessages(messages);
|
|
46
|
+
return (
|
|
47
|
+
<div className={slot("root")}>
|
|
48
|
+
<FormError message={error} unstyled={unstyled} />
|
|
49
|
+
{providers.map((provider) => (
|
|
50
|
+
<form key={provider.id} action={action} className={slot("form")}>
|
|
51
|
+
<input type="hidden" name="provider" value={provider.id} />
|
|
52
|
+
{next === undefined ? null : <input type="hidden" name="next" value={next} />}
|
|
53
|
+
<Button type="submit" variant={submitVariant} fullWidth classNames={{ root: slot("submit") }} unstyled={unstyled}>
|
|
54
|
+
{formatMessage(copy.signInWith, { provider: provider.name })}
|
|
55
|
+
</Button>
|
|
56
|
+
</form>
|
|
57
|
+
))}
|
|
58
|
+
{hasDivider ? <p className={slot("divider")}>{copy.divider}</p> : null}
|
|
59
|
+
</div>
|
|
60
|
+
);
|
|
61
|
+
}
|