generated from sergiodxa/remix-auth-strategy-template
-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.ts
423 lines (373 loc) · 12.6 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
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
import type { SessionStorage } from '@remix-run/server-runtime'
import { redirect } from '@remix-run/server-runtime'
import crypto from 'crypto-js'
import type { AuthenticateOptions, StrategyVerifyCallback } from 'remix-auth'
import { Strategy } from 'remix-auth'
export type SendEmailOptions<User> = {
emailAddress: string
magicLink: string
user?: User | null
domainUrl: string
form: FormData
}
export type SendEmailFunction<User> = {
(options: SendEmailOptions<User>): Promise<void>
}
/**
* Validate the email address the user is trying to use to login.
* This can be useful to ensure it's not a disposable email address.
* @param emailAddress The email address to validate
*/
export type VerifyEmailFunction = {
(email: string): Promise<void>
}
/**
* The content of the magic link payload
*/
export type MagicLinkPayload = {
/**
* The email address used to authenticate
*/
emailAddress: string
form: Record<string, unknown>
/**
* When the magic link was created, as an ISO string. This is used to check
* the email link is still valid.
*/
creationDate: string
/**
* If it should be validated or not.
*/
validateSessionMagicLink: boolean
}
/**
* This interface declares what configuration the strategy needs from the
* developer to correctly work.
*/
export type EmailLinkStrategyOptions<User> = {
/**
* The endpoint the user will go after clicking on the email link.
* A whole URL is not required, the pathname is enough, the strategy will
* detect the host of the request and use it to build the URL.
* @default "/magic"
*/
callbackURL?: string
/**
* A function to send the email. This function should receive the email
* address of the user and the URL to redirect to and should return a Promise.
* The value of the Promise will be ignored.
*/
sendEmail: SendEmailFunction<User>
/**
* A function to validate the email address. This function should receive the
* email address as a string and return a Promise. The value of the Promise
* will be ignored, in case of error throw an error.
*
* By default it only test the email against the RegExp `/.+@.+/`.
*/
verifyEmailAddress?: VerifyEmailFunction
/**
* A secret string used to encrypt and decrypt the token and magic link.
*/
secret: string
/**
* The name of the form input used to get the email.
* @default "email"
*/
emailField?: string
/**
* The param name the strategy will use to read the token from the email link.
* @default "token"
*/
magicLinkSearchParam?: string
/**
* How long the magic link will be valid. Default to 30 minutes.
* @default 1_800_000
*/
linkExpirationTime?: number
/**
* The key on the session to store any error message.
* @default "auth:error"
*/
sessionErrorKey?: string
/**
* The key on the session to store the magic link.
* @default "auth:magicLink"
*/
sessionMagicLinkKey?: string
/**
* Add an extra layer of protection and validate the magic link is valid.
* @default false
*/
validateSessionMagicLink?: boolean
}
/**
* This interface declares what the developer will receive from the strategy
* to verify the user identity in their system.
*/
export type EmailLinkStrategyVerifyParams = {
email: string
form: FormData
/**
* True, if the verify callback is called after clicking on the magic link
*/
magicLinkVerify: boolean
}
const verifyEmailAddress: VerifyEmailFunction = async (email) => {
if (!/.+@.+/u.test(email)) {
throw new Error('A valid email is required.')
}
}
export class EmailLinkStrategy<User> extends Strategy<
User,
EmailLinkStrategyVerifyParams
> {
public name = 'email-link'
private readonly emailField: string = 'email'
private readonly callbackURL: string
private readonly sendEmail: SendEmailFunction<User>
private readonly validateEmail: VerifyEmailFunction
private readonly secret: string
private readonly magicLinkSearchParam: string
private readonly linkExpirationTime: number
private readonly sessionErrorKey: string
private readonly sessionMagicLinkKey: string
private readonly validateSessionMagicLink: boolean
constructor(
options: EmailLinkStrategyOptions<User>,
verify: StrategyVerifyCallback<User, EmailLinkStrategyVerifyParams>
) {
super(verify)
this.sendEmail = options.sendEmail
this.callbackURL = options.callbackURL ?? '/magic'
this.secret = options.secret
this.sessionErrorKey = options.sessionErrorKey ?? 'auth:error'
this.sessionMagicLinkKey = options.sessionMagicLinkKey ?? 'auth:magiclink'
this.validateEmail = options.verifyEmailAddress ?? verifyEmailAddress
this.emailField = options.emailField ?? this.emailField
this.magicLinkSearchParam = options.magicLinkSearchParam ?? 'token'
this.linkExpirationTime = options.linkExpirationTime ?? 1000 * 60 * 30 // 30 minutes
this.validateSessionMagicLink = options.validateSessionMagicLink ?? false
}
public async authenticate(
request: Request,
sessionStorage: SessionStorage,
options: AuthenticateOptions
): Promise<User> {
const session = await sessionStorage.getSession(
request.headers.get('Cookie')
)
const form = new URLSearchParams(await request.text())
// This should only be called in an action if it's used to start the login process
if (request.method === 'POST') {
if (!options.successRedirect) {
throw new Error(
'Missing successRedirect. The successRedirect is required for POST requests.'
)
}
// get the email address from the request body
const emailAddress = form.get(this.emailField)
// if it doesn't have an email address,
if (!emailAddress || typeof emailAddress !== 'string') {
const message = 'Missing email address.'
if (!options.failureRedirect) {
throw new Error(message)
}
session.flash(this.sessionErrorKey, { message })
const cookie = await sessionStorage.commitSession(session)
throw redirect(options.failureRedirect, {
headers: { 'Set-Cookie': cookie },
})
}
try {
// Validate the email address
await this.validateEmail(emailAddress)
const domainUrl = this.getDomainURL(request)
const magicLink = await this.sendToken(emailAddress, domainUrl, form)
session.set(this.sessionMagicLinkKey, await this.encrypt(magicLink))
throw redirect(options.successRedirect, {
headers: {
'Set-Cookie': await sessionStorage.commitSession(session),
},
})
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((error as any).status === 302) {
// If it's a redirect, then just throw the redirect as it is
throw error
}
if (!options.failureRedirect) {
throw error
}
const { message } = error as Error
session.flash(this.sessionErrorKey, { message })
const cookie = await sessionStorage.commitSession(session)
throw redirect(options.failureRedirect, {
headers: { 'Set-Cookie': cookie },
})
}
}
let user: User
try {
// If we get here, the user clicked on the magic link inside email
const magicLink = session.get(this.sessionMagicLinkKey) ?? ''
const { emailAddress: email, form } = await this.validateMagicLink(
request.url,
await this.decrypt(magicLink)
)
// now that we have the user email we can call verify to get the user
user = await this.verify({ email, form, magicLinkVerify: true })
} catch (error) {
// if something happens, we should redirect to the failureRedirect
// and flash the error message, or just throw the error if failureRedirect
// is not defined
if (!options.failureRedirect) {
throw error
}
const { message } = error as Error
session.flash(this.sessionErrorKey, { message })
const cookie = await sessionStorage.commitSession(session)
throw redirect(options.failureRedirect, {
headers: { 'Set-Cookie': cookie },
})
}
if (!options.successRedirect) {
return user
}
// remove the magic link from the session
session.unset(this.sessionMagicLinkKey)
session.set(options.sessionKey, user)
const cookie = await sessionStorage.commitSession(session)
throw redirect(options.successRedirect, {
headers: { 'Set-Cookie': cookie },
})
}
private getDomainURL(request: Request): string {
const host =
request.headers.get('X-Forwarded-Host') ?? request.headers.get('host')
if (!host) {
throw new Error('Could not determine domain URL.')
}
const protocol =
host.includes('localhost') || host.includes('127.0.0.1')
? 'http'
: 'https'
return `${protocol}://${host}`
}
private async sendToken(email: string, domainUrl: string, form: FormData) {
const magicLink = await this.getMagicLink(email, domainUrl, form)
const user = await this.verify({
email,
form,
magicLinkVerify: false,
}).catch(() => null)
await this.sendEmail({
emailAddress: email,
magicLink,
user,
domainUrl,
form,
})
return magicLink
}
private async getMagicLink(
emailAddress: string,
domainUrl: string,
form: FormData
) {
const payload = this.createMagicLinkPayload(emailAddress, form)
const stringToEncrypt = JSON.stringify(payload)
const encryptedString = await this.encrypt(stringToEncrypt)
const url = new URL(domainUrl)
url.pathname = this.callbackURL
url.searchParams.set(this.magicLinkSearchParam, encryptedString)
return url.toString()
}
private createMagicLinkPayload(
emailAddress: string,
form: FormData
): MagicLinkPayload {
return {
emailAddress,
form: Object.fromEntries(
[...form.keys()].map((key) => [
key,
form.getAll(key).length > 1 ? form.getAll(key) : form.get(key),
])
),
creationDate: new Date().toISOString(),
validateSessionMagicLink: this.validateSessionMagicLink,
}
}
private async encrypt(value: string): Promise<string> {
return crypto.AES.encrypt(value, this.secret).toString()
}
private async decrypt(value: string): Promise<string> {
const bytes = crypto.AES.decrypt(value, this.secret)
return bytes.toString(crypto.enc.Utf8)
}
private getMagicLinkCode(link: string) {
try {
const url = new URL(link)
return url.searchParams.get(this.magicLinkSearchParam) ?? ''
} catch {
return ''
}
}
private async validateMagicLink(
requestUrl: string,
sessionMagicLink?: string
) {
const linkCode = this.getMagicLinkCode(requestUrl)
const sessionLinkCode = sessionMagicLink
? this.getMagicLinkCode(sessionMagicLink)
: null
let emailAddress
let linkCreationDateString
let validateSessionMagicLink
let form: Record<string, unknown>
try {
const decryptedString = await this.decrypt(linkCode)
const payload = JSON.parse(decryptedString) as MagicLinkPayload
emailAddress = payload.emailAddress
form = payload.form
linkCreationDateString = payload.creationDate
validateSessionMagicLink = payload.validateSessionMagicLink
} catch (error: unknown) {
console.error(error)
throw new Error('Sign in link invalid. Please request a new one.')
}
if (typeof emailAddress !== 'string') {
throw new TypeError('Sign in link invalid. Please request a new one.')
}
if (validateSessionMagicLink) {
if (!sessionLinkCode) {
throw new Error('Sign in link invalid. Please request a new one.')
}
if (linkCode !== sessionLinkCode) {
throw new Error(
`You must open the magic link on the same device it was created from for security reasons. Please request a new link.`
)
}
}
if (typeof linkCreationDateString !== 'string') {
throw new TypeError('Sign in link invalid. Please request a new one.')
}
const linkCreationDate = new Date(linkCreationDateString)
const expirationTime = linkCreationDate.getTime() + this.linkExpirationTime
if (Date.now() > expirationTime) {
throw new Error('Magic link expired. Please request a new one.')
}
const formData = new FormData()
Object.keys(form).forEach((key) => {
if (Array.isArray(form[key])) {
;(form[key] as unknown[]).forEach((value) => {
formData.append(key, value as string | Blob)
})
} else {
formData.append(key, form[key] as string | Blob)
}
})
return { emailAddress, form: formData }
}
}