-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Expand file tree
/
Copy pathpasskey.controller.ts
More file actions
71 lines (58 loc) · 2.97 KB
/
Copy pathpasskey.controller.ts
File metadata and controls
71 lines (58 loc) · 2.97 KB
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
import { passkeyCeremonyStore } from '@api/services/passkey-ceremony.store';
import { waMonitor } from '@api/server.module';
import { Logger } from '@config/logger.config';
export class PasskeyController {
private readonly logger = new Logger('PasskeyController');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public getCeremony(token: string): { status: number; body: any } {
if (!token) return { status: 400, body: { error: 'token is required' } };
const found = passkeyCeremonyStore.lookup(token);
if (!found) return { status: 404, body: { error: 'ceremony not found or expired' } };
const { state } = found;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const body: any = { stage: state.stage, skipHandoffUX: state.skipHandoffUX };
if (state.publicKey) body.publicKey = state.publicKey;
if (state.code) body.code = state.code;
if (state.error) body.error = state.error;
return { status: 200, body };
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async submitResponse(token: string, webAuthnResponse: any): Promise<{ status: number; body: any }> {
if (!token) return { status: 400, body: { error: 'token is required' } };
const instanceId = passkeyCeremonyStore.instanceForToken(token);
if (!instanceId) return { status: 404, body: { error: 'ceremony not found or expired' } };
const instance = this.resolveInstance(instanceId);
if (!instance) return { status: 404, body: { error: 'instance not found' } };
try {
await instance.submitPasskeyResponse(webAuthnResponse);
return { status: 200, body: { ok: true } };
} catch (error) {
this.logger.error(`submitResponse: ${(error as Error).message}`);
return { status: 500, body: { error: (error as Error).message } };
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async confirm(token: string): Promise<{ status: number; body: any }> {
if (!token) return { status: 400, body: { error: 'token is required' } };
const instanceId = passkeyCeremonyStore.instanceForToken(token);
if (!instanceId) return { status: 404, body: { error: 'ceremony not found or expired' } };
const instance = this.resolveInstance(instanceId);
if (!instance) return { status: 404, body: { error: 'instance not found' } };
try {
await instance.confirmPasskey();
return { status: 200, body: { ok: true } };
} catch (error) {
this.logger.error(`confirm: ${(error as Error).message}`);
return { status: 500, body: { error: (error as Error).message } };
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private resolveInstance(instanceId: string): any {
for (const name of Object.keys(waMonitor.waInstances)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const inst: any = waMonitor.waInstances[name];
if (inst?.instanceId === instanceId) return inst;
}
return undefined;
}
}