-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathindex.ts
195 lines (175 loc) · 6.28 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { Platform } from 'react-native';
import type {
AuthenticationResponseJSON,
CredentialReturn,
PublicKeyCredentialCreationOptionsWithoutExtensions,
PublicKeyCredentialRequestOptionsWithoutExtensions,
PublicKeyCredentialWithAuthenticatorAssertionResponse,
PublicKeyCredentialWithAuthenticatorAttestationResponse,
RegistrationResponseJSON,
SerializedPublicKeyCredentialCreationOptions,
SerializedPublicKeyCredentialRequestOptions,
} from './ClerkExpoPasskeys.types';
import ClerkExpoPasskeys from './ClerkExpoPasskeysModule';
import {
arrayBufferToBase64Url,
base64urlToArrayBuffer,
ClerkWebAuthnError,
encodeBase64Url,
mapNativeErrorToClerkWebAuthnErrorCode,
toArrayBuffer,
} from './utils';
const makeSerializedCreateResponse = (
publicCredential: RegistrationResponseJSON,
): PublicKeyCredentialWithAuthenticatorAttestationResponse => ({
id: publicCredential.id,
rawId: base64urlToArrayBuffer(publicCredential.rawId),
response: {
getTransports: () => publicCredential?.response?.transports as string[],
attestationObject: base64urlToArrayBuffer(publicCredential.response.attestationObject),
clientDataJSON: base64urlToArrayBuffer(publicCredential.response.clientDataJSON),
},
type: publicCredential.type,
authenticatorAttachment: publicCredential.authenticatorAttachment || null,
toJSON: () => publicCredential,
});
export async function create(
publicKey: PublicKeyCredentialCreationOptionsWithoutExtensions,
): Promise<CredentialReturn<PublicKeyCredentialWithAuthenticatorAttestationResponse>> {
if (!publicKey || !publicKey.rp.id) {
throw new Error('Invalid public key or RpID');
}
const createOptions: SerializedPublicKeyCredentialCreationOptions = {
rp: { id: publicKey.rp.id, name: publicKey.rp.name },
user: {
id: encodeBase64Url(toArrayBuffer(publicKey.user.id)),
displayName: publicKey.user.displayName,
name: publicKey.user.name,
},
pubKeyCredParams: publicKey.pubKeyCredParams,
challenge: encodeBase64Url(toArrayBuffer(publicKey.challenge)),
authenticatorSelection: {
authenticatorAttachment: 'platform',
requireResidentKey: true,
residentKey: 'required',
userVerification: 'required',
},
excludeCredentials: publicKey.excludeCredentials.map(c => ({
type: 'public-key',
id: encodeBase64Url(toArrayBuffer(c.id)),
})),
};
const createPasskeyModule = Platform.select({
android: async () => ClerkExpoPasskeys.create(JSON.stringify(createOptions)),
ios: async () =>
ClerkExpoPasskeys.create(
createOptions.challenge,
createOptions.rp.id,
createOptions.user.id,
createOptions.user.displayName,
),
default: null,
});
if (!createPasskeyModule) {
throw new Error('Platform not supported');
}
try {
const response = await createPasskeyModule();
return {
publicKeyCredential: makeSerializedCreateResponse(typeof response === 'string' ? JSON.parse(response) : response),
error: null,
};
} catch (error: any) {
return {
publicKeyCredential: null,
error: mapNativeErrorToClerkWebAuthnErrorCode(error.code, error.message, 'create'),
};
}
}
const makeSerializedGetResponse = (
publicKeyCredential: AuthenticationResponseJSON,
): PublicKeyCredentialWithAuthenticatorAssertionResponse => {
return {
type: publicKeyCredential.type,
id: publicKeyCredential.id,
rawId: base64urlToArrayBuffer(publicKeyCredential.rawId),
authenticatorAttachment: publicKeyCredential?.authenticatorAttachment || null,
response: {
clientDataJSON: base64urlToArrayBuffer(publicKeyCredential.response.clientDataJSON),
authenticatorData: base64urlToArrayBuffer(publicKeyCredential.response.authenticatorData),
signature: base64urlToArrayBuffer(publicKeyCredential.response.signature),
userHandle: publicKeyCredential?.response.userHandle
? base64urlToArrayBuffer(publicKeyCredential?.response.userHandle)
: null,
},
toJSON: () => publicKeyCredential,
};
};
export async function get({
publicKeyOptions,
}: {
publicKeyOptions: PublicKeyCredentialRequestOptionsWithoutExtensions;
}): Promise<CredentialReturn<PublicKeyCredentialWithAuthenticatorAssertionResponse>> {
if (!publicKeyOptions) {
throw new Error('publicKeyCredential has not been provided');
}
const serializedPublicCredential: SerializedPublicKeyCredentialRequestOptions = {
...publicKeyOptions,
// @ts-expect-error FIXME
challenge: arrayBufferToBase64Url(publicKeyOptions.challenge),
};
const getPasskeyModule = Platform.select({
android: async () => ClerkExpoPasskeys.get(JSON.stringify(serializedPublicCredential)),
ios: async () => ClerkExpoPasskeys.get(serializedPublicCredential.challenge, serializedPublicCredential.rpId),
default: null,
});
if (!getPasskeyModule) {
return {
publicKeyCredential: null,
error: new ClerkWebAuthnError('Platform is not supported', { code: 'passkey_not_supported' }),
};
}
try {
const response = await getPasskeyModule();
return {
publicKeyCredential: makeSerializedGetResponse(typeof response === 'string' ? JSON.parse(response) : response),
error: null,
};
} catch (error: any) {
return {
publicKeyCredential: null,
error: mapNativeErrorToClerkWebAuthnErrorCode(error.code, error.message, 'get'),
};
}
}
const ANDROID_9 = 28;
const IOS_15 = 15;
export function isSupported() {
if (Platform.OS === 'android') {
return Platform.Version >= ANDROID_9;
}
if (Platform.OS === 'ios') {
return parseInt(Platform.Version, 10) > IOS_15;
}
return false;
}
// FIX:The autofill function has been implemented for iOS only, but the pop-up is not showing up.
// This seems to be an issue with Expo that we haven't been able to resolve yet.
// Further investigation and possibly reaching out to Expo support may be necessary.
// async function autofill(): Promise<AuthenticationResponseJSON | null> {
// if (Platform.OS === 'android') {
// throw new Error('Not supported');
// } else if (Platform.OS === 'ios') {
// throw new Error('Not supported');
// } else {
// throw new Error('Not supported');
// }
// }
export const passkeys = {
create,
get,
isSupported,
isAutoFillSupported: () => {
throw new Error('Not supported');
},
};