forked from aws-samples/amazon-cognito-passwordless-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic-link.ts
445 lines (432 loc) · 14.1 KB
/
magic-link.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**
* Copyright Amazon.com, Inc. and its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You
* may not use this file except in compliance with the License. A copy of
* the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
import { createHash, createPublicKey, constants, createVerify } from "crypto";
import {
CreateAuthChallengeTriggerEvent,
VerifyAuthChallengeResponseTriggerEvent,
} from "aws-lambda";
import {
DynamoDBClient,
ConditionalCheckFailedException,
} from "@aws-sdk/client-dynamodb";
import {
DynamoDBDocumentClient,
DeleteCommand,
PutCommand,
} from "@aws-sdk/lib-dynamodb";
import {
SESClient,
SendEmailCommand,
MessageRejected,
} from "@aws-sdk/client-ses";
import {
KMSClient,
SignCommand,
GetPublicKeyCommand,
} from "@aws-sdk/client-kms";
import {
logger,
UserFacingError,
handleConditionalCheckFailedException,
} from "./common.js";
let config = {
/** Should Magic Link sign-in be enabled? If set to false, clients cannot sign-in with magic links (an error is shown instead when they request a magic link) */
magicLinkEnabled: !!process.env.MAGIC_LINK_ENABLED,
/** Number of seconds a Magic Link should be valid */
secondsUntilExpiry: Number(process.env.SECONDS_UNTIL_EXPIRY || 60 * 15),
/** Number of seconds that must lapse between unused Magic Links (to prevent misuse) */
minimumSecondsBetween: Number(process.env.MIN_SECONDS_BETWEEN || 60 * 1),
/** The origins that are allowed to be used in the Magic Links */
allowedOrigins: process.env.ALLOWED_ORIGINS?.split(",")
.map((href) => new URL(href))
.map((url) => url.origin),
/** The e-mail address that Magic Links will be sent from */
sesFromAddress: process.env.SES_FROM_ADDRESS,
/** The Amazon SES region, override e.g. to set a region where you are out of the SES sandbox */
sesRegion: process.env.SES_REGION || process.env.AWS_REGION,
/** KMS Key ID to use for generating Magic Links (signatures) */
kmsKeyId: process.env.KMS_KEY_ID,
/** The name of the DynamoDB table where (hashes of) Magic Links will be stored */
dynamodbSecretsTableName: process.env.DYNAMODB_SECRETS_TABLE,
/** Function that will send the actual Magic Link e-mails. Override this to e.g. use another e-mail provider instead of Amazon SES */
emailSender: sendEmailWithLink,
/** A salt to use for storing hashes of magic links in the DynamoDB table */
salt: process.env.STACK_ID,
/** Function to create the content of the Magic Link e-mails, override to e.g. use a custom e-mail template */
contentCreator: createEmailContent,
/** Error message that will be shown to the client, if the client requests a Magic Link but isn't allowed to yet */
notNowMsg:
"We can't send you a magic link right now, please try again in a minute",
};
function requireConfig<K extends keyof typeof config>(
k: K
): NonNullable<(typeof config)[K]> {
// eslint-disable-next-line security/detect-object-injection
const value = config[k];
if (value === undefined) throw new Error(`Missing configuration for: ${k}`);
return value;
}
export function configure(update?: Partial<typeof config>) {
const oldSesRegion = config.sesRegion;
config = { ...config, ...update };
if (update && update.sesRegion !== oldSesRegion) {
ses = new SESClient({ region: config.sesRegion });
}
return config;
}
const publicKeys: Record<string, ReturnType<typeof createPublicKey>> = {};
const kms = new KMSClient({});
const ddbDocClient = DynamoDBDocumentClient.from(new DynamoDBClient({}), {
marshallOptions: {
removeUndefinedValues: true,
},
});
let ses = new SESClient({ region: config.sesRegion });
export async function addChallengeToEvent(
event: CreateAuthChallengeTriggerEvent
): Promise<void> {
if (!config.magicLinkEnabled)
throw new UserFacingError("Sign-in with Magic Link not supported");
event.response.challengeMetadata = "MAGIC_LINK";
const alreadyHaveMagicLink =
event.request.clientMetadata?.alreadyHaveMagicLink;
if (alreadyHaveMagicLink === "yes") {
// The client already has a sign-in code, we don't need to send a new one
logger.info("Client will use already obtained sign-in link");
return;
}
logger.info("Client needs sign-in link");
// Determine the redirect URI for the magic link
const redirectUri = event.request.clientMetadata?.redirectUri;
if (
!redirectUri ||
!requireConfig("allowedOrigins").includes(new URL(redirectUri).origin)
) {
throw new UserFacingError(`Invalid redirectUri: ${redirectUri}`);
}
// Send challenge with new secret login code
await createAndSendMagicLink(event, {
redirectUri,
});
const email = event.request.userAttributes.email;
// The event.request.userNotFound is only present in the Lambda trigger if "Prevent user existence errors" is checked
// in the Cognito app client. If it is *not* checked, the client receives the error, which potentially allows for
// user enumeration. Additional guardrails are advisable.
if (event.request.userNotFound) {
logger.info("User not found");
}
// Current implementation has no use for publicChallengeParameters - feel free to provide them
// if you want to use them in your front-end:
// event.response.publicChallengeParameters = {};
event.response.privateChallengeParameters = {
email: email,
};
}
async function createEmailContent({
secretLoginLink,
}: {
secretLoginLink: string;
}) {
return {
html: {
data: `<html><body><p>Your secret sign-in link: <a href="${secretLoginLink}">sign in</a></p>This link is valid for ${Math.floor(
config.secondsUntilExpiry / 60
)} minutes<p></p></body></html>`,
charSet: "UTF-8",
},
text: {
data: `Your secret sign-in link: ${secretLoginLink}`,
charSet: "UTF-8",
},
subject: {
data: "Your secret sign-in link",
charSet: "UTF-8",
},
};
}
async function sendEmailWithLink({
emailAddress,
content,
}: {
emailAddress: string;
content: {
html: { charSet: string; data: string };
text: { charSet: string; data: string };
subject: { charSet: string; data: string };
};
}) {
await ses
.send(
new SendEmailCommand({
Destination: { ToAddresses: [emailAddress] },
Message: {
Body: {
Html: {
Charset: content.html.charSet,
Data: content.html.data,
},
Text: {
Charset: content.text.charSet,
Data: content.text.data,
},
},
Subject: {
Charset: content.subject.charSet,
Data: content.subject.data,
},
},
Source: requireConfig("sesFromAddress"),
})
)
.catch((err) => {
if (
err instanceof MessageRejected &&
err.message.includes("Email address is not verified")
) {
logger.error(err);
throw new UserFacingError(
"E-mail address must still be verified in the e-mail service"
);
}
throw err;
});
}
async function createAndSendMagicLink(
event: CreateAuthChallengeTriggerEvent,
{
redirectUri,
}: {
redirectUri: string;
}
): Promise<void> {
logger.debug("Creating new magic link ...");
const exp = Math.floor(Date.now() / 1000 + config.secondsUntilExpiry);
const iat = Math.floor(Date.now() / 1000);
const message = Buffer.from(
JSON.stringify({
userName: event.userName,
iat,
exp,
})
);
const messageContext = Buffer.from(
JSON.stringify({
userPoolId: event.userPoolId,
clientId: event.callerContext.clientId,
})
);
const kmsKeyId = requireConfig("kmsKeyId");
const { Signature: signature } = await kms.send(
new SignCommand({
KeyId: kmsKeyId,
Message: createHash("sha512")
.end(Buffer.concat([message, messageContext]))
.digest(),
SigningAlgorithm: "RSASSA_PSS_SHA_512",
MessageType: "DIGEST",
})
);
if (!signature) {
throw new Error("Failed to create signature with KMS");
}
logger.debug("Storing magic link hash in DynamoDB ...");
const salt = requireConfig("salt");
await ddbDocClient
.send(
new PutCommand({
TableName: requireConfig("dynamodbSecretsTableName"),
Item: {
userNameHash: createHash("sha256")
.update(salt)
.end(event.userName)
.digest(),
signatureHash: createHash("sha256")
.update(salt)
.end(signature)
.digest(),
iat,
exp,
kmsKeyId: kmsKeyId,
},
// Throttle: fail if we've alreay sent a magic link less than SECONDS_BETWEEN seconds ago:
ConditionExpression: "attribute_not_exists(#iat) or #iat < :iat",
ExpressionAttributeNames: {
"#iat": "iat",
},
ExpressionAttributeValues: {
":iat": Math.floor(Date.now() / 1000) - config.minimumSecondsBetween,
},
})
)
.catch(handleConditionalCheckFailedException(config.notNowMsg));
const secretLoginLink = `${redirectUri}#${message.toString(
"base64url"
)}.${Buffer.from(signature).toString("base64url")}`;
logger.debug("Sending magic link ...");
// Toggle userNotFound error with "Prevent user existence errors" in the Cognito app client. (see above)
if (event.request.userNotFound) {
return;
}
await config.emailSender({
emailAddress: event.request.userAttributes.email,
content: await config.contentCreator.call(undefined, {
secretLoginLink,
}),
});
logger.debug("Magic link sent!");
}
export async function addChallengeVerificationResultToEvent(
event: VerifyAuthChallengeResponseTriggerEvent
) {
logger.info("Verifying MagicLink Challenge Response ...");
// Toggle userNotFound error with "Prevent user existence errors" in the Cognito app client. (see above)
if (event.request.userNotFound) {
logger.info("User not found");
}
if (!config.magicLinkEnabled)
throw new UserFacingError("Sign-in with Magic Link not supported");
if (
event.request.privateChallengeParameters.challenge ===
"PROVIDE_AUTH_PARAMETERS" &&
event.request.clientMetadata?.alreadyHaveMagicLink !== "yes"
)
return;
event.response.answerCorrect = await verifyMagicLink(
event.request.challengeAnswer,
event.userName,
{
userPoolId: event.userPoolId,
clientId: event.callerContext.clientId,
}
);
}
async function downloadPublicKey(kmsKeyId: string) {
logger.debug("Downloading KMS public key");
const { PublicKey: publicKey } = await kms.send(
new GetPublicKeyCommand({
KeyId: kmsKeyId,
})
);
if (!publicKey) {
throw new Error("Failed to download public key from KMS");
}
return createPublicKey({
key: publicKey as Buffer,
format: "der",
type: "spki",
});
}
async function verifyMagicLink(
magicLinkFragmentIdentifier: string,
userName: string,
context: { userPoolId: string; clientId: string }
) {
logger.debug(
"Verifying magic link fragment identifier:",
magicLinkFragmentIdentifier
);
const [messageB64, signatureB64] = magicLinkFragmentIdentifier.split(".");
const signature = Buffer.from(signatureB64, "base64url");
// Read and remove item from DynamoDB
let dbItem: Record<string, unknown> | undefined = undefined;
try {
({ Attributes: dbItem } = await ddbDocClient.send(
new DeleteCommand({
TableName: requireConfig("dynamodbSecretsTableName"),
Key: {
userNameHash: createHash("sha256")
.update(requireConfig("salt"))
.end(userName)
.digest(),
},
ReturnValues: "ALL_OLD",
ConditionExpression:
"attribute_exists(#signatureHash) AND #signatureHash = :signatureHash",
ExpressionAttributeNames: {
"#signatureHash": "signatureHash",
},
ExpressionAttributeValues: {
":signatureHash": createHash("sha256")
.update(requireConfig("salt"))
.end(signature)
.digest(),
},
})
));
} catch (err) {
if (err instanceof ConditionalCheckFailedException) {
logger.error(
"Attempt to use invalid (potentially superseeded) magic link"
);
return false;
}
throw err;
}
if (!dbItem) {
logger.error("Attempt to use magic link more than once");
return false;
}
if (!dbItem.kmsKeyId || typeof dbItem.kmsKeyId !== "string") {
throw new Error("Failed to determine KMS Key ID");
}
publicKeys[dbItem.kmsKeyId] ??= await downloadPublicKey(dbItem.kmsKeyId);
const verifier = createVerify("RSA-SHA512");
const message = Buffer.from(messageB64, "base64url");
verifier.update(message);
const messageContext = Buffer.from(JSON.stringify(context));
verifier.update(messageContext);
const valid = verifier.verify(
{
key: publicKeys[dbItem.kmsKeyId],
padding: constants.RSA_PKCS1_PSS_PADDING,
saltLength: constants.RSA_PSS_SALTLEN_DIGEST,
},
signature
);
logger.debug(`Magic link signature is ${valid ? "" : "NOT "}valid`);
if (!valid) return false;
const parsed: unknown = JSON.parse(message.toString());
assertIsMessage(parsed);
logger.debug("Checking message:", parsed);
if (parsed.userName !== userName) {
logger.error("Different userName!");
return false;
}
if (parsed.exp < Date.now() / 1000) {
logger.error("Magic link expired!");
return false;
}
if (parsed.exp !== dbItem.exp || parsed.iat !== dbItem.iat) {
logger.error("State mismatch");
return false;
}
return valid;
}
function assertIsMessage(
msg: unknown
): asserts msg is { userName: string; exp: number; iat: number } {
if (
!msg ||
typeof msg !== "object" ||
!("userName" in msg) ||
typeof msg.userName !== "string" ||
!("exp" in msg) ||
typeof msg.exp !== "number" ||
!("iat" in msg) ||
typeof msg.iat !== "number"
) {
throw new Error("Invalid magic link");
}
}