|
| 1 | +import { describe, expect, it } from 'bun:test'; |
| 2 | + |
| 3 | +import { |
| 4 | + computeSessionExpiry, |
| 5 | + MAXIMUM_SESSION_DURATION_SECONDS, |
| 6 | + MINIMUM_SESSION_DURATION_SECONDS, |
| 7 | + toPositiveNumber, |
| 8 | +} from '../src/session-expiry'; |
| 9 | + |
| 10 | +const now = () => Math.floor(Date.now() / 1000); |
| 11 | + |
| 12 | +/** The expiry is computed against a clock read inside the function, so allow a little drift. */ |
| 13 | +const expectCloseTo = (actual: number, expected: number) => { |
| 14 | + expect(Math.abs(actual - expected)).toBeLessThanOrEqual(2); |
| 15 | +}; |
| 16 | + |
| 17 | +describe('computeSessionExpiry', () => { |
| 18 | + describe('with the ID token as the source', () => { |
| 19 | + it('uses the exp claim of the ID token', () => { |
| 20 | + const idTokenExp = now() + 3 * 60 * 60; |
| 21 | + expectCloseTo( |
| 22 | + computeSessionExpiry({ |
| 23 | + source: 'id_token', |
| 24 | + idTokenExp, |
| 25 | + accessTokenExpiresIn: 60, |
| 26 | + }), |
| 27 | + idTokenExp, |
| 28 | + ); |
| 29 | + }); |
| 30 | + |
| 31 | + it('ignores the expires_in of the token response', () => { |
| 32 | + const idTokenExp = now() + 3 * 60 * 60; |
| 33 | + expectCloseTo( |
| 34 | + computeSessionExpiry({ |
| 35 | + source: 'id_token', |
| 36 | + idTokenExp, |
| 37 | + accessTokenExpiresIn: MAXIMUM_SESSION_DURATION_SECONDS, |
| 38 | + }), |
| 39 | + idTokenExp, |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + it('falls back to the minimum duration when the exp claim is missing', () => { |
| 44 | + expectCloseTo( |
| 45 | + computeSessionExpiry({ |
| 46 | + source: 'id_token', |
| 47 | + idTokenExp: undefined, |
| 48 | + accessTokenExpiresIn: undefined, |
| 49 | + }), |
| 50 | + now() + MINIMUM_SESSION_DURATION_SECONDS, |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('raises a short-lived ID token up to the minimum duration', () => { |
| 55 | + expectCloseTo( |
| 56 | + computeSessionExpiry({ |
| 57 | + source: 'id_token', |
| 58 | + idTokenExp: now() + 60, |
| 59 | + accessTokenExpiresIn: undefined, |
| 60 | + }), |
| 61 | + now() + MINIMUM_SESSION_DURATION_SECONDS, |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('caps a long-lived ID token at the maximum duration', () => { |
| 66 | + expectCloseTo( |
| 67 | + computeSessionExpiry({ |
| 68 | + source: 'id_token', |
| 69 | + idTokenExp: now() + 365 * 24 * 60 * 60, |
| 70 | + accessTokenExpiresIn: undefined, |
| 71 | + }), |
| 72 | + now() + MAXIMUM_SESSION_DURATION_SECONDS, |
| 73 | + ); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('with the access token as the source', () => { |
| 78 | + it('counts expires_in from now', () => { |
| 79 | + const expiresIn = 3 * 60 * 60; |
| 80 | + expectCloseTo( |
| 81 | + computeSessionExpiry({ |
| 82 | + source: 'access_token', |
| 83 | + idTokenExp: undefined, |
| 84 | + accessTokenExpiresIn: expiresIn, |
| 85 | + }), |
| 86 | + now() + expiresIn, |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it('ignores the exp claim of the ID token', () => { |
| 91 | + const expiresIn = 3 * 60 * 60; |
| 92 | + expectCloseTo( |
| 93 | + computeSessionExpiry({ |
| 94 | + source: 'access_token', |
| 95 | + idTokenExp: now() + MAXIMUM_SESSION_DURATION_SECONDS, |
| 96 | + accessTokenExpiresIn: expiresIn, |
| 97 | + }), |
| 98 | + now() + expiresIn, |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('accepts expires_in sent as a string', () => { |
| 103 | + expectCloseTo( |
| 104 | + computeSessionExpiry({ |
| 105 | + source: 'access_token', |
| 106 | + idTokenExp: undefined, |
| 107 | + accessTokenExpiresIn: '7200', |
| 108 | + }), |
| 109 | + now() + 7200, |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it('falls back to the minimum duration when expires_in is absent', () => { |
| 114 | + expectCloseTo( |
| 115 | + computeSessionExpiry({ |
| 116 | + source: 'access_token', |
| 117 | + idTokenExp: now() + 3 * 60 * 60, |
| 118 | + accessTokenExpiresIn: undefined, |
| 119 | + }), |
| 120 | + now() + MINIMUM_SESSION_DURATION_SECONDS, |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + it('falls back to the minimum duration when expires_in is not a usable number', () => { |
| 125 | + for (const expiresIn of [0, -60, 'soon', Number.NaN, null, {}]) { |
| 126 | + expectCloseTo( |
| 127 | + computeSessionExpiry({ |
| 128 | + source: 'access_token', |
| 129 | + idTokenExp: undefined, |
| 130 | + accessTokenExpiresIn: expiresIn, |
| 131 | + }), |
| 132 | + now() + MINIMUM_SESSION_DURATION_SECONDS, |
| 133 | + ); |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + it('raises a short-lived access token up to the minimum duration', () => { |
| 138 | + expectCloseTo( |
| 139 | + computeSessionExpiry({ |
| 140 | + source: 'access_token', |
| 141 | + idTokenExp: undefined, |
| 142 | + accessTokenExpiresIn: 300, |
| 143 | + }), |
| 144 | + now() + MINIMUM_SESSION_DURATION_SECONDS, |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it('caps a long-lived access token at the maximum duration', () => { |
| 149 | + expectCloseTo( |
| 150 | + computeSessionExpiry({ |
| 151 | + source: 'access_token', |
| 152 | + idTokenExp: undefined, |
| 153 | + accessTokenExpiresIn: 30 * 24 * 60 * 60, |
| 154 | + }), |
| 155 | + now() + MAXIMUM_SESSION_DURATION_SECONDS, |
| 156 | + ); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
| 161 | +describe('toPositiveNumber', () => { |
| 162 | + it('accepts positive numbers and numeric strings', () => { |
| 163 | + expect(toPositiveNumber(3600)).toBe(3600); |
| 164 | + expect(toPositiveNumber('3600')).toBe(3600); |
| 165 | + expect(toPositiveNumber(0.5)).toBe(0.5); |
| 166 | + }); |
| 167 | + |
| 168 | + it('rejects anything that is not a usable positive number', () => { |
| 169 | + for (const value of [ |
| 170 | + 0, |
| 171 | + '0', |
| 172 | + -1, |
| 173 | + '-1', |
| 174 | + '', |
| 175 | + ' ', |
| 176 | + 'abc', |
| 177 | + Number.NaN, |
| 178 | + Number.POSITIVE_INFINITY, |
| 179 | + null, |
| 180 | + undefined, |
| 181 | + {}, |
| 182 | + [], |
| 183 | + ]) { |
| 184 | + expect(toPositiveNumber(value)).toBeUndefined(); |
| 185 | + } |
| 186 | + }); |
| 187 | +}); |
0 commit comments