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: Expose TokenManager, handle token error events
- Loading branch information
1 parent
e731e7f
commit 43517ea
Showing
21 changed files
with
1,203 additions
and
256 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"protocol": "auto", | ||
"name": "okta-react Jest", | ||
"cwd": "${workspaceFolder}/packages/okta-react", | ||
"args": [ | ||
"node_modules/.bin/jest", | ||
"--runInBand", | ||
"--no-cache", | ||
"${workspaceFolder}/${relativeFile}" | ||
], | ||
"sourceMaps": true, | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen", | ||
"env": { | ||
|
||
}, | ||
"sourceMapPathOverrides": { | ||
"webpack:///*": "/*", | ||
}, | ||
"disableOptimisticBPs": true | ||
}, | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"protocol": "auto", | ||
"name": "okta-angular Jest", | ||
"cwd": "${workspaceFolder}/packages/okta-angular", | ||
"args": [ | ||
"node_modules/.bin/jest", | ||
"--runInBand", | ||
"--no-cache", | ||
"${workspaceFolder}/${relativeFile}" | ||
], | ||
"sourceMaps": true, | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen", | ||
"env": { | ||
|
||
}, | ||
"sourceMapPathOverrides": { | ||
"webpack:///*": "/*", | ||
}, | ||
"disableOptimisticBPs": true | ||
}, | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"protocol": "auto", | ||
"name": "okta-vue Jest", | ||
"cwd": "${workspaceFolder}/packages/okta-vue", | ||
"args": [ | ||
"node_modules/.bin/jest", | ||
"--runInBand", | ||
"--no-cache", | ||
"${workspaceFolder}/${relativeFile}" | ||
], | ||
"sourceMaps": true, | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen", | ||
"env": { | ||
|
||
}, | ||
"sourceMapPathOverrides": { | ||
"webpack:///*": "/*", | ||
}, | ||
"disableOptimisticBPs": true | ||
}, | ||
] | ||
} |
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
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,3 @@ | ||
export interface TokenManager { | ||
on: Function; | ||
} |
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,128 @@ | ||
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'; | ||
import { ActivatedRouteSnapshot, RouterStateSnapshot, Router, RouterState } from '@angular/router'; | ||
|
||
const VALID_CONFIG = { | ||
clientId: 'foo', | ||
issuer: 'https://foo', | ||
redirectUri: 'https://foo' | ||
}; | ||
|
||
function createService(options) { | ||
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 guard = new OktaAuthGuard(service, null); | ||
const res = await guard.canActivate(null, null); | ||
expect(res).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('isAuthenticated() = false', () => { | ||
let service: OktaAuthService; | ||
let guard: OktaAuthGuard; | ||
let state: RouterStateSnapshot; | ||
let route: ActivatedRouteSnapshot; | ||
let 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); | ||
}); | ||
|
||
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); | ||
}); | ||
}); | ||
}); | ||
|
||
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.