-
Notifications
You must be signed in to change notification settings - Fork 124
feat: add auth related base classes #223
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
+962
−3
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import {AuthScheme} from './auth_schemes.js'; | ||
| import {BaseAuthProvider} from './base_auth_provider.js'; | ||
|
|
||
| /** | ||
| * Registry for auth provider instances. | ||
| */ | ||
| export class AuthProviderRegistry { | ||
| private readonly providers = new Map<string, BaseAuthProvider>(); | ||
|
|
||
| /** | ||
| * Register a provider instance for an auth scheme type. | ||
| * | ||
| * @param authSchemeType The auth scheme type (e.g., 'oauth2', 'apiKey'). | ||
| * @param providerInstance The provider instance to register. | ||
| */ | ||
| register(authSchemeType: string, providerInstance: BaseAuthProvider): void { | ||
| this.providers.set(authSchemeType, providerInstance); | ||
| } | ||
|
|
||
| /** | ||
| * Get the provider instance for an auth scheme. | ||
| * | ||
| * @param authScheme The auth scheme to get provider for. | ||
| * @returns The provider instance if registered, undefined otherwise. | ||
| */ | ||
| getProvider(authScheme: AuthScheme): BaseAuthProvider | undefined { | ||
| return this.providers.get(authScheme.type); | ||
| } | ||
| } |
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,25 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import {AuthCredential} from './auth_credential.js'; | ||
| import {AuthConfig} from './auth_tool.js'; | ||
|
|
||
| /** | ||
| * Abstract base interface for custom authentication providers. | ||
| */ | ||
| export interface BaseAuthProvider { | ||
| /** | ||
| * Provide an AuthCredential asynchronously. | ||
| * | ||
| * @param authConfig The current authentication configuration. | ||
| * @param context The current callback context (placeholder). | ||
| * @returns The retrieved AuthCredential, or undefined if unavailable. | ||
| */ | ||
| getAuthCredential( | ||
| authConfig: AuthConfig, | ||
| context?: unknown, | ||
| ): Promise<AuthCredential | undefined>; | ||
| } |
35 changes: 35 additions & 0 deletions
35
core/src/auth/credential_service/session_state_credential_service.ts
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 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import {Context} from '../../agents/context.js'; | ||
| import {AuthCredential} from '../auth_credential.js'; | ||
| import {AuthConfig} from '../auth_tool.js'; | ||
| import {BaseCredentialService} from './base_credential_service.js'; | ||
|
|
||
| /** | ||
| * Class for implementation of credential service using session state as the store. | ||
| * Note: store credential in session may not be secure, use at your own risk. | ||
| */ | ||
| export class SessionStateCredentialService implements BaseCredentialService { | ||
| loadCredential( | ||
| authConfig: AuthConfig, | ||
| toolContext: Context, | ||
| ): Promise<AuthCredential | undefined> { | ||
| return Promise.resolve(toolContext.state.get(authConfig.credentialKey)); | ||
| } | ||
|
|
||
| async saveCredential( | ||
| authConfig: AuthConfig, | ||
| toolContext: Context, | ||
| ): Promise<void> { | ||
| if (authConfig.exchangedAuthCredential) { | ||
| toolContext.state.set( | ||
| authConfig.credentialKey, | ||
| authConfig.exchangedAuthCredential, | ||
| ); | ||
| } | ||
| } | ||
| } |
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
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,51 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import {AuthCredential} from '../auth_credential.js'; | ||
| import {AuthScheme} from '../auth_schemes.js'; | ||
|
|
||
| /** | ||
| * Base exception for credential refresh errors. | ||
| */ | ||
| export class CredentialRefresherError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = 'CredentialRefresherError'; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Base interface for credential refreshers. | ||
| * | ||
| * Credential refreshers are responsible for checking if a credential is expired | ||
| * or needs to be refreshed, and for refreshing it if necessary. | ||
| */ | ||
| export interface BaseCredentialRefresher { | ||
| /** | ||
| * Checks if a credential needs to be refreshed. | ||
| * | ||
| * @param authCredential The credential to check. | ||
| * @param authScheme The authentication scheme (optional). | ||
| * @returns True if the credential needs to be refreshed, False otherwise. | ||
| */ | ||
| isRefreshNeeded( | ||
|
kalenkevich marked this conversation as resolved.
|
||
| authCredential: AuthCredential, | ||
| authScheme?: AuthScheme, | ||
| ): Promise<boolean>; | ||
|
|
||
| /** | ||
| * Refreshes a credential if needed. | ||
| * | ||
| * @param authCredential The credential to refresh. | ||
| * @param authScheme The authentication scheme (optional). | ||
| * @returns The refreshed credential. | ||
| * @throws {CredentialRefresherError} If credential refresh fails. | ||
| */ | ||
| refresh( | ||
| 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,49 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import {AuthCredentialTypes} from '../auth_credential.js'; | ||
| import {BaseCredentialRefresher} from './base_credential_refresher.js'; | ||
|
|
||
| /** | ||
| * Registry for credential refresher instances. | ||
| */ | ||
| export class CredentialRefresherRegistry { | ||
| private readonly refreshers: Record< | ||
| AuthCredentialTypes, | ||
| BaseCredentialRefresher | undefined | ||
|
kalenkevich marked this conversation as resolved.
|
||
| > = { | ||
| [AuthCredentialTypes.API_KEY]: undefined, | ||
| [AuthCredentialTypes.HTTP]: undefined, | ||
| [AuthCredentialTypes.OAUTH2]: undefined, | ||
| [AuthCredentialTypes.OPEN_ID_CONNECT]: undefined, | ||
| [AuthCredentialTypes.SERVICE_ACCOUNT]: undefined, | ||
| }; | ||
|
|
||
| /** | ||
| * Register a refresher instance for a credential type. | ||
| * | ||
| * @param credentialType The credential type to register for. | ||
| * @param refresherInstance The refresher instance to register. | ||
| */ | ||
| register( | ||
| credentialType: AuthCredentialTypes, | ||
| refresherInstance: BaseCredentialRefresher, | ||
| ): void { | ||
| this.refreshers[credentialType] = refresherInstance; | ||
| } | ||
|
|
||
| /** | ||
| * Get the refresher instance for a credential type. | ||
| * | ||
| * @param credentialType The credential type to get refresher for. | ||
| * @returns The refresher instance if registered, undefined otherwise. | ||
| */ | ||
| getRefresher( | ||
| credentialType: AuthCredentialTypes, | ||
| ): BaseCredentialRefresher | undefined { | ||
| return this.refreshers[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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.