|
| 1 | +/* |
| 2 | + * @adonisjs/persona |
| 3 | + * |
| 4 | + * (C) AdonisJS |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +import timekeeper from 'timekeeper' |
| 11 | +import { Secret, base64 } from '@poppinss/utils' |
| 12 | +import { getActiveTest, test } from '@japa/runner' |
| 13 | + |
| 14 | +import { VerificationToken } from '../src/helpers/verification_token.js' |
| 15 | + |
| 16 | +function freezeTime() { |
| 17 | + const t = getActiveTest() |
| 18 | + if (!t) { |
| 19 | + throw new Error('Cannot use "freezeTime" outside of a Japa test') |
| 20 | + } |
| 21 | + |
| 22 | + timekeeper.reset() |
| 23 | + |
| 24 | + const date = new Date() |
| 25 | + timekeeper.freeze(date) |
| 26 | + |
| 27 | + t.cleanup(() => { |
| 28 | + timekeeper.reset() |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +class EmailVerificationToken extends VerificationToken { |
| 33 | + constructor(props: { |
| 34 | + identifier: number |
| 35 | + tokenableId: number |
| 36 | + hash: string |
| 37 | + expiresAt: Date |
| 38 | + secret?: Secret<string> |
| 39 | + }) { |
| 40 | + super() |
| 41 | + this.identifier = props.identifier |
| 42 | + this.tokenableId = props.tokenableId |
| 43 | + this.hash = props.hash |
| 44 | + this.expiresAt = props.expiresAt |
| 45 | + if (props.secret) { |
| 46 | + this.computeValue(props.secret) |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +test.group('VerificationToken token | decode', () => { |
| 52 | + test('decode "{input}" as token') |
| 53 | + .with([ |
| 54 | + { |
| 55 | + input: null, |
| 56 | + output: null, |
| 57 | + }, |
| 58 | + { |
| 59 | + input: '', |
| 60 | + output: null, |
| 61 | + }, |
| 62 | + { |
| 63 | + input: '..', |
| 64 | + output: null, |
| 65 | + }, |
| 66 | + { |
| 67 | + input: 'foobar', |
| 68 | + output: null, |
| 69 | + }, |
| 70 | + { |
| 71 | + input: 'foo.baz', |
| 72 | + output: null, |
| 73 | + }, |
| 74 | + { |
| 75 | + input: `bar.${base64.urlEncode('baz')}`, |
| 76 | + output: null, |
| 77 | + }, |
| 78 | + { |
| 79 | + input: `${base64.urlEncode('baz')}.bar`, |
| 80 | + output: null, |
| 81 | + }, |
| 82 | + { |
| 83 | + input: `${base64.urlEncode('bar')}.${base64.urlEncode('baz')}`, |
| 84 | + output: { |
| 85 | + identifier: 'bar', |
| 86 | + secret: 'baz', |
| 87 | + }, |
| 88 | + }, |
| 89 | + ]) |
| 90 | + .run(({ assert }, { input, output }) => { |
| 91 | + const decoded = VerificationToken.decode(input as string) |
| 92 | + if (!decoded) { |
| 93 | + assert.deepEqual(decoded, output) |
| 94 | + } else { |
| 95 | + assert.deepEqual( |
| 96 | + { identifier: decoded.identifier, secret: decoded.secret.release() }, |
| 97 | + output |
| 98 | + ) |
| 99 | + } |
| 100 | + }) |
| 101 | +}) |
| 102 | + |
| 103 | +test.group('VerificationToken token | create', () => { |
| 104 | + test('create a transient token', ({ assert }) => { |
| 105 | + freezeTime() |
| 106 | + const date = new Date() |
| 107 | + const expiresAt = new Date() |
| 108 | + expiresAt.setSeconds(date.getSeconds() + 60 * 20) |
| 109 | + |
| 110 | + const token = VerificationToken.createTransientToken(1, 40, '20 mins') |
| 111 | + assert.equal(token.userId, 1) |
| 112 | + assert.exists(token.hash) |
| 113 | + assert.equal(token.expiresAt!.getTime(), expiresAt.getTime()) |
| 114 | + assert.instanceOf(token.secret, Secret) |
| 115 | + }) |
| 116 | + |
| 117 | + test('create token from persisted information', ({ assert }) => { |
| 118 | + const createdAt = new Date() |
| 119 | + const expiresAt = new Date() |
| 120 | + expiresAt.setSeconds(createdAt.getSeconds() + 60 * 20) |
| 121 | + |
| 122 | + const token = new EmailVerificationToken({ |
| 123 | + identifier: 12, |
| 124 | + tokenableId: 1, |
| 125 | + hash: '1234', |
| 126 | + expiresAt, |
| 127 | + }) |
| 128 | + |
| 129 | + assert.equal(token.identifier, 12) |
| 130 | + assert.equal(token.hash, '1234') |
| 131 | + assert.equal(token.tokenableId, 1) |
| 132 | + assert.equal(token.expiresAt!.getTime(), expiresAt.getTime()) |
| 133 | + |
| 134 | + assert.isUndefined(token.value) |
| 135 | + assert.isFalse(token.isExpired()) |
| 136 | + }) |
| 137 | + |
| 138 | + test('create token with a secret', ({ assert }) => { |
| 139 | + const createdAt = new Date() |
| 140 | + const expiresAt = new Date() |
| 141 | + expiresAt.setSeconds(createdAt.getSeconds() + 60 * 20) |
| 142 | + |
| 143 | + const transientToken = EmailVerificationToken.createTransientToken(1, 40, '20 mins') |
| 144 | + |
| 145 | + const token = new EmailVerificationToken({ |
| 146 | + identifier: 12, |
| 147 | + tokenableId: 1, |
| 148 | + hash: transientToken.hash, |
| 149 | + expiresAt, |
| 150 | + secret: transientToken.secret, |
| 151 | + }) |
| 152 | + |
| 153 | + const decoded = EmailVerificationToken.decode(token.value!.release()) |
| 154 | + |
| 155 | + assert.equal(token.identifier, 12) |
| 156 | + assert.equal(token.tokenableId, 1) |
| 157 | + assert.equal(token.hash, transientToken.hash) |
| 158 | + assert.instanceOf(token.value, Secret) |
| 159 | + assert.isTrue(token.verify(transientToken.secret)) |
| 160 | + assert.isTrue(token.verify(decoded!.secret)) |
| 161 | + assert.equal(token.expiresAt!.getTime(), expiresAt.getTime()) |
| 162 | + assert.isFalse(token.isExpired()) |
| 163 | + }) |
| 164 | + |
| 165 | + test('verify token hash', ({ assert }) => { |
| 166 | + const transientToken = EmailVerificationToken.createTransientToken(1, 40, '20 mins') |
| 167 | + |
| 168 | + const token = new EmailVerificationToken({ |
| 169 | + identifier: 12, |
| 170 | + tokenableId: 1, |
| 171 | + hash: transientToken.hash, |
| 172 | + expiresAt: new Date(), |
| 173 | + secret: transientToken.secret, |
| 174 | + }) |
| 175 | + |
| 176 | + assert.isTrue(token.verify(transientToken.secret)) |
| 177 | + }) |
| 178 | +}) |
0 commit comments