@@ -2,7 +2,7 @@ import { describe, test, expect, afterEach, vi } from 'vitest'
22import { AuthorizationError } from 'remix-auth'
33
44import { TOTPStrategy } from '../src/index'
5- import { generateTOTP , generateMagicLink , getHostUrl , signJWT } from '../src/utils'
5+ import { generateTOTP , generateMagicLink , signJWT } from '../src/utils'
66import { STRATEGY_NAME , FORM_FIELDS , SESSION_KEYS , ERRORS } from '../src/constants'
77
88import {
@@ -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
708693describe ( '[ 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} )
0 commit comments