@polaris-auth/client 0.7.0 → 0.8.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/README.md +38 -3
- package/dist/index.d.ts +11 -2
- package/dist/index.js +8 -1
- package/dist/passkey.d.ts +2028 -0
- package/dist/passkey.js +19 -0
- package/dist/social.d.ts +2791 -0
- package/dist/social.js +23 -0
- package/dist/webauthn.d.ts +27 -0
- package/dist/webauthn.js +129 -0
- package/openapi.json +1889 -1
- package/package.json +8 -3
- package/src/index.ts +13 -2
- package/src/passkey.ts +26 -0
- package/src/schema.d.ts +1344 -0
- package/src/social.ts +30 -0
- package/src/webauthn.ts +172 -0
package/src/social.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/* Generated by scripts/generate-namespaces.mjs from openapi.json; do not edit. */
|
|
2
|
+
import type { Client } from "openapi-fetch";
|
|
3
|
+
import type { paths } from "./schema.js";
|
|
4
|
+
import type { Init } from "./namespace.js";
|
|
5
|
+
|
|
6
|
+
/** The `social` plugin's routes as methods of `client.social`. */
|
|
7
|
+
export function social(client: Client<paths>) {
|
|
8
|
+
return {
|
|
9
|
+
/** GET /social/accounts: List your linked provider accounts */
|
|
10
|
+
accounts: (...init: Init<"/social/accounts", "get">) => client.GET("/social/accounts", ...init),
|
|
11
|
+
/** GET /social/{provider}/callback: The provider's callback */
|
|
12
|
+
callback: (...init: Init<"/social/{provider}/callback", "get">) => client.GET("/social/{provider}/callback", ...init),
|
|
13
|
+
/** POST /social/{provider}/callback: The provider's callback (form_post) */
|
|
14
|
+
callbackPost: (...init: Init<"/social/{provider}/callback", "post">) => client.POST("/social/{provider}/callback", ...init),
|
|
15
|
+
/** POST /social/exchange: Exchange the sign-in code for the session */
|
|
16
|
+
exchange: (...init: Init<"/social/exchange", "post">) => client.POST("/social/exchange", ...init),
|
|
17
|
+
/** POST /social/{provider}/link: Link a provider to your account */
|
|
18
|
+
link: (...init: Init<"/social/{provider}/link", "post">) => client.POST("/social/{provider}/link", ...init),
|
|
19
|
+
/** POST /social/google/one-tap: Sign in with Google One Tap */
|
|
20
|
+
oneTap: (...init: Init<"/social/google/one-tap", "post">) => client.POST("/social/google/one-tap", ...init),
|
|
21
|
+
/** POST /social/{provider}/start: Start a social sign-in */
|
|
22
|
+
start: (...init: Init<"/social/{provider}/start", "post">) => client.POST("/social/{provider}/start", ...init),
|
|
23
|
+
/** POST /social/{provider}/token: Get a provider token for the user */
|
|
24
|
+
token: (...init: Init<"/social/{provider}/token", "post">) => client.POST("/social/{provider}/token", ...init),
|
|
25
|
+
/** DELETE /social/{provider}: Unlink a provider */
|
|
26
|
+
unlink: (...init: Init<"/social/{provider}", "delete">) => client.DELETE("/social/{provider}", ...init),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type SocialApi = ReturnType<typeof social>;
|
package/src/webauthn.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type { PolarisClient } from "./index.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The browser side of `polaris/passkey`: `navigator.credentials` with the options the routes answer.
|
|
5
|
+
* `registerPasskey()` and `signInWithPasskey()` run a whole ceremony; `passkeyAssertion()` yields the
|
|
6
|
+
* assertion to send as the `code` of core's `POST /auth/mfa/verify` or `/auth/mfa/step-up` when the
|
|
7
|
+
* passkey is the second factor. Needs a secure context (https, or localhost).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** What the browser needs to make a credential: the JSON the routes answer (WebAuthn Level 3). */
|
|
11
|
+
type CreationOptionsJSON = Record<string, unknown>;
|
|
12
|
+
type RequestOptionsJSON = Record<string, unknown>;
|
|
13
|
+
|
|
14
|
+
interface PasskeyRegistration {
|
|
15
|
+
passkey: Record<string, unknown>;
|
|
16
|
+
recovery_codes: string[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SignInWithPasskeyOptions {
|
|
20
|
+
/** Conditional UI: the browser offers the passkey in the form's autofill instead of a prompt (`autocomplete="username webauthn"`). */
|
|
21
|
+
conditional?: boolean;
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Registers a passkey for the signed-in user of `client` and answers the passkey (and the recovery
|
|
27
|
+
* codes when it is the user's first MFA factor).
|
|
28
|
+
*/
|
|
29
|
+
export async function registerPasskey(client: PolarisClient, name?: string): Promise<PasskeyRegistration> {
|
|
30
|
+
const options = await client.passkey.registerOptions();
|
|
31
|
+
if (options.data === undefined) {
|
|
32
|
+
throw new Error(describe("POST /passkey/register/options", options.error));
|
|
33
|
+
}
|
|
34
|
+
const credential = await navigator.credentials.create({ publicKey: parseCreationOptions(options.data.data as CreationOptionsJSON) });
|
|
35
|
+
if (!(credential instanceof PublicKeyCredential)) {
|
|
36
|
+
throw new Error("The browser created no passkey.");
|
|
37
|
+
}
|
|
38
|
+
const body: Record<string, unknown> = { credential: serialize(credential) };
|
|
39
|
+
if (name !== undefined) {
|
|
40
|
+
body["name"] = name;
|
|
41
|
+
}
|
|
42
|
+
const registered = await client.passkey.registerVerify({ body } as never);
|
|
43
|
+
if (registered.data === undefined) {
|
|
44
|
+
throw new Error(describe("POST /passkey/register/verify", registered.error));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return registered.data.data as unknown as PasskeyRegistration;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Signs in with a passkey (discoverable, or from the form's autofill with `conditional`) and answers
|
|
52
|
+
* core's login envelope, or its `mfa_required` ticket when the authenticator did not verify the user.
|
|
53
|
+
*/
|
|
54
|
+
export async function signInWithPasskey(client: PolarisClient, options: SignInWithPasskeyOptions = {}): Promise<Record<string, unknown>> {
|
|
55
|
+
const credential = await passkeyAssertion(client, options);
|
|
56
|
+
const verified = await client.passkey.authenticateVerify({ body: { credential: JSON.parse(credential) } } as never);
|
|
57
|
+
if (verified.data === undefined) {
|
|
58
|
+
throw new Error(describe("POST /passkey/authenticate/verify", verified.error));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return verified.data.data as Record<string, unknown>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The assertion for a fresh challenge, as the JSON string the routes take (`credential`, or core's `code`).
|
|
66
|
+
*/
|
|
67
|
+
export async function passkeyAssertion(client: PolarisClient, options: SignInWithPasskeyOptions = {}): Promise<string> {
|
|
68
|
+
const request = await client.passkey.authenticateOptions();
|
|
69
|
+
if (request.data === undefined) {
|
|
70
|
+
throw new Error(describe("POST /passkey/authenticate/options", request.error));
|
|
71
|
+
}
|
|
72
|
+
const get: CredentialRequestOptions & { mediation?: string } = { publicKey: parseRequestOptions(request.data.data as RequestOptionsJSON) };
|
|
73
|
+
if (options.conditional === true) {
|
|
74
|
+
get.mediation = "conditional";
|
|
75
|
+
}
|
|
76
|
+
if (options.signal !== undefined) {
|
|
77
|
+
get.signal = options.signal;
|
|
78
|
+
}
|
|
79
|
+
const credential = await navigator.credentials.get(get);
|
|
80
|
+
if (!(credential instanceof PublicKeyCredential)) {
|
|
81
|
+
throw new Error("The browser answered no passkey.");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return JSON.stringify(serialize(credential));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Whether this browser can autofill a passkey into a form (conditional UI). */
|
|
88
|
+
export async function passkeyAutofillAvailable(): Promise<boolean> {
|
|
89
|
+
const api = globalThis.PublicKeyCredential as unknown as { isConditionalMediationAvailable?: () => Promise<boolean> } | undefined;
|
|
90
|
+
|
|
91
|
+
return api?.isConditionalMediationAvailable !== undefined ? api.isConditionalMediationAvailable() : false;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function parseCreationOptions(json: CreationOptionsJSON): PublicKeyCredentialCreationOptions {
|
|
95
|
+
const api = globalThis.PublicKeyCredential as unknown as { parseCreationOptionsFromJSON?: (json: unknown) => PublicKeyCredentialCreationOptions };
|
|
96
|
+
if (api.parseCreationOptionsFromJSON !== undefined) {
|
|
97
|
+
return api.parseCreationOptionsFromJSON(json);
|
|
98
|
+
}
|
|
99
|
+
const user = json["user"] as Record<string, unknown>;
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
...json,
|
|
103
|
+
challenge: fromBase64Url(json["challenge"] as string),
|
|
104
|
+
user: { ...user, id: fromBase64Url(user["id"] as string) },
|
|
105
|
+
excludeCredentials: descriptors(json["excludeCredentials"]),
|
|
106
|
+
} as unknown as PublicKeyCredentialCreationOptions;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function parseRequestOptions(json: RequestOptionsJSON): PublicKeyCredentialRequestOptions {
|
|
110
|
+
const api = globalThis.PublicKeyCredential as unknown as { parseRequestOptionsFromJSON?: (json: unknown) => PublicKeyCredentialRequestOptions };
|
|
111
|
+
if (api.parseRequestOptionsFromJSON !== undefined) {
|
|
112
|
+
return api.parseRequestOptionsFromJSON(json);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return { ...json, challenge: fromBase64Url(json["challenge"] as string), allowCredentials: descriptors(json["allowCredentials"]) } as unknown as PublicKeyCredentialRequestOptions;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function descriptors(list: unknown): PublicKeyCredentialDescriptor[] {
|
|
119
|
+
return Array.isArray(list) ? list.map((entry: Record<string, unknown>) => ({ ...entry, id: fromBase64Url(entry["id"] as string) }) as PublicKeyCredentialDescriptor) : [];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** `credential.toJSON()` where the browser has it; the same shape by hand otherwise. */
|
|
123
|
+
function serialize(credential: PublicKeyCredential): Record<string, unknown> {
|
|
124
|
+
const withJson = credential as PublicKeyCredential & { toJSON?: () => Record<string, unknown> };
|
|
125
|
+
if (withJson.toJSON !== undefined) {
|
|
126
|
+
return withJson.toJSON();
|
|
127
|
+
}
|
|
128
|
+
const response = credential.response as AuthenticatorAttestationResponse & AuthenticatorAssertionResponse;
|
|
129
|
+
const body: Record<string, unknown> = { clientDataJSON: toBase64Url(response.clientDataJSON) };
|
|
130
|
+
if (response.attestationObject !== undefined) {
|
|
131
|
+
body["attestationObject"] = toBase64Url(response.attestationObject);
|
|
132
|
+
body["transports"] = typeof response.getTransports === "function" ? response.getTransports() : [];
|
|
133
|
+
} else {
|
|
134
|
+
body["authenticatorData"] = toBase64Url(response.authenticatorData);
|
|
135
|
+
body["signature"] = toBase64Url(response.signature);
|
|
136
|
+
body["userHandle"] = response.userHandle === null ? null : toBase64Url(response.userHandle);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
id: credential.id,
|
|
141
|
+
rawId: toBase64Url(credential.rawId),
|
|
142
|
+
type: credential.type,
|
|
143
|
+
authenticatorAttachment: credential.authenticatorAttachment ?? null,
|
|
144
|
+
clientExtensionResults: credential.getClientExtensionResults(),
|
|
145
|
+
response: body,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function fromBase64Url(encoded: string): ArrayBuffer {
|
|
150
|
+
const binary = atob(encoded.replace(/-/g, "+").replace(/_/g, "/"));
|
|
151
|
+
const bytes = new Uint8Array(binary.length);
|
|
152
|
+
for (let i = 0; i < binary.length; i++) {
|
|
153
|
+
bytes[i] = binary.charCodeAt(i);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return bytes.buffer;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function toBase64Url(buffer: ArrayBuffer): string {
|
|
160
|
+
let binary = "";
|
|
161
|
+
for (const byte of new Uint8Array(buffer)) {
|
|
162
|
+
binary += String.fromCharCode(byte);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function describe(route: string, error: unknown): string {
|
|
169
|
+
const problem = error as { error?: string; detail?: string; message?: string } | undefined;
|
|
170
|
+
|
|
171
|
+
return `${route} failed: ${problem?.detail ?? problem?.message ?? problem?.error ?? "unknown error"}`;
|
|
172
|
+
}
|