@plainkey/browser 0.17.0 → 0.20.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/dist/index.d.ts +73 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +87 -50
- package/dist/index.js.map +1 -1
- package/package.json +1 -2
- package/readme.md +7 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,58 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AuthenticationResponseJSON, PublicKeyCredentialCreationOptionsJSON, PublicKeyCredentialCreationOptionsJSON as PublicKeyCredentialCreationOptionsJSON$1, PublicKeyCredentialRequestOptionsJSON, RegistrationResponseJSON } from "@simplewebauthn/browser";
|
|
2
2
|
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type UserIdentifier = {
|
|
5
|
+
userId?: string;
|
|
6
|
+
userName?: string;
|
|
7
|
+
};
|
|
8
|
+
type AuthenticationToken = {
|
|
9
|
+
token: string;
|
|
10
|
+
expiresAt: number;
|
|
11
|
+
};
|
|
12
|
+
type CredentialBasicInfo = {
|
|
13
|
+
id: string;
|
|
14
|
+
label: string | null;
|
|
15
|
+
authenticatorType: string | null;
|
|
16
|
+
userId: string;
|
|
17
|
+
};
|
|
18
|
+
interface AuthenticateResult {
|
|
19
|
+
success: boolean;
|
|
20
|
+
data?: {
|
|
21
|
+
authenticationToken: AuthenticationToken;
|
|
22
|
+
};
|
|
23
|
+
error?: {
|
|
24
|
+
message: string;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
interface CreateUserWithPasskeyResult {
|
|
28
|
+
success: boolean;
|
|
29
|
+
data?: {
|
|
30
|
+
userId: string;
|
|
31
|
+
credential: CredentialBasicInfo;
|
|
32
|
+
authenticationToken: AuthenticationToken;
|
|
33
|
+
};
|
|
34
|
+
error?: {
|
|
35
|
+
message: string;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
interface AddPasskeyResult {
|
|
39
|
+
success: boolean;
|
|
40
|
+
data?: {
|
|
41
|
+
credential: CredentialBasicInfo;
|
|
42
|
+
authenticationToken: AuthenticationToken;
|
|
43
|
+
};
|
|
44
|
+
error?: {
|
|
45
|
+
message: string;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
interface UpdatePasskeyLabelResult {
|
|
49
|
+
success: boolean;
|
|
50
|
+
error?: {
|
|
51
|
+
message: string;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
3
55
|
//#region src/plainKey.d.ts
|
|
4
|
-
|
|
5
56
|
/**
|
|
6
57
|
* PlainKey client for the browser. Used to register new users, add passkeys to existing users, and log users in.
|
|
7
58
|
*
|
|
@@ -16,7 +67,7 @@ declare class PlainKey {
|
|
|
16
67
|
constructor(projectId: string, baseUrl?: string);
|
|
17
68
|
/**
|
|
18
69
|
* Helper to parse response JSON.
|
|
19
|
-
* Throws error if status code is not 200 OK
|
|
70
|
+
* Throws error if status code is not 200 OK or if the response is not valid JSON.
|
|
20
71
|
*/
|
|
21
72
|
private parseResponse;
|
|
22
73
|
/**
|
|
@@ -29,17 +80,27 @@ declare class PlainKey {
|
|
|
29
80
|
/**
|
|
30
81
|
* Adds a passkey to an existing user. Will require user interaction to create a passkey.
|
|
31
82
|
*
|
|
32
|
-
* @param authenticationToken - The user authentication token,
|
|
83
|
+
* @param authenticationToken - The user authentication token, returned from .authenticate() or createUserWithPasskey().
|
|
33
84
|
* Do NOT store it in local storage, database, etc. Always keep it in memory.
|
|
34
|
-
* @param userName - A unique identifier for the user,
|
|
35
|
-
* If not provided, the user's stored userName will be used.
|
|
85
|
+
* @param userName - A unique identifier for the user. If not provided, the user's stored userName will be used.
|
|
36
86
|
*/
|
|
37
87
|
addPasskey(authenticationToken: string, userName?: string): Promise<AddPasskeyResult>;
|
|
38
88
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
89
|
+
* Completes a server-initiated passkey registration. Use this when your backend has already called
|
|
90
|
+
* beginCredentialRegistration() via the Server SDK (or the associated endpoint via REST API), and passed the options and
|
|
91
|
+
* authenticationToken to the frontend.
|
|
92
|
+
*
|
|
93
|
+
* @param authenticationToken - The short-lived token returned alongside the options by beginCredentialRegistration().
|
|
94
|
+
* @param options - The WebAuthn creation options returned by the server's beginCredentialRegistration().
|
|
95
|
+
* Do NOT store it in local storage, database, etc. Always keep it in memory.
|
|
96
|
+
*/
|
|
97
|
+
completePasskeyRegistration(authenticationToken: string, options: PublicKeyCredentialCreationOptionsJSON$1): Promise<AddPasskeyResult>;
|
|
98
|
+
/**
|
|
99
|
+
* Updates a passkey label. Any passkey registered to the user can be updated.
|
|
100
|
+
*
|
|
101
|
+
* @param authenticationToken - The user authentication token, returned from .authenticate() or createUserWithPasskey().
|
|
41
102
|
* Do NOT store it in local storage, database, etc. Always keep it in memory.
|
|
42
|
-
* @param credentialId - The ID of the passkey
|
|
103
|
+
* @param credentialId - The ID of the passkey to update, returned from createUserWithPasskey() or addPasskey().
|
|
43
104
|
* @param label - The new label for the passkey.
|
|
44
105
|
*/
|
|
45
106
|
updatePasskeyLabel(authenticationToken: string, credentialId: string, label: string): Promise<UpdatePasskeyLabelResult>;
|
|
@@ -47,11 +108,11 @@ declare class PlainKey {
|
|
|
47
108
|
* Authenticates a user. Can be used for login, verification, 2FA, etc.
|
|
48
109
|
* Will require user interaction to authenticate.
|
|
49
110
|
*
|
|
50
|
-
* @param userIdentifier - Optional
|
|
51
|
-
*
|
|
111
|
+
* @param userIdentifier - Optional. Identify the user by their PlainKey user ID or userName.
|
|
112
|
+
* Not required for usernameless authentication.
|
|
52
113
|
*/
|
|
53
114
|
authenticate(userIdentifier?: UserIdentifier): Promise<AuthenticateResult>;
|
|
54
115
|
}
|
|
55
116
|
//#endregion
|
|
56
|
-
export { PlainKey };
|
|
117
|
+
export { AddPasskeyResult, AuthenticateResult, type AuthenticationResponseJSON, AuthenticationToken, CreateUserWithPasskeyResult, CredentialBasicInfo, PlainKey, type PublicKeyCredentialCreationOptionsJSON, type PublicKeyCredentialRequestOptionsJSON, type RegistrationResponseJSON, UpdatePasskeyLabelResult, UserIdentifier };
|
|
57
118
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/plainKey.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/types.ts","../src/plainKey.ts"],"sourcesContent":[],"mappings":";;;KAAY,cAAA;;;AAAZ,CAAA;AAKY,KAAA,mBAAA,GAAmB;EAKnB,KAAA,EAAA,MAAA;EAOK,SAAA,EAAA,MAAA;AAQjB,CAAA;AAUiB,KAzBL,mBAAA,GA4BI;EAMC,EAAA,EAAA,MAAA;;;;ACXjB,CAAA;AAiD0D,UDjEzC,kBAAA,CCiEyC;EAAR,OAAA,EAAA,OAAA;EAwD0B,IAAA,CAAA,EAAA;IAAR,mBAAA,EDtH3C,mBCsH2C;EA8DvD,CAAA;EACA,KAAA,CAAA,EAAA;IAAR,OAAA,EAAA,MAAA;EAkDQ,CAAA;;AAiCyB,UDnQrB,2BAAA,CCmQqB;EAAyB,OAAA,EAAA,OAAA;EAAR,IAAA,CAAA,EAAA;IAAO,MAAA,EAAA,MAAA;gBD/P9C;yBACS;;;;;;UAKR,gBAAA;;;gBAGD;yBACS;;;;;;UAKR,wBAAA;;;;;;;;;AA5CjB;AAKA;AAKA;AAOA;AAQA;AAUA;AASA;cCXa,QAAA;;;EAAA,WAAQ,CAAA,SAAA,EAAA,MAAA,EAAA,OAAA,CAAA,EAAA,MAAA;EAiDqC;;;;EAsH7C,QAAA,aAAA;EACA;;;;;;EAmF0C,qBAAA,CAAA,QAAA,CAAA,EAAA,MAAA,CAAA,EA1ML,OA0MK,CA1MG,2BA0MH,CAAA;EAAO;;;;;;;8DAlJM,QAAQ;;;;;;;;;;oEA8D/D,2CACR,QAAQ;;;;;;;;;wFAkDR,QAAQ;;;;;;;;gCAiCyB,iBAAiB,QAAQ"}
|
package/dist/index.js
CHANGED
|
@@ -18,7 +18,7 @@ var PlainKey = class {
|
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
20
|
* Helper to parse response JSON.
|
|
21
|
-
* Throws error if status code is not 200 OK
|
|
21
|
+
* Throws error if status code is not 200 OK or if the response is not valid JSON.
|
|
22
22
|
*/
|
|
23
23
|
async parseResponse(response) {
|
|
24
24
|
let bodyText;
|
|
@@ -48,36 +48,35 @@ var PlainKey = class {
|
|
|
48
48
|
*/
|
|
49
49
|
async createUserWithPasskey(userName) {
|
|
50
50
|
try {
|
|
51
|
-
const beginRequestBody = { userName };
|
|
52
51
|
const beginResponse = await fetch(`${this.baseUrl}/user/register/begin`, {
|
|
53
52
|
method: "POST",
|
|
54
53
|
headers: {
|
|
55
54
|
"Content-Type": "application/json",
|
|
56
55
|
"x-project-id": this.projectId
|
|
57
56
|
},
|
|
58
|
-
body: JSON.stringify(
|
|
57
|
+
body: JSON.stringify({ userName })
|
|
59
58
|
});
|
|
60
59
|
const { userId, options } = await this.parseResponse(beginResponse);
|
|
61
|
-
const
|
|
62
|
-
userId,
|
|
63
|
-
credential: await startRegistration({ optionsJSON: options })
|
|
64
|
-
};
|
|
60
|
+
const credential = await startRegistration({ optionsJSON: options });
|
|
65
61
|
const completeResponse = await fetch(`${this.baseUrl}/user/register/complete`, {
|
|
66
62
|
method: "POST",
|
|
67
63
|
headers: {
|
|
68
64
|
"Content-Type": "application/json",
|
|
69
65
|
"x-project-id": this.projectId
|
|
70
66
|
},
|
|
71
|
-
body: JSON.stringify(
|
|
67
|
+
body: JSON.stringify({
|
|
68
|
+
userId,
|
|
69
|
+
credential
|
|
70
|
+
})
|
|
72
71
|
});
|
|
73
|
-
const
|
|
74
|
-
if (!
|
|
72
|
+
const completeData = await this.parseResponse(completeResponse);
|
|
73
|
+
if (!completeData.success) throw new Error("Server could not complete registration");
|
|
75
74
|
return {
|
|
76
|
-
success:
|
|
75
|
+
success: true,
|
|
77
76
|
data: {
|
|
78
|
-
userId:
|
|
79
|
-
authenticationToken:
|
|
80
|
-
credential:
|
|
77
|
+
userId: completeData.userId,
|
|
78
|
+
authenticationToken: completeData.authenticationToken,
|
|
79
|
+
credential: completeData.credential
|
|
81
80
|
}
|
|
82
81
|
};
|
|
83
82
|
} catch (error) {
|
|
@@ -90,46 +89,85 @@ var PlainKey = class {
|
|
|
90
89
|
/**
|
|
91
90
|
* Adds a passkey to an existing user. Will require user interaction to create a passkey.
|
|
92
91
|
*
|
|
93
|
-
* @param authenticationToken - The user authentication token,
|
|
92
|
+
* @param authenticationToken - The user authentication token, returned from .authenticate() or createUserWithPasskey().
|
|
94
93
|
* Do NOT store it in local storage, database, etc. Always keep it in memory.
|
|
95
|
-
* @param userName - A unique identifier for the user,
|
|
96
|
-
* If not provided, the user's stored userName will be used.
|
|
94
|
+
* @param userName - A unique identifier for the user. If not provided, the user's stored userName will be used.
|
|
97
95
|
*/
|
|
98
96
|
async addPasskey(authenticationToken, userName) {
|
|
99
97
|
if (!authenticationToken) throw new Error("Authentication token is required");
|
|
100
98
|
try {
|
|
101
|
-
const beginParams = {
|
|
102
|
-
authenticationToken,
|
|
103
|
-
userName
|
|
104
|
-
};
|
|
105
99
|
const beginResponse = await fetch(`${this.baseUrl}/user/credential/begin`, {
|
|
106
100
|
method: "POST",
|
|
107
101
|
headers: {
|
|
108
102
|
"Content-Type": "application/json",
|
|
109
103
|
"x-project-id": this.projectId
|
|
110
104
|
},
|
|
111
|
-
body: JSON.stringify(
|
|
105
|
+
body: JSON.stringify({
|
|
106
|
+
authenticationToken,
|
|
107
|
+
userName
|
|
108
|
+
})
|
|
112
109
|
});
|
|
113
110
|
const { options } = await this.parseResponse(beginResponse);
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
111
|
+
const credential = await startRegistration({ optionsJSON: options });
|
|
112
|
+
const completeResponse = await fetch(`${this.baseUrl}/user/credential/complete`, {
|
|
113
|
+
method: "POST",
|
|
114
|
+
headers: {
|
|
115
|
+
"Content-Type": "application/json",
|
|
116
|
+
"x-project-id": this.projectId
|
|
117
|
+
},
|
|
118
|
+
body: JSON.stringify({
|
|
119
|
+
authenticationToken,
|
|
120
|
+
credential
|
|
121
|
+
})
|
|
122
|
+
});
|
|
123
|
+
const completeData = await this.parseResponse(completeResponse);
|
|
124
|
+
if (!completeData.success) throw new Error("Server could not complete passkey registration");
|
|
125
|
+
return {
|
|
126
|
+
success: true,
|
|
127
|
+
data: {
|
|
128
|
+
authenticationToken: completeData.authenticationToken,
|
|
129
|
+
credential: completeData.credential
|
|
130
|
+
}
|
|
117
131
|
};
|
|
132
|
+
} catch (error) {
|
|
133
|
+
return {
|
|
134
|
+
success: false,
|
|
135
|
+
error: { message: error instanceof Error ? error.message : "Unknown error" }
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Completes a server-initiated passkey registration. Use this when your backend has already called
|
|
141
|
+
* beginCredentialRegistration() via the Server SDK (or the associated endpoint via REST API), and passed the options and
|
|
142
|
+
* authenticationToken to the frontend.
|
|
143
|
+
*
|
|
144
|
+
* @param authenticationToken - The short-lived token returned alongside the options by beginCredentialRegistration().
|
|
145
|
+
* @param options - The WebAuthn creation options returned by the server's beginCredentialRegistration().
|
|
146
|
+
* Do NOT store it in local storage, database, etc. Always keep it in memory.
|
|
147
|
+
*/
|
|
148
|
+
async completePasskeyRegistration(authenticationToken, options) {
|
|
149
|
+
if (!authenticationToken) throw new Error("Authentication token is required");
|
|
150
|
+
if (!options) throw new Error("Options are required");
|
|
151
|
+
try {
|
|
152
|
+
const credential = await startRegistration({ optionsJSON: options });
|
|
118
153
|
const completeResponse = await fetch(`${this.baseUrl}/user/credential/complete`, {
|
|
119
154
|
method: "POST",
|
|
120
155
|
headers: {
|
|
121
156
|
"Content-Type": "application/json",
|
|
122
157
|
"x-project-id": this.projectId
|
|
123
158
|
},
|
|
124
|
-
body: JSON.stringify(
|
|
159
|
+
body: JSON.stringify({
|
|
160
|
+
authenticationToken,
|
|
161
|
+
credential
|
|
162
|
+
})
|
|
125
163
|
});
|
|
126
|
-
const
|
|
127
|
-
if (!
|
|
164
|
+
const completeData = await this.parseResponse(completeResponse);
|
|
165
|
+
if (!completeData.success) throw new Error("Server could not complete passkey registration");
|
|
128
166
|
return {
|
|
129
|
-
success:
|
|
167
|
+
success: true,
|
|
130
168
|
data: {
|
|
131
|
-
authenticationToken:
|
|
132
|
-
credential:
|
|
169
|
+
authenticationToken: completeData.authenticationToken,
|
|
170
|
+
credential: completeData.credential
|
|
133
171
|
}
|
|
134
172
|
};
|
|
135
173
|
} catch (error) {
|
|
@@ -140,17 +178,18 @@ var PlainKey = class {
|
|
|
140
178
|
}
|
|
141
179
|
}
|
|
142
180
|
/**
|
|
143
|
-
* Updates a passkey label.
|
|
144
|
-
*
|
|
181
|
+
* Updates a passkey label. Any passkey registered to the user can be updated.
|
|
182
|
+
*
|
|
183
|
+
* @param authenticationToken - The user authentication token, returned from .authenticate() or createUserWithPasskey().
|
|
145
184
|
* Do NOT store it in local storage, database, etc. Always keep it in memory.
|
|
146
|
-
* @param credentialId - The ID of the passkey
|
|
185
|
+
* @param credentialId - The ID of the passkey to update, returned from createUserWithPasskey() or addPasskey().
|
|
147
186
|
* @param label - The new label for the passkey.
|
|
148
187
|
*/
|
|
149
188
|
async updatePasskeyLabel(authenticationToken, credentialId, label) {
|
|
150
189
|
if (!authenticationToken) throw new Error("Authentication token is required");
|
|
151
190
|
if (!credentialId) throw new Error("Credential ID is required");
|
|
152
191
|
try {
|
|
153
|
-
const
|
|
192
|
+
const body = {
|
|
154
193
|
authenticationToken,
|
|
155
194
|
label
|
|
156
195
|
};
|
|
@@ -160,7 +199,7 @@ var PlainKey = class {
|
|
|
160
199
|
"Content-Type": "application/json",
|
|
161
200
|
"x-project-id": this.projectId
|
|
162
201
|
},
|
|
163
|
-
body: JSON.stringify(
|
|
202
|
+
body: JSON.stringify(body)
|
|
164
203
|
})).ok) throw new Error("Failed to update passkey label");
|
|
165
204
|
return { success: true };
|
|
166
205
|
} catch (error) {
|
|
@@ -174,39 +213,37 @@ var PlainKey = class {
|
|
|
174
213
|
* Authenticates a user. Can be used for login, verification, 2FA, etc.
|
|
175
214
|
* Will require user interaction to authenticate.
|
|
176
215
|
*
|
|
177
|
-
* @param userIdentifier - Optional
|
|
178
|
-
*
|
|
216
|
+
* @param userIdentifier - Optional. Identify the user by their PlainKey user ID or userName.
|
|
217
|
+
* Not required for usernameless authentication.
|
|
179
218
|
*/
|
|
180
219
|
async authenticate(userIdentifier) {
|
|
181
220
|
try {
|
|
182
|
-
const beginParams = { userIdentifier };
|
|
183
221
|
const beginResponse = await fetch(`${this.baseUrl}/authenticate/begin`, {
|
|
184
222
|
method: "POST",
|
|
185
223
|
headers: {
|
|
186
224
|
"Content-Type": "application/json",
|
|
187
225
|
"x-project-id": this.projectId
|
|
188
226
|
},
|
|
189
|
-
body: JSON.stringify(
|
|
227
|
+
body: JSON.stringify({ userIdentifier })
|
|
190
228
|
});
|
|
191
|
-
const
|
|
192
|
-
if (!
|
|
193
|
-
const authenticationResponse = await startAuthentication({ optionsJSON:
|
|
229
|
+
const beginData = await this.parseResponse(beginResponse);
|
|
230
|
+
if (!beginData.options) throw new Error("Server returned no options in authentication begin response");
|
|
231
|
+
const authenticationResponse = await startAuthentication({ optionsJSON: beginData.options });
|
|
194
232
|
if (!authenticationResponse) throw new Error("No authentication response from browser");
|
|
195
|
-
const
|
|
196
|
-
loginSessionId: beginResponseData.loginSession.id,
|
|
197
|
-
authenticationResponse
|
|
198
|
-
};
|
|
199
|
-
const authenticateCompleteResponse = await fetch(`${this.baseUrl}/authenticate/complete`, {
|
|
233
|
+
const completeResponse = await fetch(`${this.baseUrl}/authenticate/complete`, {
|
|
200
234
|
method: "POST",
|
|
201
235
|
headers: {
|
|
202
236
|
"Content-Type": "application/json",
|
|
203
237
|
"x-project-id": this.projectId
|
|
204
238
|
},
|
|
205
|
-
body: JSON.stringify(
|
|
239
|
+
body: JSON.stringify({
|
|
240
|
+
loginSessionId: beginData.loginSession.id,
|
|
241
|
+
authenticationResponse
|
|
242
|
+
})
|
|
206
243
|
});
|
|
207
244
|
return {
|
|
208
245
|
success: true,
|
|
209
|
-
data: { authenticationToken: (await this.parseResponse(
|
|
246
|
+
data: { authenticationToken: (await this.parseResponse(completeResponse)).authenticationToken }
|
|
210
247
|
};
|
|
211
248
|
} catch (error) {
|
|
212
249
|
return {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["bodyText: string","json: any","beginRequestBody: UserRegisterBeginRequest","completeRequestBody: UserRegisterCompleteRequest","completeResponseData: UserRegisterCompleteResponse","beginParams: UserCredentialBeginRequest","completeParams: UserCredentialCompleteRequest","completeResponseData: UserCredentialCompleteResponse","updateLabelParams: CredentialLabelUpdateRequest","beginParams: LoginBeginRequest","beginResponseData: AuthenticationBeginResponse","authenticationResponse: AuthenticationResponseJSON","completeParams: LoginCompleteRequest"],"sources":["../src/plainKey.ts"],"sourcesContent":["import { startAuthentication, startRegistration } from \"@simplewebauthn/browser\"\nimport { RegistrationResponseJSON, AuthenticationResponseJSON } from \"@simplewebauthn/browser\"\n\nimport type {\n UserCredentialBeginRequest,\n UserCredentialCompleteRequest,\n LoginBeginRequest,\n LoginCompleteRequest,\n UserIdentifier,\n CreateUserWithPasskeyResult,\n AddPasskeyResult,\n AuthenticateResult,\n UserRegisterBeginRequest,\n UserRegisterBeginResponse,\n UserRegisterCompleteRequest,\n UserRegisterCompleteResponse,\n AuthenticationCompleteResponse,\n AuthenticationBeginResponse,\n CredentialLabelUpdateRequest,\n UpdatePasskeyLabelResult\n} from \"@plainkey/types\"\n\nimport type { UserCredentialBeginResponse, UserCredentialCompleteResponse } from \"@plainkey/types\"\n\n/**\n * PlainKey client for the browser. Used to register new users, add passkeys to existing users, and log users in.\n *\n * Docs: https://plainkey.io/docs\n *\n * @param projectId - Your PlainKey project ID. You can find it in the PlainKey admin dashboard.\n * @param baseUrl - Set by default to https://api.plainkey.io/browser. Change only for development purposes.\n */\nexport class PlainKey {\n private readonly projectId: string\n private readonly baseUrl: string\n\n constructor(projectId: string, baseUrl: string = \"https://api.plainkey.io/browser\") {\n if (!projectId) throw new Error(\"Project ID is required\")\n if (!baseUrl) throw new Error(\"Base URL is required\")\n\n this.projectId = projectId\n this.baseUrl = baseUrl.replace(/\\/$/, \"\") // Remove trailing slash\n }\n\n /**\n * Helper to parse response JSON.\n * Throws error if status code is not 200 OK, if the response is not valid JSON.\n */\n private async parseResponse<T = any>(response: Response): Promise<T> {\n let bodyText: string\n\n // Read as text first to avoid JSON.parse errors on any HTML/plaintext error responses.\n try {\n bodyText = await response.text()\n } catch {\n throw new Error(\"Network error while reading server response\")\n }\n\n // Parse the response text as JSON.\n let json: any\n\n try {\n json = bodyText ? JSON.parse(bodyText) : {}\n } catch {\n if (!response.ok) throw new Error(\"Server returned an invalid JSON error response\")\n throw new Error(\"Invalid JSON received from server\")\n }\n\n if (!response.ok) {\n // Server should return { error: string }\n const message = json && typeof json.error === \"string\" ? json.error : \"Unknown server error\"\n throw new Error(message)\n }\n\n return json as T\n }\n\n /**\n * Registration of a new user with a passkey. Will require user interaction to create a passkey.\n *\n * @param userName - A unique identifier for the user, like an email address or username.\n * Can be empty for usernameless authentication.\n */\n async createUserWithPasskey(userName?: string): Promise<CreateUserWithPasskeyResult> {\n try {\n // Step 1: Get registration options from server\n const beginRequestBody: UserRegisterBeginRequest = { userName }\n const beginResponse = await fetch(`${this.baseUrl}/user/register/begin`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify(beginRequestBody)\n })\n\n // Parse response JSON\n const { userId, options } = await this.parseResponse<UserRegisterBeginResponse>(beginResponse)\n\n // Step 2: Create credential using browser's WebAuthn API\n const credential: RegistrationResponseJSON = await startRegistration({\n optionsJSON: options\n })\n\n // Step 3: Send credential to server for verification\n const completeRequestBody: UserRegisterCompleteRequest = { userId, credential }\n const completeResponse = await fetch(`${this.baseUrl}/user/register/complete`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify(completeRequestBody)\n })\n\n // Parse response JSON\n const completeResponseData: UserRegisterCompleteResponse =\n await this.parseResponse<UserRegisterCompleteResponse>(completeResponse)\n\n if (!completeResponseData.success) throw new Error(\"Server could not complete registration\")\n\n // Return success\n return {\n success: completeResponseData.success,\n data: {\n userId: completeResponseData.userId,\n authenticationToken: completeResponseData.authenticationToken,\n credential: completeResponseData.credential\n }\n }\n } catch (error) {\n // Return error\n return {\n success: false,\n error: {\n message: error instanceof Error ? error.message : \"Unknown error\"\n }\n }\n }\n }\n\n /**\n * Adds a passkey to an existing user. Will require user interaction to create a passkey.\n *\n * @param authenticationToken - The user authentication token, is returned from .authenticate() and createUserWithPasskey().\n * Do NOT store it in local storage, database, etc. Always keep it in memory.\n * @param userName - A unique identifier for the user, like an email address or username.\n * If not provided, the user's stored userName will be used.\n */\n async addPasskey(authenticationToken: string, userName?: string): Promise<AddPasskeyResult> {\n if (!authenticationToken) throw new Error(\"Authentication token is required\")\n\n try {\n // Step 1: Get credential registration options from server\n const beginParams: UserCredentialBeginRequest = { authenticationToken, userName }\n const beginResponse = await fetch(`${this.baseUrl}/user/credential/begin`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify(beginParams)\n })\n\n // Parse response JSON\n const { options }: UserCredentialBeginResponse =\n await this.parseResponse<UserCredentialBeginResponse>(beginResponse)\n\n // Step 2: Create credential using browser's WebAuthn API\n const credential: RegistrationResponseJSON = await startRegistration({ optionsJSON: options })\n\n // Step 3: Send credential to server for verification\n const completeParams: UserCredentialCompleteRequest = { authenticationToken, credential }\n\n const completeResponse = await fetch(`${this.baseUrl}/user/credential/complete`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify(completeParams)\n })\n\n // Parse response JSON\n const completeResponseData: UserCredentialCompleteResponse =\n await this.parseResponse<UserCredentialCompleteResponse>(completeResponse)\n\n if (!completeResponseData.success)\n throw new Error(\"Server could not complete passkey registration\")\n\n // Return success\n return {\n success: completeResponseData.success,\n data: {\n authenticationToken: completeResponseData.authenticationToken,\n credential: completeResponseData.credential\n }\n }\n } catch (error) {\n // Return error\n return {\n success: false,\n error: {\n message: error instanceof Error ? error.message : \"Unknown error\"\n }\n }\n }\n }\n\n /**\n * Updates a passkey label. Requires authentication shortly before this call. Any passkey registered to the user can be updated.\n * @param authenticationToken - The user authentication token, is returned from .authenticate() and createUserWithPasskey().\n * Do NOT store it in local storage, database, etc. Always keep it in memory.\n * @param credentialId - The ID of the passkey credential to update. Is returned from createUserWithPasskey() and addPasskey().\n * @param label - The new label for the passkey.\n */\n async updatePasskeyLabel(\n authenticationToken: string,\n credentialId: string,\n label: string\n ): Promise<UpdatePasskeyLabelResult> {\n if (!authenticationToken) throw new Error(\"Authentication token is required\")\n if (!credentialId) throw new Error(\"Credential ID is required\")\n // Empty label is allowed\n\n try {\n const updateLabelParams: CredentialLabelUpdateRequest = { authenticationToken, label }\n const updateLabelResponse = await fetch(`${this.baseUrl}/credential/${credentialId}/label`, {\n method: \"PATCH\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify(updateLabelParams)\n })\n\n if (!updateLabelResponse.ok) throw new Error(\"Failed to update passkey label\")\n\n // Return success\n return { success: true }\n } catch (error) {\n // Return error\n return {\n success: false,\n error: { message: error instanceof Error ? error.message : \"Unknown error\" }\n }\n }\n }\n\n /**\n * Authenticates a user. Can be used for login, verification, 2FA, etc.\n * Will require user interaction to authenticate.\n *\n * @param userIdentifier - Optional object containing either the user's PlainKey User ID or their userName.\n * Does not have to be provided for usernameless authentication.\n */\n async authenticate(userIdentifier?: UserIdentifier): Promise<AuthenticateResult> {\n try {\n // Step 1: Get authentication options from server\n const beginParams: LoginBeginRequest = { userIdentifier }\n const beginResponse = await fetch(`${this.baseUrl}/authenticate/begin`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify(beginParams)\n })\n\n // Parse response JSON\n const beginResponseData: AuthenticationBeginResponse =\n await this.parseResponse<AuthenticationBeginResponse>(beginResponse)\n\n if (!beginResponseData.options)\n throw new Error(\"Server returned no options in login begin response\")\n\n // Step 2: Pass options to the authenticator and wait for response\n const authenticationResponse: AuthenticationResponseJSON = await startAuthentication({\n optionsJSON: beginResponseData.options\n })\n\n if (!authenticationResponse) throw new Error(\"No authentication response from browser\")\n\n // Step 3: POST the response to the server\n // This uses the authentication session ID from the begin response - always in JS memory.\n // Do not store it in local storage, database, etc.\n const completeParams: LoginCompleteRequest = {\n loginSessionId: beginResponseData.loginSession.id,\n authenticationResponse\n }\n\n const authenticateCompleteResponse = await fetch(`${this.baseUrl}/authenticate/complete`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify(completeParams)\n })\n\n const authCompleteResponseData: AuthenticationCompleteResponse =\n await this.parseResponse<AuthenticationCompleteResponse>(authenticateCompleteResponse)\n\n // Return success\n return {\n success: true,\n data: {\n authenticationToken: authCompleteResponseData.authenticationToken\n }\n }\n } catch (error) {\n // Return error\n return {\n success: false,\n error: {\n message: error instanceof Error ? error.message : \"Unknown error\"\n }\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;AAgCA,IAAa,WAAb,MAAsB;CAIpB,YAAY,WAAmB,UAAkB,mCAAmC;AAClF,MAAI,CAAC,UAAW,OAAM,IAAI,MAAM,yBAAyB;AACzD,MAAI,CAAC,QAAS,OAAM,IAAI,MAAM,uBAAuB;AAErD,OAAK,YAAY;AACjB,OAAK,UAAU,QAAQ,QAAQ,OAAO,GA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,iBACnD;IACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["bodyText: string","json: any","body: UpdateCredentialLabelBody"],"sources":["../src/plainKey.ts"],"sourcesContent":["import {\n startAuthentication,\n startRegistration,\n type PublicKeyCredentialCreationOptionsJSON,\n type PublicKeyCredentialRequestOptionsJSON\n} from \"@simplewebauthn/browser\"\n\nimport type {\n AuthenticateResult,\n AddPasskeyResult,\n CreateUserWithPasskeyResult,\n UpdatePasskeyLabelResult,\n UserIdentifier\n} from \"./types\"\n\nimport type {\n BeginUserRegistration200,\n CompleteUserRegistration200,\n BeginCredentialRegistration200,\n CompleteCredentialRegistration200,\n BeginAuthentication200,\n CompleteAuthentication200,\n UpdateCredentialLabelBody\n} from \"./generated/api\"\n\n/**\n * PlainKey client for the browser. Used to register new users, add passkeys to existing users, and log users in.\n *\n * Docs: https://plainkey.io/docs\n *\n * @param projectId - Your PlainKey project ID. You can find it in the PlainKey admin dashboard.\n * @param baseUrl - Set by default to https://api.plainkey.io/browser. Change only for development purposes.\n */\nexport class PlainKey {\n private readonly projectId: string\n private readonly baseUrl: string\n\n constructor(projectId: string, baseUrl: string = \"https://api.plainkey.io/browser\") {\n if (!projectId) throw new Error(\"Project ID is required\")\n if (!baseUrl) throw new Error(\"Base URL is required\")\n\n this.projectId = projectId\n this.baseUrl = baseUrl.replace(/\\/$/, \"\") // Remove trailing slash\n }\n\n /**\n * Helper to parse response JSON.\n * Throws error if status code is not 200 OK or if the response is not valid JSON.\n */\n private async parseResponse<T = any>(response: Response): Promise<T> {\n let bodyText: string\n\n // Read as text first to avoid JSON.parse errors on any HTML/plaintext error responses.\n try {\n bodyText = await response.text()\n } catch {\n throw new Error(\"Network error while reading server response\")\n }\n\n let json: any\n\n try {\n json = bodyText ? JSON.parse(bodyText) : {}\n } catch {\n if (!response.ok) throw new Error(\"Server returned an invalid JSON error response\")\n throw new Error(\"Invalid JSON received from server\")\n }\n\n if (!response.ok) {\n const message = json && typeof json.error === \"string\" ? json.error : \"Unknown server error\"\n throw new Error(message)\n }\n\n return json as T\n }\n\n /**\n * Registration of a new user with a passkey. Will require user interaction to create a passkey.\n *\n * @param userName - A unique identifier for the user, like an email address or username.\n * Can be empty for usernameless authentication.\n */\n async createUserWithPasskey(userName?: string): Promise<CreateUserWithPasskeyResult> {\n try {\n // Step 1: Get registration options from server\n const beginResponse = await fetch(`${this.baseUrl}/user/register/begin`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify({ userName })\n })\n\n const { userId, options } = await this.parseResponse<BeginUserRegistration200>(beginResponse)\n\n // Step 2: Create credential using browser's WebAuthn API\n const credential = await startRegistration({\n optionsJSON: options as unknown as PublicKeyCredentialCreationOptionsJSON\n })\n\n // Step 3: Send credential to server for verification\n const completeResponse = await fetch(`${this.baseUrl}/user/register/complete`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify({ userId, credential })\n })\n\n const completeData = await this.parseResponse<CompleteUserRegistration200>(completeResponse)\n\n if (!completeData.success) throw new Error(\"Server could not complete registration\")\n\n return {\n success: true,\n data: {\n userId: completeData.userId,\n authenticationToken: completeData.authenticationToken,\n credential: completeData.credential\n }\n }\n } catch (error) {\n return {\n success: false,\n error: { message: error instanceof Error ? error.message : \"Unknown error\" }\n }\n }\n }\n\n /**\n * Adds a passkey to an existing user. Will require user interaction to create a passkey.\n *\n * @param authenticationToken - The user authentication token, returned from .authenticate() or createUserWithPasskey().\n * Do NOT store it in local storage, database, etc. Always keep it in memory.\n * @param userName - A unique identifier for the user. If not provided, the user's stored userName will be used.\n */\n async addPasskey(authenticationToken: string, userName?: string): Promise<AddPasskeyResult> {\n if (!authenticationToken) throw new Error(\"Authentication token is required\")\n\n try {\n // Step 1: Get credential registration options from server\n const beginResponse = await fetch(`${this.baseUrl}/user/credential/begin`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify({ authenticationToken, userName })\n })\n\n const { options } = await this.parseResponse<BeginCredentialRegistration200>(beginResponse)\n\n // Step 2: Create credential using browser's WebAuthn API\n const credential = await startRegistration({\n optionsJSON: options as unknown as PublicKeyCredentialCreationOptionsJSON\n })\n\n // Step 3: Send credential to server for verification\n const completeResponse = await fetch(`${this.baseUrl}/user/credential/complete`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify({ authenticationToken, credential })\n })\n\n const completeData =\n await this.parseResponse<CompleteCredentialRegistration200>(completeResponse)\n\n if (!completeData.success) throw new Error(\"Server could not complete passkey registration\")\n\n return {\n success: true,\n data: {\n authenticationToken: completeData.authenticationToken,\n credential: completeData.credential\n }\n }\n } catch (error) {\n return {\n success: false,\n error: { message: error instanceof Error ? error.message : \"Unknown error\" }\n }\n }\n }\n\n /**\n * Completes a server-initiated passkey registration. Use this when your backend has already called\n * beginCredentialRegistration() via the Server SDK (or the associated endpoint via REST API), and passed the options and\n * authenticationToken to the frontend.\n *\n * @param authenticationToken - The short-lived token returned alongside the options by beginCredentialRegistration().\n * @param options - The WebAuthn creation options returned by the server's beginCredentialRegistration().\n * Do NOT store it in local storage, database, etc. Always keep it in memory.\n */\n async completePasskeyRegistration(\n authenticationToken: string,\n options: PublicKeyCredentialCreationOptionsJSON\n ): Promise<AddPasskeyResult> {\n if (!authenticationToken) throw new Error(\"Authentication token is required\")\n if (!options) throw new Error(\"Options are required\")\n\n try {\n // Step 1: Complete the WebAuthn ceremony using the server-provided options\n const credential = await startRegistration({ optionsJSON: options })\n\n // Step 2: Send credential to server for verification\n const completeResponse = await fetch(`${this.baseUrl}/user/credential/complete`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify({ authenticationToken, credential })\n })\n\n const completeData =\n await this.parseResponse<CompleteCredentialRegistration200>(completeResponse)\n\n if (!completeData.success) throw new Error(\"Server could not complete passkey registration\")\n\n return {\n success: true,\n data: {\n authenticationToken: completeData.authenticationToken,\n credential: completeData.credential\n }\n }\n } catch (error) {\n return {\n success: false,\n error: { message: error instanceof Error ? error.message : \"Unknown error\" }\n }\n }\n }\n\n /**\n * Updates a passkey label. Any passkey registered to the user can be updated.\n *\n * @param authenticationToken - The user authentication token, returned from .authenticate() or createUserWithPasskey().\n * Do NOT store it in local storage, database, etc. Always keep it in memory.\n * @param credentialId - The ID of the passkey to update, returned from createUserWithPasskey() or addPasskey().\n * @param label - The new label for the passkey.\n */\n async updatePasskeyLabel(\n authenticationToken: string,\n credentialId: string,\n label: string\n ): Promise<UpdatePasskeyLabelResult> {\n if (!authenticationToken) throw new Error(\"Authentication token is required\")\n if (!credentialId) throw new Error(\"Credential ID is required\")\n\n try {\n const body: UpdateCredentialLabelBody = { authenticationToken, label }\n const response = await fetch(`${this.baseUrl}/credential/${credentialId}/label`, {\n method: \"PATCH\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify(body)\n })\n\n if (!response.ok) throw new Error(\"Failed to update passkey label\")\n\n return { success: true }\n } catch (error) {\n return {\n success: false,\n error: { message: error instanceof Error ? error.message : \"Unknown error\" }\n }\n }\n }\n\n /**\n * Authenticates a user. Can be used for login, verification, 2FA, etc.\n * Will require user interaction to authenticate.\n *\n * @param userIdentifier - Optional. Identify the user by their PlainKey user ID or userName.\n * Not required for usernameless authentication.\n */\n async authenticate(userIdentifier?: UserIdentifier): Promise<AuthenticateResult> {\n try {\n // Step 1: Get authentication options from server\n const beginResponse = await fetch(`${this.baseUrl}/authenticate/begin`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify({ userIdentifier })\n })\n\n const beginData = await this.parseResponse<BeginAuthentication200>(beginResponse)\n\n if (!beginData.options)\n throw new Error(\"Server returned no options in authentication begin response\")\n\n // Step 2: Pass options to the authenticator and wait for response\n const authenticationResponse = await startAuthentication({\n optionsJSON: beginData.options as unknown as PublicKeyCredentialRequestOptionsJSON\n })\n\n if (!authenticationResponse) throw new Error(\"No authentication response from browser\")\n\n // Step 3: POST the response to the server\n const completeResponse = await fetch(`${this.baseUrl}/authenticate/complete`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-project-id\": this.projectId\n },\n body: JSON.stringify({\n loginSessionId: beginData.loginSession.id,\n authenticationResponse\n })\n })\n\n const completeData = await this.parseResponse<CompleteAuthentication200>(completeResponse)\n\n return {\n success: true,\n data: { authenticationToken: completeData.authenticationToken }\n }\n } catch (error) {\n return {\n success: false,\n error: { message: error instanceof Error ? error.message : \"Unknown error\" }\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;AAiCA,IAAa,WAAb,MAAsB;CAIpB,YAAY,WAAmB,UAAkB,mCAAmC;AAClF,MAAI,CAAC,UAAW,OAAM,IAAI,MAAM,yBAAyB;AACzD,MAAI,CAAC,QAAS,OAAM,IAAI,MAAM,uBAAuB;AAErD,OAAK,YAAY;AACjB,OAAK,UAAU,QAAQ,QAAQ,OAAO,GAAG;;;;;;CAO3C,MAAc,cAAuB,UAAgC;EACnE,IAAIA;AAGJ,MAAI;AACF,cAAW,MAAM,SAAS,MAAM;UAC1B;AACN,SAAM,IAAI,MAAM,8CAA8C;;EAGhE,IAAIC;AAEJ,MAAI;AACF,UAAO,WAAW,KAAK,MAAM,SAAS,GAAG,EAAE;UACrC;AACN,OAAI,CAAC,SAAS,GAAI,OAAM,IAAI,MAAM,iDAAiD;AACnF,SAAM,IAAI,MAAM,oCAAoC;;AAGtD,MAAI,CAAC,SAAS,IAAI;GAChB,MAAM,UAAU,QAAQ,OAAO,KAAK,UAAU,WAAW,KAAK,QAAQ;AACtE,SAAM,IAAI,MAAM,QAAQ;;AAG1B,SAAO;;;;;;;;CAST,MAAM,sBAAsB,UAAyD;AACnF,MAAI;GAEF,MAAM,gBAAgB,M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AQ,MAAM,UAAU,iBAAiB;IAC7E"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plainkey/browser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "PlainKey browser SDK for passkey registration, login and credential management",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@plainkey/types": "0.17.0",
|
|
27
26
|
"@simplewebauthn/browser": "^13.2.0"
|
|
28
27
|
},
|
|
29
28
|
"devDependencies": {
|