WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 62163e9

Browse files
mw10013dev-xo
andauthored
refactor: use request origin for magic-link if hostUrl not provided (#29)
* refactor: use request origin for magic link if hostUrl not provided * chore: minor test name rearrangement * chore: remove 'getHostUrl' and its respective tests, along with 'HOST' constant and its calls in test headers. --------- Co-authored-by: Dev XO <[email protected]>
1 parent 6eca590 commit 62163e9

File tree

3 files changed

+24
-52
lines changed

3 files changed

+24
-52
lines changed

src/utils.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function generateMagicLink(
3333

3434
const url = new URL(
3535
options.callbackPath ?? '/',
36-
options.hostUrl ?? getHostUrl(options.request),
36+
options.hostUrl ?? new URL(options.request.url).origin,
3737
)
3838
url.searchParams.set(options.param, options.code)
3939

@@ -81,18 +81,3 @@ export async function verifyJWT({ jwt, secretKey }: VerifyJWTOptions) {
8181
throw new Error(ERRORS.INVALID_JWT)
8282
}
8383
}
84-
85-
/**
86-
* Miscellaneous.
87-
*/
88-
export function getHostUrl(request: Request) {
89-
const host = request.headers.get('X-Forwarded-Host') ?? request.headers.get('host')
90-
if (!host) throw new Error('Could not determine host.')
91-
92-
// If the host is localhost or ends with .local, use http.
93-
const protocol = host.match(/(:?\.local|^localhost|^127\.\d+\.\d+\.\d+)(:?:\d+)?$/)
94-
? 'http'
95-
: 'https'
96-
97-
return `${protocol}://${host}`
98-
}

test/index.spec.ts

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, test, expect, afterEach, vi } from 'vitest'
22
import { AuthorizationError } from 'remix-auth'
33

44
import { TOTPStrategy } from '../src/index'
5-
import { generateTOTP, generateMagicLink, getHostUrl, signJWT } from '../src/utils'
5+
import { generateTOTP, generateMagicLink, signJWT } from '../src/utils'
66
import { STRATEGY_NAME, FORM_FIELDS, SESSION_KEYS, ERRORS } from '../src/constants'
77

88
import {
@@ -275,7 +275,6 @@ describe('[ TOTP ]', () => {
275275
const request = new Request(`${HOST_URL}`, {
276276
method: 'POST',
277277
headers: {
278-
host: HOST_URL,
279278
cookie: await sessionStorage.commitSession(session),
280279
},
281280
body: formData,
@@ -308,7 +307,6 @@ describe('[ TOTP ]', () => {
308307

309308
const request = new Request(`${HOST_URL}`, {
310309
method: 'POST',
311-
headers: { host: HOST_URL },
312310
body: formData,
313311
})
314312

@@ -334,7 +332,6 @@ describe('[ TOTP ]', () => {
334332

335333
const request = new Request(`${HOST_URL}`, {
336334
method: 'POST',
337-
headers: { host: HOST_URL },
338335
body: formData,
339336
})
340337

@@ -370,7 +367,6 @@ describe('[ TOTP ]', () => {
370367

371368
const request = new Request(`${HOST_URL}`, {
372369
method: 'POST',
373-
headers: { host: HOST_URL },
374370
body: formData,
375371
})
376372

@@ -410,7 +406,6 @@ describe('[ TOTP ]', () => {
410406

411407
const request = new Request(`${HOST_URL}`, {
412408
method: 'POST',
413-
headers: { host: HOST_URL },
414409
body: formData,
415410
})
416411

@@ -450,7 +445,6 @@ describe('[ TOTP ]', () => {
450445
const request = new Request(`${HOST_URL}`, {
451446
method: 'POST',
452447
headers: {
453-
host: HOST_URL,
454448
cookie: await sessionStorage.commitSession(session),
455449
},
456450
body: formData,
@@ -495,7 +489,6 @@ describe('[ TOTP ]', () => {
495489
const request = new Request(`${HOST_URL}`, {
496490
method: 'POST',
497491
headers: {
498-
host: HOST_URL,
499492
cookie: await sessionStorage.commitSession(session),
500493
},
501494
body: formData,
@@ -543,9 +536,7 @@ describe('[ TOTP ]', () => {
543536
callbackPath: '/magic-link',
544537
param: 'code',
545538
code: _otp,
546-
request: new Request(HOST_URL, {
547-
headers: { host: HOST_URL },
548-
}),
539+
request: new Request(HOST_URL),
549540
})
550541

551542
const session = await sessionStorage.getSession()
@@ -554,7 +545,6 @@ describe('[ TOTP ]', () => {
554545
const request = new Request(`${magicLink}`, {
555546
method: 'GET',
556547
headers: {
557-
host: HOST_URL,
558548
cookie: await sessionStorage.commitSession(session),
559549
},
560550
})
@@ -589,14 +579,11 @@ describe('[ TOTP ]', () => {
589579
callbackPath: '/invalid',
590580
param: 'code',
591581
code: _otp,
592-
request: new Request(HOST_URL, {
593-
headers: { host: HOST_URL },
594-
}),
582+
request: new Request(HOST_URL),
595583
})
596584

597585
const request = new Request(`${magicLink}`, {
598586
method: 'GET',
599-
headers: { host: HOST_URL },
600587
})
601588

602589
const strategy = new TOTPStrategy(
@@ -635,7 +622,6 @@ describe('[ TOTP ]', () => {
635622
const request = new Request(`${HOST_URL}`, {
636623
method: 'POST',
637624
headers: {
638-
host: HOST_URL,
639625
cookie: await sessionStorage.commitSession(session),
640626
},
641627
body: formData,
@@ -679,7 +665,6 @@ describe('[ TOTP ]', () => {
679665
const request = new Request(`${HOST_URL}`, {
680666
method: 'POST',
681667
headers: {
682-
host: HOST_URL,
683668
cookie: await sessionStorage.commitSession(session),
684669
},
685670
body: formData,
@@ -706,25 +691,27 @@ describe('[ TOTP ]', () => {
706691
})
707692

708693
describe('[ Utils ]', () => {
709-
test('Should properly use the HTTP protocol for local environments.', async () => {
710-
const request = new Request(`${HOST_URL}`)
711-
const samples: Array<[string, 'http:' | 'https:']> = [
712-
['127.0.0.1', 'http:'],
713-
['127.1.1.1', 'http:'],
714-
['127.0.0.1:8888', 'http:'],
715-
['localhost', 'http:'],
716-
['localhost:3000', 'http:'],
717-
['remix.run', 'https:'],
718-
['remix.run:3000', 'https:'],
719-
['local.com', 'https:'],
720-
['legit.local.com:3000', 'https:'],
721-
['remix-auth-otp.local', 'http:'],
722-
['remix-auth-otp.local:3000', 'http:'],
694+
test('Should use the origin from the request for the magic-link if hostUrl is not provided.', async () => {
695+
const samples: Array<[string, string]> = [
696+
['http://localhost/login', 'http://localhost/magic-link?code=U2N2EY'],
697+
['http://localhost:3000/login', 'http://localhost:3000/magic-link?code=U2N2EY'],
698+
['http://127.0.0.1/login', 'http://127.0.0.1/magic-link?code=U2N2EY'],
699+
['http://127.0.0.1:3000/login', 'http://127.0.0.1:3000/magic-link?code=U2N2EY'],
700+
['http://localhost:8788/signin', 'http://localhost:8788/magic-link?code=U2N2EY'],
701+
['https://host.com/login', 'https://host.com/magic-link?code=U2N2EY'],
702+
['https://host.com:3000/login', 'https://host.com:3000/magic-link?code=U2N2EY'],
723703
]
724704

725-
for (const [host, protocol] of samples) {
726-
request.headers.set('host', host)
727-
expect(getHostUrl(request).startsWith(protocol)).toBe(true)
705+
for (const [requestUrl, magicLinkUrl] of samples) {
706+
const request = new Request(requestUrl)
707+
expect(
708+
generateMagicLink({
709+
...MAGIC_LINK_GENERATION_DEFAULTS,
710+
param: 'code',
711+
code: 'U2N2EY',
712+
request,
713+
}),
714+
).toBe(magicLinkUrl)
728715
}
729716
})
730717
})

test/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as crypto from 'crypto'
1010
* Constants.
1111
*/
1212
export const SECRET_ENV = 'SECRET_ENV'
13-
export const HOST_URL = 'localhost:3000'
13+
export const HOST_URL = 'http://localhost:3000'
1414
export const DEFAULT_EMAIL = '[email protected]'
1515

1616
/**

0 commit comments

Comments
 (0)