-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Custom session timeout and refresh configuration (#8342)
- Loading branch information
1 parent
f4f496a
commit 07e6705
Showing
9 changed files
with
299 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Container } from 'typedi'; | ||
import { mock } from 'jest-mock-extended'; | ||
|
||
import config from '@/config'; | ||
import { JwtService } from '@/services/jwt.service'; | ||
import { License } from '@/License'; | ||
import { Time } from '@/constants'; | ||
import { issueJWT } from '@/auth/jwt'; | ||
|
||
import { mockInstance } from '../../shared/mocking'; | ||
|
||
import type { User } from '@db/entities/User'; | ||
|
||
mockInstance(License); | ||
|
||
describe('jwt.issueJWT', () => { | ||
const jwtService = Container.get(JwtService); | ||
|
||
describe('when not setting userManagement.jwtSessionDuration', () => { | ||
it('should default to expire in 7 days', () => { | ||
const defaultInSeconds = 7 * Time.days.toSeconds; | ||
const mockUser = mock<User>({ password: 'passwordHash' }); | ||
const { token, expiresIn } = issueJWT(mockUser); | ||
|
||
expect(expiresIn).toBe(defaultInSeconds); | ||
const decodedToken = jwtService.verify(token); | ||
if (decodedToken.exp === undefined || decodedToken.iat === undefined) { | ||
fail('Expected exp and iat to be defined'); | ||
} | ||
|
||
expect(decodedToken.exp - decodedToken.iat).toBe(defaultInSeconds); | ||
}); | ||
}); | ||
|
||
describe('when setting userManagement.jwtSessionDuration', () => { | ||
const oldDuration = config.get('userManagement.jwtSessionDurationHours'); | ||
const testDurationHours = 1; | ||
const testDurationSeconds = testDurationHours * Time.hours.toSeconds; | ||
|
||
beforeEach(() => { | ||
mockInstance(License); | ||
config.set('userManagement.jwtSessionDurationHours', testDurationHours); | ||
}); | ||
|
||
afterEach(() => { | ||
config.set('userManagement.jwtSessionDuration', oldDuration); | ||
}); | ||
|
||
it('should apply it to tokens', () => { | ||
const mockUser = mock<User>({ password: 'passwordHash' }); | ||
const { token, expiresIn } = issueJWT(mockUser); | ||
|
||
expect(expiresIn).toBe(testDurationSeconds); | ||
const decodedToken = jwtService.verify(token); | ||
if (decodedToken.exp === undefined || decodedToken.iat === undefined) { | ||
fail('Expected exp and iat to be defined on decodedToken'); | ||
} | ||
expect(decodedToken.exp - decodedToken.iat).toBe(testDurationSeconds); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
describe('userManagement.jwtRefreshTimeoutHours', () => { | ||
it("resets jwtRefreshTimeoutHours to 0 if it's greater than or equal to jwtSessionDurationHours", async () => { | ||
process.env.N8N_USER_MANAGEMENT_JWT_DURATION_HOURS = '1'; | ||
process.env.N8N_USER_MANAGEMENT_JWT_REFRESH_TIMEOUT_HOURS = '1'; | ||
const { default: config } = await import('@/config'); | ||
|
||
expect(config.getEnv('userManagement.jwtRefreshTimeoutHours')).toBe(0); | ||
}); | ||
}); |
Oops, something went wrong.