-
Notifications
You must be signed in to change notification settings - Fork 13
Auth credential exchange registry implementation #15
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
Open
hariprasath2603
wants to merge
1
commit into
main
Choose a base branch
from
hariprasath2603/auth-credential-excahnger
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+196
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,35 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import {AuthCredential} from '../auth_credential.js'; | ||
| import {AuthScheme} from '../auth_schemes.js'; | ||
|
|
||
| /** | ||
| * Base exception for credential exchange errors. | ||
| */ | ||
| export class CredentialExchangeError extends Error {} | ||
|
|
||
| /** | ||
| * Base interface for credential exchangers. | ||
| * | ||
| * Credential exchangers are responsible for exchanging credentials from | ||
| * one format or scheme to another. | ||
| */ | ||
| export interface BaseCredentialExchanger { | ||
| /** | ||
| * Exchange credential if needed. | ||
| * | ||
| * @param authCredential - The credential to exchange. | ||
| * @param authScheme - The authentication scheme (optional, some exchangers don't need it). | ||
| * @returns The exchanged credential. | ||
| * @throws CredentialExchangeError: If credential exchange fails. | ||
| */ | ||
|
|
||
| exchange( | ||
| authCredential: AuthCredential, | ||
| authScheme?: AuthScheme, | ||
| ): Promise<AuthCredential>; | ||
| } | ||
This file contains hidden or 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,43 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Credential exchanger registry. | ||
| */ | ||
| import {AuthCredentialTypes} from '../auth_credential.js'; | ||
| import {BaseCredentialExchanger} from './base_credential_exchanger.js'; | ||
|
|
||
| /** | ||
| * Registry for credential exchanger instances. | ||
| */ | ||
| export class CredentialExchangerRegistry { | ||
| private exchangers: Partial< | ||
| Record<AuthCredentialTypes, BaseCredentialExchanger> | ||
| > = {}; | ||
|
|
||
| /** | ||
| * Register an exchanger instance for a credential type. | ||
| * @param credentialType - The credential type to register for. | ||
| * @param exchangerInstance - The exchanger instance to register. | ||
| */ | ||
| register( | ||
| credentialType: AuthCredentialTypes, | ||
| exchangerInstance: BaseCredentialExchanger, | ||
| ): void { | ||
| this.exchangers[credentialType] = exchangerInstance; | ||
| } | ||
|
|
||
| /** | ||
| * Get the exchanger instance for a credential type. | ||
| * @param credentialType - The credential type to get exchanger for. | ||
| * @returns The exchanger instance if registered, undefined otherwise. | ||
| */ | ||
| getExchanger( | ||
| credentialType: AuthCredentialTypes, | ||
| ): BaseCredentialExchanger | undefined { | ||
| return this.exchangers[credentialType]; | ||
| } | ||
| } |
This file contains hidden or 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,118 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { | ||
| AuthCredential, | ||
| AuthCredentialTypes, | ||
| } from '../../../src/auth/auth_credential.js'; | ||
| import {AuthScheme} from '../../../src/auth/auth_schemes.js'; | ||
| import {BaseCredentialExchanger} from '../../../src/auth/exchanger/base_credential_exchanger.js'; | ||
| import {CredentialExchangerRegistry} from '../../../src/auth/exchanger/credential_exchanger_registry.js'; | ||
|
|
||
| // Mock credential exchanger for testing | ||
| class MockCredentialExchanger implements BaseCredentialExchanger { | ||
| async exchange( | ||
| authCredential: AuthCredential, | ||
| authScheme?: AuthScheme, | ||
| ): Promise<AuthCredential> { | ||
| return authCredential; | ||
| } | ||
| } | ||
|
|
||
| describe('CredentialExchangerRegistry', () => { | ||
| it('should initialize with an empty exchangers dictionary', () => { | ||
| const registry = new CredentialExchangerRegistry(); | ||
|
|
||
| // Since exchangers is private, test indirectly by trying to get an exchanger | ||
| // All credential types should return undefined when registry is empty | ||
| expect(registry.getExchanger(AuthCredentialTypes.OAUTH2)).toBeUndefined(); | ||
| expect(registry.getExchanger(AuthCredentialTypes.API_KEY)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should register a single exchanger', () => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: test case can be simplified to focus on one type. |
||
| const registry = new CredentialExchangerRegistry(); | ||
| const mockExchanger = new MockCredentialExchanger(); | ||
|
|
||
| registry.register(AuthCredentialTypes.API_KEY, mockExchanger); | ||
|
|
||
| // Verify the exchanger was registered | ||
| const retrievedExchanger = registry.getExchanger( | ||
| AuthCredentialTypes.API_KEY, | ||
| ); | ||
| expect(retrievedExchanger).toBe(mockExchanger); | ||
| }); | ||
|
|
||
| it('Should register all credential types', () => { | ||
| const registry = new CredentialExchangerRegistry(); | ||
|
|
||
| // Register all credential types | ||
| const mockExchangerApiKey = new MockCredentialExchanger(); | ||
| const mockExchangerOauth2 = new MockCredentialExchanger(); | ||
| const mockExchangerOpenIdConnect = new MockCredentialExchanger(); | ||
| const mockExchangerServiceAccount = new MockCredentialExchanger(); | ||
|
|
||
| // Register each credential type | ||
| registry.register(AuthCredentialTypes.API_KEY, mockExchangerApiKey); | ||
| registry.register(AuthCredentialTypes.OAUTH2, mockExchangerOauth2); | ||
| registry.register( | ||
| AuthCredentialTypes.OPEN_ID_CONNECT, | ||
| mockExchangerOpenIdConnect, | ||
| ); | ||
| registry.register( | ||
| AuthCredentialTypes.SERVICE_ACCOUNT, | ||
| mockExchangerServiceAccount, | ||
| ); | ||
|
|
||
| // Verify each credential type was registered | ||
| expect(registry.getExchanger(AuthCredentialTypes.API_KEY)).toBe( | ||
| mockExchangerApiKey, | ||
| ); | ||
| expect(registry.getExchanger(AuthCredentialTypes.OAUTH2)).toBe( | ||
| mockExchangerOauth2, | ||
| ); | ||
| expect(registry.getExchanger(AuthCredentialTypes.OPEN_ID_CONNECT)).toBe( | ||
| mockExchangerOpenIdConnect, | ||
| ); | ||
| expect(registry.getExchanger(AuthCredentialTypes.SERVICE_ACCOUNT)).toBe( | ||
| mockExchangerServiceAccount, | ||
| ); | ||
| }); | ||
|
|
||
| it('Should return undefined for an not registered credential type', () => { | ||
| const registry = new CredentialExchangerRegistry(); | ||
| const mockExchangerApiKey = new MockCredentialExchanger(); | ||
|
|
||
| registry.register(AuthCredentialTypes.API_KEY, mockExchangerApiKey); | ||
|
|
||
| // Verify the exchanger with API key is registered and undefined for OAuth2 | ||
| expect(registry.getExchanger(AuthCredentialTypes.API_KEY)).toBe( | ||
| mockExchangerApiKey, | ||
| ); | ||
| expect(registry.getExchanger(AuthCredentialTypes.OAUTH2)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('Should isolate registry instances', () => { | ||
| const registry1 = new CredentialExchangerRegistry(); | ||
| const registry2 = new CredentialExchangerRegistry(); | ||
|
|
||
| const mockExchangerApiKey = new MockCredentialExchanger(); | ||
| const mockExchangerOauth2 = new MockCredentialExchanger(); | ||
|
|
||
| // Register different credential types for different registries | ||
| registry1.register(AuthCredentialTypes.API_KEY, mockExchangerApiKey); | ||
| registry2.register(AuthCredentialTypes.OAUTH2, mockExchangerOauth2); | ||
|
|
||
| // Verify each registry has the correct exchanger and undefined for the other credential type | ||
| expect(registry1.getExchanger(AuthCredentialTypes.API_KEY)).toBe( | ||
| mockExchangerApiKey, | ||
| ); | ||
| expect(registry1.getExchanger(AuthCredentialTypes.OAUTH2)).toBeUndefined(); | ||
| expect(registry2.getExchanger(AuthCredentialTypes.OAUTH2)).toBe( | ||
| mockExchangerOauth2, | ||
| ); | ||
| expect(registry2.getExchanger(AuthCredentialTypes.API_KEY)).toBeUndefined(); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's use named Parameters