-
Notifications
You must be signed in to change notification settings - Fork 93
✨(frontend) store and retrieve user token in sessionStorage #2724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kernicPanel
wants to merge
6
commits into
master
Choose a base branch
from
keycloak
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+346
−25
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aeea4f5
✨(frontend) store and retrieve user token in sessionStorage
kernicPanel ccb9f6a
✨(frontend) adapt personal information verification for Keycloak
kernicPanel 6918de6
✨(backend) add referrer info to Keycloak account URL
kernicPanel a223b6a
✨(frontend) enhance Keycloak initialization and token handling
kernicPanel fdef347
🔥(backend) remove unused Keycloak token configuration
kernicPanel 70ba44b
🐛(frontend) enhances Keycloak token expiration handling
kernicPanel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,17 +1,33 @@ | ||
| import { RichieContextFactory as mockRichieContextFactory } from 'utils/test/factories/richie'; | ||
| import { KeycloakAccountApi } from 'types/api'; | ||
| import API from './keycloak'; | ||
|
|
||
| const mockKeycloakInit = jest.fn().mockResolvedValue(true); | ||
| const mockKeycloakLogout = jest.fn().mockResolvedValue(undefined); | ||
| const mockKeycloakLogin = jest.fn().mockResolvedValue(undefined); | ||
| const mockKeycloakLoadUserProfile = jest.fn(); | ||
| const mockKeycloakUpdateToken = jest.fn().mockResolvedValue(true); | ||
| const mockKeycloakCreateAccountUrl = jest | ||
| .fn() | ||
| .mockReturnValue('https://keycloak.test/auth/realms/richie-realm/account'); | ||
| const mockIdToken = 'mock-id-token-12345'; | ||
| const mockIdTokenParsed = { | ||
| preferred_username: 'johndoe', | ||
| firstName: 'John', | ||
| lastName: 'Doe', | ||
| email: '[email protected]', | ||
| }; | ||
|
|
||
| jest.mock('keycloak-js', () => { | ||
| return jest.fn().mockImplementation(() => ({ | ||
| init: mockKeycloakInit, | ||
| logout: mockKeycloakLogout, | ||
| login: mockKeycloakLogin, | ||
| loadUserProfile: mockKeycloakLoadUserProfile, | ||
| updateToken: mockKeycloakUpdateToken, | ||
| createAccountUrl: mockKeycloakCreateAccountUrl, | ||
| idToken: mockIdToken, | ||
| idTokenParsed: mockIdTokenParsed, | ||
| })); | ||
| }); | ||
|
|
||
|
|
@@ -50,28 +66,54 @@ describe('Keycloak API', () => { | |
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| sessionStorage.clear(); | ||
| keycloakApi = API(authConfig); | ||
| }); | ||
|
|
||
| describe('user.accessToken', () => { | ||
| it('returns null when no token is stored', () => { | ||
| const token = keycloakApi.user.accessToken!(); | ||
| expect(token).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns the token from sessionStorage', () => { | ||
| sessionStorage.setItem('RICHIE_USER_TOKEN', mockIdToken); | ||
| const token = keycloakApi.user.accessToken!(); | ||
| expect(token).toEqual(mockIdToken); | ||
| }); | ||
| }); | ||
|
|
||
| describe('user.me', () => { | ||
| it('returns null when updateToken fails', async () => { | ||
| mockKeycloakUpdateToken.mockRejectedValueOnce(new Error('Token refresh failed')); | ||
| const response = await keycloakApi.user.me(); | ||
| expect(response).toBeNull(); | ||
| expect(mockKeycloakLoadUserProfile).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('returns null when loadUserProfile fails', async () => { | ||
| mockKeycloakUpdateToken.mockResolvedValueOnce(true); | ||
| mockKeycloakLoadUserProfile.mockRejectedValueOnce(new Error('Not authenticated')); | ||
| const response = await keycloakApi.user.me(); | ||
| expect(response).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns user when loadUserProfile succeeds', async () => { | ||
| mockKeycloakUpdateToken.mockResolvedValueOnce(true); | ||
| mockKeycloakLoadUserProfile.mockResolvedValueOnce({ | ||
| firstName: 'John', | ||
| lastName: 'Doe', | ||
| email: '[email protected]', | ||
| }); | ||
|
|
||
| const response = await keycloakApi.user.me(); | ||
| expect(mockKeycloakUpdateToken).toHaveBeenCalledWith(30); | ||
| expect(response).toEqual({ | ||
| username: 'John Doe', | ||
| email: '[email protected]', | ||
| access_token: mockIdToken, | ||
| }); | ||
| expect(sessionStorage.getItem('RICHIE_USER_TOKEN')).toEqual(mockIdToken); | ||
kernicPanel marked this conversation as resolved.
Show resolved
Hide resolved
kernicPanel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| }); | ||
|
|
||
|
|
@@ -106,6 +148,24 @@ describe('Keycloak API', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('user.account', () => { | ||
| it('returns profile data from idTokenParsed via account.get()', () => { | ||
| const profile = (keycloakApi.user.account as KeycloakAccountApi).get(); | ||
| expect(profile).toEqual({ | ||
| username: 'johndoe', | ||
| firstName: 'John', | ||
| lastName: 'Doe', | ||
| email: '[email protected]', | ||
| }); | ||
| }); | ||
|
|
||
| it('returns the account management URL via account.updateUrl()', () => { | ||
| const url = (keycloakApi.user.account as any).updateUrl(); | ||
| expect(url).toBe('https://keycloak.test/auth/realms/richie-realm/account'); | ||
| expect(mockKeycloakCreateAccountUrl).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Keycloak initialization', () => { | ||
| it('initializes keycloak with correct configuration', () => { | ||
| const Keycloak = require('keycloak-js'); | ||
|
|
@@ -118,8 +178,9 @@ describe('Keycloak API', () => { | |
|
|
||
| expect(mockKeycloakInit).toHaveBeenCalledWith({ | ||
| checkLoginIframe: false, | ||
| flow: 'implicit', | ||
| token: undefined, | ||
| flow: 'standard', | ||
| onLoad: 'check-sso', | ||
| pkceMethod: 'S256', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.