Repository navigation
Expand file tree
/
Copy pathcontroller.ts
More file actions
109 lines (106 loc) · 3.95 KB
/
Copy pathcontroller.ts
File metadata and controls
109 lines (106 loc) · 3.95 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
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
/** @module SMS verification */
import { sessionId } from "@/shared/server/session/cookies";
import { json, requestFailure } from "@/shared/server/http/response";
import { checkAuthLimit } from "@/shared/server/storage/rate-limit";
import { cibaService } from "@/shared/server/ciba/service";
import { addApiLogContext } from "@/shared/server/common/logger";
import {
readCibaRequest,
startResponse,
tokenResponse,
operationFailure,
} from "@/shared/server/ciba/handlers";
/**
* `POST /sms/auth` starts a provider SMS challenge.
*
* @param request - Send `client_id`, `server_id`, `login_hint` and `scope` as
* JSON. Use a normalized phone number for `login_hint`.
* @returns On success, HTTP 200 with `auth_server`, `auth_req_id` and `nonce`.
* Save the request ID and nonce for {@link verifySmsCode}. Rate limiting
* returns 429 with `Retry-After`.
*/
export async function sendSmsCode(request: Request) {
let context, input;
try {
// Parse common CIBA fields and reject requests that exceed this endpoint's rate limit.
({ context, input } = await readCibaRequest(request));
addApiLogContext(request, {
clientId: input.client_id,
serverId: input.server_id,
scope: input.scope,
phone: input.login_hint,
});
const limit = await checkAuthLimit("sms-auth", context, input);
if (!limit.allowed) {
addApiLogContext(request, { result: "rate_limited" });
return json({ error: "RATE_LIMITED" }, 429, limit.headers);
}
} catch (error) {
addApiLogContext(request, { result: "invalid_request" });
return requestFailure(error);
}
try {
// Ask the shared CIBA service to create the provider SMS challenge.
const outcome = await cibaService.sendSmsCode(context, input);
addApiLogContext(request, {
result: outcome.kind,
authServerId: outcome.server.id,
...(outcome.kind === "failure"
? { providerStatus: outcome.failure.status }
: { authRequestId: outcome.authReqId }),
});
return startResponse(outcome);
} catch (error) {
addApiLogContext(request, { result: "service_unavailable" });
return operationFailure(error, "auth");
}
}
/**
* `POST /sms/token` submits the user's OTP and exchanges the SMS request.
*
* @param request - Send `client_id`, `server_id`, `code`, `auth_req_id` and
* `nonce`. Reuse the values from {@link sendSmsCode} for this challenge.
* @returns HTTP 200 with `auth_server` and provider claims on success. Check
* the status and claims before treating the phone as verified. The demo
* may create a display session for an error response.
*/
export async function verifySmsCode(request: Request) {
let context, input;
try {
// Read the same challenge fields so the provider request stays tied to /sms/auth.
({ context, input } = await readCibaRequest(request));
addApiLogContext(request, {
clientId: input.client_id,
serverId: input.server_id,
authRequestId: input.auth_req_id,
code: input.code,
});
const limit = await checkAuthLimit("sms-token", context, input);
if (!limit.allowed) {
addApiLogContext(request, { result: "rate_limited" });
return json({ error: "RATE_LIMITED" }, 429, limit.headers);
}
} catch (error) {
addApiLogContext(request, { result: "invalid_request" });
return requestFailure(error);
}
try {
// Exchange the submitted OTP; tokenResponse also sets the Showcase result session.
const outcome = await cibaService.verifySmsCode(
context,
input,
sessionId(request),
);
addApiLogContext(request, {
result: outcome.kind,
authServerId: outcome.server.id,
...(outcome.kind === "failure"
? { providerStatus: outcome.failure.status }
: { claimCount: Object.keys(outcome.data).length }),
});
return tokenResponse(outcome, context.origin);
} catch (error) {
addApiLogContext(request, { result: "service_unavailable" });
return operationFailure(error, "token");
}
}