adk-typescript
Version:
TypeScript port of Google's Agent Development Kit (ADK)
106 lines (105 loc) • 3.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasicAuthScheme = exports.BearerAuthScheme = exports.ApiKeyAuthScheme = exports.OAuthGrantType = exports.AuthSchemeType = void 0;
exports.getOAuthGrantTypeFromFlow = getOAuthGrantTypeFromFlow;
/**
* Enum for supported authentication scheme types.
*/
var AuthSchemeType;
(function (AuthSchemeType) {
AuthSchemeType["API_KEY"] = "apiKey";
AuthSchemeType["HTTP"] = "http";
AuthSchemeType["OAUTH2"] = "oauth2";
AuthSchemeType["OPEN_ID_CONNECT"] = "openIdConnect";
AuthSchemeType["SERVICE_ACCOUNT"] = "serviceAccount";
})(AuthSchemeType || (exports.AuthSchemeType = AuthSchemeType = {}));
/**
* Enum for OAuth2 grant types.
*/
var OAuthGrantType;
(function (OAuthGrantType) {
OAuthGrantType["CLIENT_CREDENTIALS"] = "client_credentials";
OAuthGrantType["AUTHORIZATION_CODE"] = "authorization_code";
OAuthGrantType["IMPLICIT"] = "implicit";
OAuthGrantType["PASSWORD"] = "password";
OAuthGrantType["REFRESH_TOKEN"] = "refresh_token";
})(OAuthGrantType || (exports.OAuthGrantType = OAuthGrantType = {}));
/**
* Utility function to get OAuthGrantType from an OAuthFlows-like object.
*/
function getOAuthGrantTypeFromFlow(flow) {
if (flow.clientCredentials)
return OAuthGrantType.CLIENT_CREDENTIALS;
if (flow.authorizationCode)
return OAuthGrantType.AUTHORIZATION_CODE;
if (flow.implicit)
return OAuthGrantType.IMPLICIT;
if (flow.password)
return OAuthGrantType.PASSWORD;
return undefined;
}
/**
* API key authentication scheme
*/
class ApiKeyAuthScheme {
/**
* Initializes the API key authentication scheme
* @param headerName The name of the header to include the API key in
*/
constructor(headerName = 'X-Api-Key') {
this.type = 'apiKey';
this.headerName = headerName;
}
/**
* Generate authentication headers for HTTP requests
* @param apiKey The API key to include in the headers
* @returns The headers to include in HTTP requests
*/
generateHeaders(apiKey) {
const headers = {};
headers[this.headerName] = apiKey;
return headers;
}
}
exports.ApiKeyAuthScheme = ApiKeyAuthScheme;
/**
* Bearer token authentication scheme
*/
class BearerAuthScheme {
constructor() {
this.type = 'bearer';
}
/**
* Generate authentication headers for HTTP requests
* @param token The bearer token to include in the headers
* @returns The headers to include in HTTP requests
*/
generateHeaders(token) {
return {
'Authorization': `Bearer ${token}`
};
}
}
exports.BearerAuthScheme = BearerAuthScheme;
/**
* Basic authentication scheme
*/
class BasicAuthScheme {
constructor() {
this.type = 'basic';
}
/**
* Generate authentication headers for HTTP requests
* @param credentials The credentials to use for authentication
* @param credentials.username The username for basic authentication
* @param credentials.password The password for basic authentication
* @returns The headers to include in HTTP requests
*/
generateHeaders(credentials) {
const auth = Buffer.from(`${credentials.username}:${credentials.password}`).toString('base64');
return {
'Authorization': `Basic ${auth}`
};
}
}
exports.BasicAuthScheme = BasicAuthScheme;