Skip to content

Commit

Permalink
refactor(oidcServiceWorker): Extract GetCurrentDatabaseTokenEndpoint,…
Browse files Browse the repository at this point in the history
… add tests. (#1405) (release)
  • Loading branch information
jafin committed Jul 10, 2024
1 parent e51aef2 commit 73eae7e
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 21 deletions.
22 changes: 1 addition & 21 deletions packages/oidc-client-service-worker/src/OidcServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import version from './version';
import {generateJwkAsync, generateJwtDemonstratingProofOfPossessionAsync} from "./jwt";
import {getDpopConfiguration, getDpopOnlyWhenDpopHeaderPresent} from "./dpop";
import {base64urlOfHashOfASCIIEncodingAsync} from "./crypto";
import { getCurrentDatabasesTokenEndpoint } from './oidcConfig';

// @ts-ignore
if (typeof trustedTypes !== 'undefined' && typeof trustedTypes.createPolicy == 'function') {
Expand Down Expand Up @@ -56,27 +57,6 @@ const handleActivate = (event: ExtendableEvent) => {

const database: Database = {};

const getCurrentDatabasesTokenEndpoint = (database: Database, url: string) => {
const databases: OidcConfig[] = [];
for (const [, value] of Object.entries<OidcConfig>(database)) {
if (
value.oidcServerConfiguration != null &&
url.startsWith(normalizeUrl(value.oidcServerConfiguration.tokenEndpoint))
) {
databases.push(value);
} else if (
value.oidcServerConfiguration != null &&
value.oidcServerConfiguration.revocationEndpoint &&
url.startsWith(
normalizeUrl(value.oidcServerConfiguration.revocationEndpoint),
)
) {
databases.push(value);
}
}
return databases;
};

const keepAliveAsync = async (event: FetchEvent) => {
const originalRequest = event.request;
const isFromVanilla = originalRequest.headers.has('oidc-vanilla');
Expand Down
152 changes: 152 additions & 0 deletions packages/oidc-client-service-worker/src/__tests__/oidcConfig.spec.ts
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)
})
})
17 changes: 17 additions & 0 deletions packages/oidc-client-service-worker/src/oidcConfig.ts
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 };

0 comments on commit 73eae7e

Please sign in to comment.