|
| 1 | +import { HttpStatus } from '@nestjs/common'; |
| 2 | +import test from 'ava'; |
| 3 | +import { JwtScopesEnum } from '../../../src/entities/user/enums/jwt-scopes.enum.js'; |
| 4 | +import { assertTokenScopeAllowed } from '../../../src/entities/user/utils/assert-token-scope-allowed.js'; |
| 5 | +import { ExceptionsInternalCodes } from '../../../src/exceptions/custom-exceptions/custom-exceptions-internal-codes/exceptions-internal-codes.js'; |
| 6 | +import { EmailVerificationRequiredException } from '../../../src/exceptions/custom-exceptions/email-verification-required-exception.js'; |
| 7 | +import { TwoFaRequiredException } from '../../../src/exceptions/custom-exceptions/two-fa-required-exception.js'; |
| 8 | + |
| 9 | +// The single place every auth path asks "is this restricted token allowed here?". |
| 10 | +// A scope on a token means the session is limited to the flow that finishes it — |
| 11 | +// '2fa_enable' until the user enrols in 2FA, 'email_verify' until the user confirms |
| 12 | +// their email — so it must be refused everywhere except the routes that complete it. |
| 13 | + |
| 14 | +test('a token with no scope claim is allowed', (t) => { |
| 15 | + t.notThrows(() => assertTokenScopeAllowed(undefined)); |
| 16 | + t.notThrows(() => assertTokenScopeAllowed(null)); |
| 17 | + t.notThrows(() => assertTokenScopeAllowed([])); |
| 18 | +}); |
| 19 | + |
| 20 | +test("'2fa_enable' is refused when the caller allows no scopes", (t) => { |
| 21 | + const error = t.throws(() => assertTokenScopeAllowed([JwtScopesEnum.TWO_FA_ENABLE]), { |
| 22 | + instanceOf: TwoFaRequiredException, |
| 23 | + }); |
| 24 | + t.is(error.getStatus(), HttpStatus.BAD_REQUEST); |
| 25 | + t.is(error.internalCode, ExceptionsInternalCodes.TWO_FA_REQUIRED); |
| 26 | +}); |
| 27 | + |
| 28 | +test("'2fa_enable' is allowed when the caller allows it (2FA-enrolment routes)", (t) => { |
| 29 | + t.notThrows(() => assertTokenScopeAllowed([JwtScopesEnum.TWO_FA_ENABLE], [JwtScopesEnum.TWO_FA_ENABLE])); |
| 30 | +}); |
| 31 | + |
| 32 | +test("'email_verify' is refused when the caller allows no scopes", (t) => { |
| 33 | + const error = t.throws(() => assertTokenScopeAllowed([JwtScopesEnum.EMAIL_VERIFY]), { |
| 34 | + instanceOf: EmailVerificationRequiredException, |
| 35 | + }); |
| 36 | + t.is(error.getStatus(), HttpStatus.BAD_REQUEST); |
| 37 | + t.is(error.internalCode, ExceptionsInternalCodes.EMAIL_VERIFICATION_REQUIRED); |
| 38 | +}); |
| 39 | + |
| 40 | +test("'email_verify' is allowed when the caller allows it (the verify-code routes)", (t) => { |
| 41 | + t.notThrows(() => assertTokenScopeAllowed([JwtScopesEnum.EMAIL_VERIFY], [JwtScopesEnum.EMAIL_VERIFY])); |
| 42 | +}); |
| 43 | + |
| 44 | +test('allowing one scope does not allow a different one', (t) => { |
| 45 | + t.throws(() => assertTokenScopeAllowed([JwtScopesEnum.EMAIL_VERIFY], [JwtScopesEnum.TWO_FA_ENABLE]), { |
| 46 | + instanceOf: EmailVerificationRequiredException, |
| 47 | + }); |
| 48 | + t.throws(() => assertTokenScopeAllowed([JwtScopesEnum.TWO_FA_ENABLE], [JwtScopesEnum.EMAIL_VERIFY]), { |
| 49 | + instanceOf: TwoFaRequiredException, |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +test('a token carrying both scopes is refused until both are allowed', (t) => { |
| 54 | + const scopes = [JwtScopesEnum.TWO_FA_ENABLE, JwtScopesEnum.EMAIL_VERIFY]; |
| 55 | + t.throws(() => assertTokenScopeAllowed(scopes, [JwtScopesEnum.TWO_FA_ENABLE]), { |
| 56 | + instanceOf: EmailVerificationRequiredException, |
| 57 | + }); |
| 58 | + t.notThrows(() => assertTokenScopeAllowed(scopes, scopes)); |
| 59 | +}); |
| 60 | + |
| 61 | +// Forward compatibility: an unrecognized scope keeps the pre-existing behavior of |
| 62 | +// every call site (the old `includes(TWO_FA_ENABLE)` check ignored anything else), |
| 63 | +// so a token minted by a newer service never locks a user out of an older one. |
| 64 | +test('an unrecognized scope is ignored', (t) => { |
| 65 | + t.notThrows(() => assertTokenScopeAllowed(['some_future_scope' as JwtScopesEnum])); |
| 66 | +}); |
0 commit comments