这是indexloc提供的服务,不要输入任何密码
Skip to content

refactor (oidcServiceWorker): Extract GetCurrentDatabaseTokenEndpoint, add tests #1405

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
Loading