This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle onSessionExpired, add isAuthenticated option, expose Tok…
…enManager (#648)
- Loading branch information
1 parent
c27670c
commit 40155af
Showing
36 changed files
with
1,301 additions
and
1,235 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
15 changes: 0 additions & 15 deletions
15
packages/okta-angular/src/@types/okta__okta-auth-js/index.d.ts
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
17 changes: 0 additions & 17 deletions
17
packages/okta-angular/src/okta/models/auth-required-function.ts
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
import { UserClaims } from './user-claims'; | ||
|
||
export interface AccessToken { | ||
accessToken: string; | ||
} | ||
|
||
export interface IDToken { | ||
idToken: string; | ||
claims: UserClaims; | ||
} | ||
|
||
export interface TokenManager { | ||
get(key: string): AccessToken | IDToken; | ||
add(key: string, token: AccessToken | IDToken): void; | ||
on(event: string, handler: Function): void; | ||
off(event: string, handler: Function): void; | ||
} |
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,131 @@ | ||
jest.mock('@okta/okta-auth-js'); | ||
|
||
import { TestBed } from '@angular/core/testing'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import OktaAuth from '@okta/okta-auth-js'; | ||
|
||
import { | ||
OktaAuthModule, | ||
OktaAuthService, | ||
OktaAuthGuard, | ||
} from '../../src/okta-angular'; | ||
import { ActivatedRouteSnapshot, RouterStateSnapshot, Router, RouterState } from '@angular/router'; | ||
|
||
const VALID_CONFIG = { | ||
clientId: 'foo', | ||
issuer: 'https://foo', | ||
redirectUri: 'https://foo' | ||
}; | ||
|
||
function createService(options: any) { | ||
options = options || {}; | ||
|
||
const oktaAuth = options.oktaAuth || {}; | ||
oktaAuth.tokenManager = oktaAuth.tokenManager || { on: jest.fn() }; | ||
OktaAuth.mockImplementation(() => oktaAuth); | ||
|
||
TestBed.configureTestingModule({ | ||
imports: [ | ||
RouterTestingModule.withRoutes([{ path: 'foo', redirectTo: '/foo' }]), | ||
OktaAuthModule.initAuth(VALID_CONFIG) | ||
], | ||
providers: [OktaAuthService], | ||
}); | ||
const service = TestBed.get(OktaAuthService); | ||
service.getTokenManager = jest.fn().mockReturnValue({ on: jest.fn() }); | ||
service.isAuthenticated = jest.fn().mockReturnValue(Promise.resolve(options.isAuthenticated)); | ||
service.setFromUri = jest.fn(); | ||
service.loginRedirect = jest.fn(); | ||
return service; | ||
} | ||
|
||
describe('Angular auth guard', () => { | ||
|
||
beforeEach(() => { | ||
OktaAuth.mockClear(); | ||
}); | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('canActivate', () => { | ||
describe('isAuthenticated() = true', () => { | ||
it('returns true', async () => { | ||
const service = createService({ isAuthenticated: true }); | ||
const router: unknown = undefined; | ||
const guard = new OktaAuthGuard(service, router as Router); | ||
const route: unknown = undefined; | ||
const state: unknown = undefined; | ||
const res = await guard.canActivate(route as ActivatedRouteSnapshot, state as RouterStateSnapshot); | ||
expect(res).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('isAuthenticated() = false', () => { | ||
let service: OktaAuthService; | ||
let guard: OktaAuthGuard; | ||
let state: RouterStateSnapshot; | ||
let route: ActivatedRouteSnapshot; | ||
let router: Router; | ||
beforeEach(() => { | ||
service = createService({ isAuthenticated: false }); | ||
router = TestBed.get(Router); | ||
guard = new OktaAuthGuard(service, router); | ||
const routerState: RouterState = router.routerState; | ||
state = routerState.snapshot; | ||
route = state.root; | ||
}); | ||
|
||
it('returns false', async () => { | ||
const config = service.getOktaConfig(); | ||
const res = await guard.canActivate(route, state); | ||
expect(res).toBe(false); | ||
}); | ||
|
||
it('by default, calls "loginRedirect()"', async () => { | ||
const config = service.getOktaConfig(); | ||
const res = await guard.canActivate(route, state); | ||
expect(service.loginRedirect).toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls "setFromUri" with baseUrl and query object', async () => { | ||
const baseUrl = 'http://fake.url/path'; | ||
const query = '?query=foo&bar=baz'; | ||
const hash = '#hash=foo'; | ||
state.url = `${baseUrl}${query}${hash}`; | ||
const queryObj = { 'bar': 'baz' }; | ||
route.queryParams = queryObj; | ||
const res = await guard.canActivate(route, state); | ||
expect(service.setFromUri).toHaveBeenCalledWith(baseUrl, queryObj); | ||
}); | ||
|
||
it('onAuthRequired can be set on route', async () => { | ||
const fn = route.data['onAuthRequired'] = jest.fn(); | ||
const res = await guard.canActivate(route, state); | ||
expect(fn).toHaveBeenCalledWith(service, router as Router); | ||
}); | ||
|
||
it('onAuthRequired can be set on config', async () => { | ||
const config = service.getOktaConfig(); | ||
const fn = config.onAuthRequired = jest.fn(); | ||
|
||
const res = await guard.canActivate(route, state); | ||
expect(fn).toHaveBeenCalledWith(service, router as Router); | ||
}); | ||
}); | ||
}); | ||
|
||
it('Can create the guard via angular injection', () => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
RouterTestingModule.withRoutes([{ path: 'foo', redirectTo: '/foo' }]), | ||
OktaAuthModule.initAuth(VALID_CONFIG) | ||
], | ||
providers: [OktaAuthService, OktaAuthGuard], | ||
}); | ||
const guard = TestBed.get(OktaAuthGuard); | ||
expect(guard.oktaAuth).toBeTruthy(); | ||
expect(guard.router).toBeTruthy(); | ||
expect(typeof guard.canActivate).toBe('function'); | ||
}); | ||
}); |
Oops, something went wrong.