forked from AxaFrance/oidc-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor (oidcServiceWorker): Extract GetCurrentDatabaseTokenEndpoint…
…, add tests.
- Loading branch information
Showing
3 changed files
with
170 additions
and
21 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
152 changes: 152 additions & 0 deletions
152
packages/oidc-client-service-worker/src/__tests__/oidcConfig.spec.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { getCurrentDatabasesTokenEndpoint } from '../oidcConfig' | ||
import { Database } from '../types' | ||
|
||
const oidcConfigDefaults = { | ||
demonstratingProofOfPossessionConfiguration: null, | ||
configurationName: '', | ||
tokens: null, | ||
status: null, | ||
state: null, | ||
codeVerifier: null, | ||
nonce: null, | ||
hideAccessToken: false, | ||
convertAllRequestsToCorsExceptNavigate: true, | ||
setAccessTokenToNavigateRequests: true, | ||
demonstratingProofOfPossessionNonce: null, | ||
demonstratingProofOfPossessionJwkJson: null, | ||
demonstratingProofOfPossessionOnlyWhenDpopHeaderPresent: false, | ||
} | ||
|
||
const oidcServerConfigDefault = { | ||
revocationEndpoint: '', | ||
tokenEndpoint: '', | ||
issuer: '', | ||
userInfoEndpoint: '', | ||
authorizationEndpoint: '' | ||
} | ||
|
||
describe('getCurrentDatabasesTokenEndpoint', () => { | ||
it('should return configs with matching token endpoint', () => { | ||
const database: Database = { | ||
config1: { | ||
...oidcConfigDefaults, | ||
oidcServerConfiguration: { | ||
...oidcServerConfigDefault, | ||
tokenEndpoint: 'https://example.com/token', | ||
}, | ||
}, | ||
config2: { | ||
...oidcConfigDefaults, | ||
oidcServerConfiguration: { | ||
...oidcServerConfigDefault, | ||
tokenEndpoint: 'https://example.org/token', | ||
}, | ||
}, | ||
config3: { | ||
...oidcConfigDefaults, | ||
oidcServerConfiguration: { | ||
...oidcServerConfigDefault, | ||
revocationEndpoint: 'https://example.net/revoke', | ||
}, | ||
}, | ||
} | ||
|
||
const url = 'https://example.com/token' | ||
const result = getCurrentDatabasesTokenEndpoint(database, url) | ||
|
||
expect(result).toHaveLength(1) | ||
expect(result[0]).toBe(database.config1) | ||
}) | ||
|
||
it('should return configs with matching revocation endpoint', () => { | ||
const database = { | ||
config1: { | ||
...oidcConfigDefaults, | ||
oidcServerConfiguration: { | ||
...oidcServerConfigDefault, | ||
revocationEndpoint: 'https://example.com/revoke', | ||
}, | ||
}, | ||
config2: { | ||
...oidcConfigDefaults, | ||
oidcServerConfiguration: { | ||
...oidcServerConfigDefault, | ||
revocationEndpoint: 'https://example.org/revoke', | ||
}, | ||
}, | ||
config3: { | ||
...oidcConfigDefaults, | ||
oidcServerConfiguration: { | ||
...oidcServerConfigDefault, | ||
tokenEndpoint: 'https://example.net/token', | ||
}, | ||
}, | ||
} | ||
|
||
const url = 'https://example.com/revoke' | ||
const result = getCurrentDatabasesTokenEndpoint(database, url) | ||
|
||
expect(result).toHaveLength(1) | ||
expect(result[0]).toBe(database.config1) | ||
}) | ||
|
||
it('should return multiple matching configs', () => { | ||
const database = { | ||
config1: { | ||
...oidcConfigDefaults, | ||
oidcServerConfiguration: { | ||
...oidcServerConfigDefault, | ||
tokenEndpoint: 'https://example.com/token', | ||
revocationEndpoint: 'https://example.com/revoke', | ||
}, | ||
}, | ||
config2: { | ||
...oidcConfigDefaults, | ||
oidcServerConfiguration: { | ||
...oidcServerConfigDefault, | ||
tokenEndpoint: 'https://example.org/token', | ||
}, | ||
}, | ||
config3: { | ||
...oidcConfigDefaults, | ||
oidcServerConfiguration: { | ||
...oidcServerConfigDefault, | ||
tokenEndpoint: 'https://example.com/token', | ||
revocationEndpoint: 'https://example.com/revoke', | ||
}, | ||
}, | ||
} | ||
|
||
const url = 'https://example.com/token' | ||
const result = getCurrentDatabasesTokenEndpoint(database, url) | ||
|
||
expect(result).toHaveLength(2) | ||
expect(result).toContain(database.config1) | ||
expect(result).toContain(database.config3) | ||
}) | ||
|
||
it('should return empty array for no matching configs', () => { | ||
const database = { | ||
config1: { | ||
...oidcConfigDefaults, | ||
oidcServerConfiguration: { | ||
...oidcServerConfigDefault, | ||
tokenEndpoint: 'https://example.com/token', | ||
}, | ||
}, | ||
config2: { | ||
...oidcConfigDefaults, | ||
oidcServerConfiguration: { | ||
...oidcServerConfigDefault, | ||
revocationEndpoint: 'https://example.org/revoke', | ||
}, | ||
}, | ||
} | ||
|
||
const url = 'https://example.net/other' | ||
const result = getCurrentDatabasesTokenEndpoint(database, url) | ||
|
||
expect(result).toHaveLength(0) | ||
}) | ||
}) |
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 { Database, OidcConfig } from './types'; | ||
import { normalizeUrl } from './utils'; | ||
|
||
const getMatchingOidcConfigurations = (database: Database, url: string): OidcConfig[] => { | ||
return Object.values(database).filter((config) => { | ||
const { oidcServerConfiguration } = config || {}; | ||
const { tokenEndpoint, revocationEndpoint } = oidcServerConfiguration || {}; | ||
|
||
const normalizedUrl = normalizeUrl(url); | ||
return ( | ||
(tokenEndpoint && normalizedUrl.startsWith(normalizeUrl(tokenEndpoint))) || | ||
(revocationEndpoint && normalizedUrl.startsWith(normalizeUrl(revocationEndpoint))) | ||
); | ||
}); | ||
}; | ||
|
||
export { getMatchingOidcConfigurations as getCurrentDatabasesTokenEndpoint }; |