-
Notifications
You must be signed in to change notification settings - Fork 1
/
hanko-handlers.ts
212 lines (197 loc) · 5.5 KB
/
hanko-handlers.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { http, HttpResponse } from "msw";
import { buildMockAccessToken } from "./data/access-token";
import {
buildHankoNotAuthorizedResponse,
buildHankoNotFoundResponse,
buildResponseForToken,
buildUser,
findUserByEmail,
findUserById,
getUserByCookies,
type HankoConfigResponse,
type HankoCurrentUserResponse,
type HankoEmailResponse,
type HankoInitializePasscodeChallengeResponse,
type HankoNotFoundResponse,
type HankoUnauthorizedResponse,
type HankoUserInfoResponse,
} from "./data/hanko";
import { logger } from "./data/log";
const ENABLE_PASSCODE_FLOW = true;
const hankoUrl = import.meta.env.VITE_HANKO_API_URL as string;
const getWellKnownConfig = http.get<{}, {}, HankoConfigResponse>(
`${hankoUrl}/.well-known/config`,
async () => {
return HttpResponse.json({
password: { enabled: !ENABLE_PASSCODE_FLOW, min_password_length: 0 },
emails: { require_verification: false, max_num_of_addresses: 5 },
account: { allow_deletion: true, allow_signup: false },
providers: [],
use_enterprise: false,
});
},
);
const me = http.get<
{},
{},
HankoCurrentUserResponse | HankoUnauthorizedResponse
>(`${hankoUrl}/me`, async ({ cookies }) => {
const user = getUserByCookies(cookies);
if (!user) {
return buildHankoNotAuthorizedResponse();
}
logger.success(`[MSW/Hanko] Found user ${user.id} from cookies`, {
user,
});
return HttpResponse.json({
id: user.id,
});
});
const usersByUserId = http.get<
{
userId: string;
},
{},
HankoNotFoundResponse | any
>(`${hankoUrl}/users/:userId`, ({ params }) => {
const { userId } = params;
const user = buildUser(findUserById(userId as string));
if (!user) {
return buildHankoNotFoundResponse();
}
return HttpResponse.json(user);
});
const initializePasscodeChallenge = http.post<
{},
{ user_id: string },
HankoInitializePasscodeChallengeResponse | HankoNotFoundResponse
>(`${hankoUrl}/passcode/login/initialize`, async ({ request }) => {
const { user_id } = await request.json();
const userInfo = findUserById(user_id)!;
const user = buildUser(userInfo);
if (!user) {
return buildHankoNotFoundResponse();
}
return HttpResponse.json({
id: user_id,
ttl: 300,
created_at: new Date().toISOString(),
} as HankoInitializePasscodeChallengeResponse);
});
const finalizePasscodeChallenge = http.post<
{},
{ id: string; code: string },
| HankoInitializePasscodeChallengeResponse
| HankoNotFoundResponse
| HankoUnauthorizedResponse
>(`${hankoUrl}/passcode/login/finalize`, async ({ request }) => {
const { id, code } = await request.json();
const userInfo = findUserById(id)!;
const user = buildUser(userInfo);
if (!user) {
return buildHankoNotFoundResponse();
}
if (code !== userInfo.passcode) {
return buildHankoNotAuthorizedResponse();
}
return buildResponseForToken(
{
id,
ttl: 300,
created_at: new Date().toISOString(),
} satisfies HankoInitializePasscodeChallengeResponse,
buildMockAccessToken(user.id),
);
});
const userInfoByEmail = http.post<
{},
{ email: string },
HankoUserInfoResponse | HankoNotFoundResponse
>(`${hankoUrl}/user`, async ({ request }) => {
const { email } = await request.json();
logger.info(`[MSW/Hanko] Retrieving user by email: ${email}`);
const user = buildUser(findUserByEmail(email));
if (!user) {
return buildHankoNotFoundResponse();
}
logger.success(`[MSW/Hanko] User ${user.id} found`, { user });
return HttpResponse.json({
id: user.id,
email_id: user.email,
verified: true,
has_webauthn_credential: true,
});
});
const login = http.post<{}, { user_id: string; password: string }>(
`${hankoUrl}/password/login`,
async ({ request }) => {
const { user_id, password } = await request.json();
logger.info(`[MSW/Hanko] Login flow for user ${user_id}`);
const user = findUserById(user_id);
logger.success(`[MSW/Hanko] User ${user_id} found`, { user });
if (!user) {
return buildHankoNotFoundResponse();
}
if (user.password !== password) {
return buildHankoNotAuthorizedResponse();
}
return buildResponseForToken(null, buildMockAccessToken(user.id));
},
);
const logout = http.post(`${hankoUrl}/logout`, () => {
return HttpResponse.text(null, { status: 204 });
});
const emails = [] as HankoEmailResponse[];
const getCurrentUserEmails = http.get<
{},
{},
HankoUnauthorizedResponse | HankoEmailResponse[]
>(`${hankoUrl}/emails`, ({ cookies }) => {
const user = getUserByCookies(cookies);
if (!user) {
return buildHankoNotAuthorizedResponse();
}
return HttpResponse.json([
{
id: user.emailId,
address: user.email,
is_verified: true,
is_primary: true,
identity: null,
} as {} as HankoEmailResponse,
...emails,
]);
});
const getCurrentUserCredentials = http.get(
`${hankoUrl}/webauthn/credentials`,
() => {
return HttpResponse.json([]);
},
);
const addEmail = http.post<{}, { address: string }>(
`${hankoUrl}/emails`,
async ({ request }) => {
const body = await request.json();
emails.push({
address: body.address,
id: crypto.randomUUID(),
is_verified: false,
is_primary: false,
identity: { id: crypto.randomUUID(), provider: "google" },
});
return HttpResponse.json([]);
},
);
export const hankoHandlers = [
me,
usersByUserId,
userInfoByEmail,
login,
getWellKnownConfig,
logout,
initializePasscodeChallenge,
finalizePasscodeChallenge,
getCurrentUserEmails,
getCurrentUserCredentials,
addEmail,
];