@pexis/auth 0.0.0-stage → 0.1.0
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/LICENSE +23 -0
- package/README.md +31 -2
- package/dist/index.d.ts +52 -0
- package/dist/index.js +363 -0
- package/dist/index.js.map +1 -0
- package/dist/jwt.d.ts +62 -0
- package/dist/jwt.js +140 -0
- package/dist/jwt.js.map +1 -0
- package/dist/providers.d.ts +81 -0
- package/dist/providers.js +164 -0
- package/dist/providers.js.map +1 -0
- package/dist/server/client.d.ts +48 -0
- package/dist/server/client.js +208 -0
- package/dist/server/client.js.map +1 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +7 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/plugin.d.ts +37 -0
- package/dist/server/plugin.js +522 -0
- package/dist/server/plugin.js.map +1 -0
- package/dist/server/store.d.ts +81 -0
- package/dist/server/store.js +106 -0
- package/dist/server/store.js.map +1 -0
- package/package.json +38 -4
package/dist/jwt.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// JSON Web Token verification and signing on WebCrypto, for OpenID Connect.
|
|
2
|
+
// Deliberately narrow: RS256 and ES256 only. Never "none", never HMAC, and
|
|
3
|
+
// the key's algorithm must match the token's.
|
|
4
|
+
const ALGORITHMS = {
|
|
5
|
+
RS256: { import: { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }, verify: { name: 'RSASSA-PKCS1-v1_5' }, kty: 'RSA' },
|
|
6
|
+
ES256: { import: { name: 'ECDSA', namedCurve: 'P-256' }, verify: { name: 'ECDSA', hash: 'SHA-256' }, kty: 'EC' },
|
|
7
|
+
};
|
|
8
|
+
export class JwtError extends Error {
|
|
9
|
+
}
|
|
10
|
+
export function base64urlEncode(bytes) {
|
|
11
|
+
const data = typeof bytes === 'string' ? new TextEncoder().encode(bytes) : bytes;
|
|
12
|
+
let binary = '';
|
|
13
|
+
for (const b of data)
|
|
14
|
+
binary += String.fromCharCode(b);
|
|
15
|
+
return btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/, '');
|
|
16
|
+
}
|
|
17
|
+
export function base64urlDecode(text) {
|
|
18
|
+
if (!/^[A-Za-z0-9_-]*$/.test(text))
|
|
19
|
+
throw new JwtError('Invalid base64url.');
|
|
20
|
+
const padded = text.replaceAll('-', '+').replaceAll('_', '/') + '='.repeat((4 - (text.length % 4)) % 4);
|
|
21
|
+
const binary = atob(padded);
|
|
22
|
+
const out = new Uint8Array(binary.length);
|
|
23
|
+
for (let i = 0; i < binary.length; i++)
|
|
24
|
+
out[i] = binary.charCodeAt(i);
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
function parseJson(bytes) {
|
|
28
|
+
try {
|
|
29
|
+
const value = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes));
|
|
30
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
31
|
+
throw new Error();
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
throw new JwtError('A token part is not a JSON object.');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function decodeJwt(token) {
|
|
39
|
+
if (typeof token !== 'string' || token.length > 16_384)
|
|
40
|
+
throw new JwtError('Token missing or too large.');
|
|
41
|
+
const parts = token.split('.');
|
|
42
|
+
if (parts.length !== 3)
|
|
43
|
+
throw new JwtError('A token has three parts.');
|
|
44
|
+
const [h, p, s] = parts;
|
|
45
|
+
return { header: parseJson(base64urlDecode(h)), claims: parseJson(base64urlDecode(p)), signingInput: `${h}.${p}`, signature: base64urlDecode(s) };
|
|
46
|
+
}
|
|
47
|
+
/** Fetches and caches a provider's signing keys. */
|
|
48
|
+
export class KeySet {
|
|
49
|
+
keys = new Map();
|
|
50
|
+
fetchedAt = 0;
|
|
51
|
+
url;
|
|
52
|
+
fetch;
|
|
53
|
+
constructor(url, fetcher = fetch) {
|
|
54
|
+
this.url = url;
|
|
55
|
+
this.fetch = fetcher;
|
|
56
|
+
}
|
|
57
|
+
loading = null;
|
|
58
|
+
// Concurrent callers share one request for the key set.
|
|
59
|
+
load() {
|
|
60
|
+
this.loading ??= this.fetchKeys().finally(() => (this.loading = null));
|
|
61
|
+
return this.loading;
|
|
62
|
+
}
|
|
63
|
+
async fetchKeys() {
|
|
64
|
+
const res = await this.fetch(this.url, { headers: { accept: 'application/json' } });
|
|
65
|
+
if (!res.ok)
|
|
66
|
+
throw new JwtError(`Could not fetch signing keys (${res.status}).`);
|
|
67
|
+
const body = (await res.json());
|
|
68
|
+
if (!Array.isArray(body.keys))
|
|
69
|
+
throw new JwtError('The key set has no keys.');
|
|
70
|
+
this.keys = new Map(body.keys.filter((k) => typeof k.kid === 'string').map((k) => [k.kid, k]));
|
|
71
|
+
this.fetchedAt = Date.now();
|
|
72
|
+
}
|
|
73
|
+
async get(kid) {
|
|
74
|
+
if (Date.now() - this.fetchedAt > 3_600_000)
|
|
75
|
+
await this.load();
|
|
76
|
+
let key = this.keys.get(kid);
|
|
77
|
+
// Keys rotate: refetch once for an unknown key id, at most once a minute.
|
|
78
|
+
// Measured after any load above, so a key set fetched just now is not fetched again.
|
|
79
|
+
if (!key && Date.now() - this.fetchedAt > 60_000) {
|
|
80
|
+
await this.load();
|
|
81
|
+
key = this.keys.get(kid);
|
|
82
|
+
}
|
|
83
|
+
if (!key)
|
|
84
|
+
throw new JwtError('The token was signed with an unknown key.');
|
|
85
|
+
return key;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
export async function verifyJwt(token, keys, expect) {
|
|
89
|
+
const { header, claims, signingInput, signature } = decodeJwt(token);
|
|
90
|
+
if (!(header.alg in ALGORITHMS))
|
|
91
|
+
throw new JwtError(`Algorithm "${String(header.alg)}" is not accepted.`);
|
|
92
|
+
if (typeof header.kid !== 'string')
|
|
93
|
+
throw new JwtError('The token names no key.');
|
|
94
|
+
const alg = header.alg;
|
|
95
|
+
const jwk = await keys.get(header.kid);
|
|
96
|
+
if (jwk.kty !== ALGORITHMS[alg].kty)
|
|
97
|
+
throw new JwtError('The key does not match the token algorithm.');
|
|
98
|
+
if (jwk.alg && jwk.alg !== alg)
|
|
99
|
+
throw new JwtError('The key is for a different algorithm.');
|
|
100
|
+
if (jwk.use && jwk.use !== 'sig')
|
|
101
|
+
throw new JwtError('The key is not a signing key.');
|
|
102
|
+
const key = await crypto.subtle.importKey('jwk', { ...jwk, ext: true }, ALGORITHMS[alg].import, false, ['verify']);
|
|
103
|
+
const ok = await crypto.subtle.verify(ALGORITHMS[alg].verify, key, signature, new TextEncoder().encode(signingInput));
|
|
104
|
+
if (!ok)
|
|
105
|
+
throw new JwtError('The token signature is not valid.');
|
|
106
|
+
const now = Math.floor((expect.now ?? Date.now()) / 1000);
|
|
107
|
+
const skew = expect.skewSeconds ?? 60;
|
|
108
|
+
const iss = claims.iss;
|
|
109
|
+
const issuerOk = typeof iss === 'string' &&
|
|
110
|
+
(typeof expect.issuer === 'function' ? expect.issuer(iss, claims) : Array.isArray(expect.issuer) ? expect.issuer.includes(iss) : iss === expect.issuer);
|
|
111
|
+
if (!issuerOk)
|
|
112
|
+
throw new JwtError('The token comes from the wrong issuer.');
|
|
113
|
+
const aud = claims.aud;
|
|
114
|
+
const audiences = typeof aud === 'string' ? [aud] : Array.isArray(aud) ? aud : [];
|
|
115
|
+
if (!audiences.includes(expect.audience))
|
|
116
|
+
throw new JwtError('The token is for a different application.');
|
|
117
|
+
if (audiences.length > 1 && claims.azp !== expect.audience)
|
|
118
|
+
throw new JwtError('The token has several audiences and is not authorized for this application.');
|
|
119
|
+
if (typeof claims.exp !== 'number' || claims.exp + skew < now)
|
|
120
|
+
throw new JwtError('The token has expired.');
|
|
121
|
+
if (typeof claims.iat === 'number' && claims.iat - skew > now)
|
|
122
|
+
throw new JwtError('The token was issued in the future.');
|
|
123
|
+
if (typeof claims.nbf === 'number' && claims.nbf - skew > now)
|
|
124
|
+
throw new JwtError('The token is not valid yet.');
|
|
125
|
+
if (expect.nonce !== undefined && claims.nonce !== expect.nonce)
|
|
126
|
+
throw new JwtError('The token nonce does not match.');
|
|
127
|
+
if (typeof claims.sub !== 'string' || !claims.sub)
|
|
128
|
+
throw new JwtError('The token has no subject.');
|
|
129
|
+
return claims;
|
|
130
|
+
}
|
|
131
|
+
/** Signs an ES256 token with a PKCS#8 PEM private key, for Apple's client secret. */
|
|
132
|
+
export async function signEs256(header, claims, pem) {
|
|
133
|
+
const body = pem.replace(/-----(BEGIN|END) PRIVATE KEY-----/g, '').replace(/\s+/g, '');
|
|
134
|
+
const der = Uint8Array.from(atob(body), (c) => c.charCodeAt(0));
|
|
135
|
+
const key = await crypto.subtle.importKey('pkcs8', der, { name: 'ECDSA', namedCurve: 'P-256' }, false, ['sign']);
|
|
136
|
+
const input = `${base64urlEncode(JSON.stringify({ ...header, alg: 'ES256' }))}.${base64urlEncode(JSON.stringify(claims))}`;
|
|
137
|
+
const signature = await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, key, new TextEncoder().encode(input));
|
|
138
|
+
return `${input}.${base64urlEncode(new Uint8Array(signature))}`;
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=jwt.js.map
|
package/dist/jwt.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwt.js","sourceRoot":"","sources":["../src/jwt.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,2EAA2E;AAC3E,8CAA8C;AA+B9C,MAAM,UAAU,GAAG;IACjB,KAAK,EAAE,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,mBAAmB,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,mBAAmB,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE;IACpH,KAAK,EAAE,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,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I,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IACrH,OAAO,GAAG,KAAK,IAAI,eAAe,CAAC,IAAI,UAAU,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { type Claims } from './jwt.js';
|
|
2
|
+
export interface TokenResponse {
|
|
3
|
+
access_token?: string;
|
|
4
|
+
id_token?: string;
|
|
5
|
+
token_type?: string;
|
|
6
|
+
expires_in?: number;
|
|
7
|
+
scope?: string;
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
/** What a provider tells us about the person. */
|
|
11
|
+
export interface Profile {
|
|
12
|
+
/** The provider's stable ID for the person. */
|
|
13
|
+
subject: string;
|
|
14
|
+
email?: string;
|
|
15
|
+
/** Whether the provider verified the email. Never link accounts by an unverified email. */
|
|
16
|
+
emailVerified: boolean;
|
|
17
|
+
name?: string;
|
|
18
|
+
picture?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface ProfileInput {
|
|
21
|
+
claims?: Claims;
|
|
22
|
+
tokens: TokenResponse;
|
|
23
|
+
fetch: typeof fetch;
|
|
24
|
+
/** Extra fields a form_post provider sent with the callback (Apple's "user"). Untrusted. */
|
|
25
|
+
form?: Record<string, string>;
|
|
26
|
+
}
|
|
27
|
+
export interface Provider {
|
|
28
|
+
id: string;
|
|
29
|
+
/** Shown on the sign-in button: "Continue with <name>". */
|
|
30
|
+
name: string;
|
|
31
|
+
clientId: string;
|
|
32
|
+
/** A fixed secret, or a function for providers whose secret is a short-lived token (Apple). */
|
|
33
|
+
clientSecret?: string | (() => Promise<string>);
|
|
34
|
+
scopes: string[];
|
|
35
|
+
/** OpenID Connect issuer. Set this, or the endpoints below. */
|
|
36
|
+
issuer?: string;
|
|
37
|
+
/** For providers whose issuer varies, such as Microsoft's multi-tenant endpoint. */
|
|
38
|
+
issuerCheck?: (iss: string, claims: Claims) => boolean;
|
|
39
|
+
authorizationEndpoint?: string;
|
|
40
|
+
tokenEndpoint?: string;
|
|
41
|
+
jwksUri?: string;
|
|
42
|
+
/** Whether the provider returns an ID token to verify. */
|
|
43
|
+
oidc: boolean;
|
|
44
|
+
/** Some providers post the callback as a form (Apple, when asking for name and email). */
|
|
45
|
+
responseMode?: 'query' | 'form_post';
|
|
46
|
+
/** Extra parameters for the authorization request, such as prompt. */
|
|
47
|
+
authorizationParams?: Record<string, string>;
|
|
48
|
+
tokenAuth?: 'post' | 'basic';
|
|
49
|
+
profile(input: ProfileInput): Promise<Profile>;
|
|
50
|
+
}
|
|
51
|
+
interface Credentials {
|
|
52
|
+
clientId: string;
|
|
53
|
+
clientSecret: string;
|
|
54
|
+
/** Extra scopes beyond the defaults. */
|
|
55
|
+
scopes?: string[];
|
|
56
|
+
authorizationParams?: Record<string, string>;
|
|
57
|
+
}
|
|
58
|
+
export declare function google(options: Credentials): Provider;
|
|
59
|
+
export declare function microsoft(options: Credentials & {
|
|
60
|
+
tenant?: string;
|
|
61
|
+
}): Provider;
|
|
62
|
+
export declare function github(options: Credentials): Provider;
|
|
63
|
+
export interface AppleOptions {
|
|
64
|
+
/** The Services ID. */
|
|
65
|
+
clientId: string;
|
|
66
|
+
teamId: string;
|
|
67
|
+
keyId: string;
|
|
68
|
+
/** The .p8 private key, as PEM text. Read it from a secret, never commit it. */
|
|
69
|
+
privateKey: string;
|
|
70
|
+
}
|
|
71
|
+
export declare function apple(options: AppleOptions): Provider;
|
|
72
|
+
export interface OidcOptions extends Credentials {
|
|
73
|
+
id: string;
|
|
74
|
+
name: string;
|
|
75
|
+
issuer: string;
|
|
76
|
+
/** Default ["openid", "email", "profile"]. */
|
|
77
|
+
scopes?: string[];
|
|
78
|
+
}
|
|
79
|
+
/** Any OpenID Connect provider: Okta, Auth0, Keycloak, Entra ID, Cognito, GitLab and others. */
|
|
80
|
+
export declare function oidc(options: OidcOptions): Provider;
|
|
81
|
+
export {};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// Sign-in providers. Each one is a few facts about the provider plus a way to
|
|
2
|
+
// turn its answer into a profile. OpenID Connect providers publish their
|
|
3
|
+
// endpoints at <issuer>/.well-known/openid-configuration.
|
|
4
|
+
import { signEs256 } from './jwt.js';
|
|
5
|
+
const str = (v) => (typeof v === 'string' && v ? v : undefined);
|
|
6
|
+
const verified = (v) => v === true || v === 'true';
|
|
7
|
+
function claimsProfile(claims) {
|
|
8
|
+
if (!claims?.sub)
|
|
9
|
+
throw new Error('The provider returned no subject.');
|
|
10
|
+
const profile = { subject: claims.sub, emailVerified: verified(claims.email_verified) };
|
|
11
|
+
const email = str(claims.email);
|
|
12
|
+
const name = str(claims.name);
|
|
13
|
+
const picture = str(claims.picture);
|
|
14
|
+
if (email)
|
|
15
|
+
profile.email = email;
|
|
16
|
+
if (name)
|
|
17
|
+
profile.name = name;
|
|
18
|
+
if (picture && /^https:\/\//.test(picture))
|
|
19
|
+
profile.picture = picture;
|
|
20
|
+
return profile;
|
|
21
|
+
}
|
|
22
|
+
export function google(options) {
|
|
23
|
+
return {
|
|
24
|
+
id: 'google',
|
|
25
|
+
name: 'Google',
|
|
26
|
+
clientId: options.clientId,
|
|
27
|
+
clientSecret: options.clientSecret,
|
|
28
|
+
scopes: ['openid', 'email', 'profile', ...(options.scopes ?? [])],
|
|
29
|
+
issuer: 'https://accounts.google.com',
|
|
30
|
+
// Google signs ID tokens with either spelling of its issuer.
|
|
31
|
+
issuerCheck: (iss) => iss === 'https://accounts.google.com' || iss === 'accounts.google.com',
|
|
32
|
+
oidc: true,
|
|
33
|
+
authorizationParams: { prompt: 'select_account', ...options.authorizationParams },
|
|
34
|
+
profile: async ({ claims }) => claimsProfile(claims),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function microsoft(options) {
|
|
38
|
+
const tenant = (options.tenant ?? 'common').toLowerCase();
|
|
39
|
+
const multi = ['common', 'organizations', 'consumers'].includes(tenant);
|
|
40
|
+
// Tokens name their tenant by ID only, so a domain could never be checked.
|
|
41
|
+
if (!multi && !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(tenant)) {
|
|
42
|
+
throw new Error('microsoft(): tenant must be "common", "organizations", "consumers" or a tenant ID (a GUID, shown as "Directory (tenant) ID" in the Azure portal).');
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
id: 'microsoft',
|
|
46
|
+
name: 'Microsoft',
|
|
47
|
+
clientId: options.clientId,
|
|
48
|
+
clientSecret: options.clientSecret,
|
|
49
|
+
scopes: ['openid', 'email', 'profile', ...(options.scopes ?? [])],
|
|
50
|
+
issuer: `https://login.microsoftonline.com/${tenant}/v2.0`,
|
|
51
|
+
// Multi-tenant tokens are issued by the user's own tenant.
|
|
52
|
+
issuerCheck: (iss, claims) => typeof claims.tid === 'string' && /^[0-9a-f-]{36}$/.test(claims.tid) && iss === `https://login.microsoftonline.com/${claims.tid}/v2.0` && (multi || claims.tid === tenant),
|
|
53
|
+
oidc: true,
|
|
54
|
+
...(options.authorizationParams ? { authorizationParams: options.authorizationParams } : {}),
|
|
55
|
+
profile: async ({ claims }) => {
|
|
56
|
+
const profile = claimsProfile(claims);
|
|
57
|
+
// Microsoft does not promise the email claim is verified.
|
|
58
|
+
profile.emailVerified = false;
|
|
59
|
+
const email = str(claims?.email) ?? str(claims?.preferred_username);
|
|
60
|
+
if (email && email.includes('@'))
|
|
61
|
+
profile.email = email;
|
|
62
|
+
return profile;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export function github(options) {
|
|
67
|
+
return {
|
|
68
|
+
id: 'github',
|
|
69
|
+
name: 'GitHub',
|
|
70
|
+
clientId: options.clientId,
|
|
71
|
+
clientSecret: options.clientSecret,
|
|
72
|
+
scopes: ['read:user', 'user:email', ...(options.scopes ?? [])],
|
|
73
|
+
authorizationEndpoint: 'https://github.com/login/oauth/authorize',
|
|
74
|
+
tokenEndpoint: 'https://github.com/login/oauth/access_token',
|
|
75
|
+
oidc: false,
|
|
76
|
+
...(options.authorizationParams ? { authorizationParams: options.authorizationParams } : {}),
|
|
77
|
+
async profile({ tokens, fetch }) {
|
|
78
|
+
if (!tokens.access_token)
|
|
79
|
+
throw new Error('GitHub returned no access token.');
|
|
80
|
+
const headers = { authorization: `Bearer ${tokens.access_token}`, accept: 'application/vnd.github+json', 'user-agent': 'pexis-auth' };
|
|
81
|
+
const userRes = await fetch('https://api.github.com/user', { headers });
|
|
82
|
+
if (!userRes.ok)
|
|
83
|
+
throw new Error(`GitHub user lookup failed (${userRes.status}).`);
|
|
84
|
+
const user = (await userRes.json());
|
|
85
|
+
if (typeof user.id !== 'number')
|
|
86
|
+
throw new Error('GitHub returned no user ID.');
|
|
87
|
+
const profile = { subject: String(user.id), emailVerified: false };
|
|
88
|
+
const name = str(user.name) ?? str(user.login);
|
|
89
|
+
if (name)
|
|
90
|
+
profile.name = name;
|
|
91
|
+
if (str(user.avatar_url)?.startsWith('https://'))
|
|
92
|
+
profile.picture = user.avatar_url;
|
|
93
|
+
const emailsRes = await fetch('https://api.github.com/user/emails', { headers });
|
|
94
|
+
if (emailsRes.ok) {
|
|
95
|
+
const emails = (await emailsRes.json());
|
|
96
|
+
const primary = Array.isArray(emails) ? emails.find((e) => e.primary && e.verified) : undefined;
|
|
97
|
+
if (primary) {
|
|
98
|
+
profile.email = primary.email;
|
|
99
|
+
profile.emailVerified = true;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return profile;
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
export function apple(options) {
|
|
107
|
+
let cached;
|
|
108
|
+
return {
|
|
109
|
+
id: 'apple',
|
|
110
|
+
name: 'Apple',
|
|
111
|
+
clientId: options.clientId,
|
|
112
|
+
// Apple's client secret is a short-lived ES256 token signed with your key.
|
|
113
|
+
clientSecret: async () => {
|
|
114
|
+
const now = Math.floor(Date.now() / 1000);
|
|
115
|
+
if (cached && cached.until > now + 60)
|
|
116
|
+
return cached.secret;
|
|
117
|
+
const until = now + 30 * 60;
|
|
118
|
+
const secret = await signEs256({ kid: options.keyId }, { iss: options.teamId, iat: now, exp: until, aud: 'https://appleid.apple.com', sub: options.clientId }, options.privateKey);
|
|
119
|
+
cached = { secret, until };
|
|
120
|
+
return secret;
|
|
121
|
+
},
|
|
122
|
+
scopes: ['name', 'email'],
|
|
123
|
+
issuer: 'https://appleid.apple.com',
|
|
124
|
+
authorizationEndpoint: 'https://appleid.apple.com/auth/authorize',
|
|
125
|
+
tokenEndpoint: 'https://appleid.apple.com/auth/token',
|
|
126
|
+
jwksUri: 'https://appleid.apple.com/auth/keys',
|
|
127
|
+
oidc: true,
|
|
128
|
+
// Apple only returns name and email to a form-posted callback.
|
|
129
|
+
responseMode: 'form_post',
|
|
130
|
+
async profile({ claims, form }) {
|
|
131
|
+
const profile = claimsProfile(claims);
|
|
132
|
+
// Apple sends the name once, on first sign-in, as untrusted form data.
|
|
133
|
+
try {
|
|
134
|
+
const user = form?.user ? JSON.parse(form.user) : undefined;
|
|
135
|
+
const name = [user?.name?.firstName, user?.name?.lastName].filter((p) => typeof p === 'string' && p).join(' ').slice(0, 200);
|
|
136
|
+
if (name)
|
|
137
|
+
profile.name = name;
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
// Ignore a malformed name; the ID token is what counts.
|
|
141
|
+
}
|
|
142
|
+
return profile;
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/** Any OpenID Connect provider: Okta, Auth0, Keycloak, Entra ID, Cognito, GitLab and others. */
|
|
147
|
+
export function oidc(options) {
|
|
148
|
+
if (!/^[a-z][a-z0-9-]{0,31}$/.test(options.id))
|
|
149
|
+
throw new Error('oidc(): id must be lowercase letters, digits and hyphens.');
|
|
150
|
+
return {
|
|
151
|
+
id: options.id,
|
|
152
|
+
name: options.name,
|
|
153
|
+
clientId: options.clientId,
|
|
154
|
+
clientSecret: options.clientSecret,
|
|
155
|
+
scopes: options.scopes ?? ['openid', 'email', 'profile'],
|
|
156
|
+
// Kept exactly as given: some issuers end in "/" (Auth0), and the ID
|
|
157
|
+
// token's iss claim must match it character for character.
|
|
158
|
+
issuer: options.issuer,
|
|
159
|
+
oidc: true,
|
|
160
|
+
...(options.authorizationParams ? { authorizationParams: options.authorizationParams } : {}),
|
|
161
|
+
profile: async ({ claims }) => claimsProfile(claims),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
//# sourceMappingURL=providers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"providers.js","sourceRoot":"","sources":["../src/providers.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,SAAS,EAAe,MAAM,UAAU,CAAC;AAuDlD,MAAM,GAAG,GAAG,CAAC,CAAU,EAAsB,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAC7F,MAAM,QAAQ,GAAG,CAAC,CAAU,EAAW,EAAE,CAAC,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,MAAM,CAAC;AAErE,SAAS,aAAa,CAAC,MAA0B;IAC/C,IAAI,CAAC,MAAM,EAAE,GAAG;QAAE,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IACvE,MAAM,OAAO,GAAY,EAAE,OAAO,EAAE,MAAM,CAAC,GAAG,EAAE,aAAa,EAAE,QAAQ,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE,CAAC;IACjG,MAAM,KAAK,GAAG,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAChC,MAAM,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC9B,MAAM,OAAO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+
status: number;
|
|
4
|
+
headers: Headers;
|
|
5
|
+
body: Uint8Array;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Fetches a metadata URL. Must not follow redirects, and must stop at maxBytes
|
|
9
|
+
* and when the signal aborts. publicFetch from @pexis/node/public-fetch also
|
|
10
|
+
* refuses names that resolve to non-public addresses; the default (web fetch)
|
|
11
|
+
* cannot see addresses, so it is accepted only for clients the app lists.
|
|
12
|
+
*/
|
|
13
|
+
export type MetadataFetch = (url: URL, options: {
|
|
14
|
+
signal: AbortSignal;
|
|
15
|
+
maxBytes: number;
|
|
16
|
+
}) => Promise<MetadataResponse>;
|
|
17
|
+
export interface ClientMetadata {
|
|
18
|
+
clientId: string;
|
|
19
|
+
/** What the client calls itself. Untrusted text: show it escaped, never as the client's identity. */
|
|
20
|
+
name: string;
|
|
21
|
+
redirectUris: string[];
|
|
22
|
+
}
|
|
23
|
+
/** A client that cannot be used, with a reason safe to show the person. */
|
|
24
|
+
export declare class ClientError extends Error {
|
|
25
|
+
}
|
|
26
|
+
/** The client_id as a URL, or a ClientError saying why it cannot be one. */
|
|
27
|
+
export declare function clientIdUrl(raw: string): URL;
|
|
28
|
+
/** A redirect URI a client may list: https, or http on a loopback address for apps on the person's computer (RFC 8252). */
|
|
29
|
+
export declare function checkRedirectUri(raw: string): URL;
|
|
30
|
+
export declare const isLoopback: (url: URL) => boolean;
|
|
31
|
+
/** Whether a requested redirect URI is one the client listed: exact, except the port of a loopback URI (RFC 8252 §7.3). */
|
|
32
|
+
export declare function redirectAllowed(client: ClientMetadata, requested: string): boolean;
|
|
33
|
+
/** Web fetch, for clients the app lists by name. No redirects, a byte limit counted after decompression. */
|
|
34
|
+
export declare const webMetadataFetch: MetadataFetch;
|
|
35
|
+
export interface ClientLoaderOptions {
|
|
36
|
+
fetch: MetadataFetch;
|
|
37
|
+
/** Cached clients and failures, at most. Default 1000. */
|
|
38
|
+
maxEntries?: number;
|
|
39
|
+
/** Fetches running at once, in all. Default 8. */
|
|
40
|
+
concurrency?: number;
|
|
41
|
+
/** Fetches running at once to one host. Default 2. */
|
|
42
|
+
perHost?: number;
|
|
43
|
+
}
|
|
44
|
+
export interface ClientLoader {
|
|
45
|
+
/** The client, from the cache or fetched. Throws ClientError. */
|
|
46
|
+
load(clientId: string): Promise<ClientMetadata>;
|
|
47
|
+
}
|
|
48
|
+
export declare function clientLoader(options: ClientLoaderOptions): ClientLoader;
|