forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth-utils.ts
More file actions
58 lines (53 loc) · 2.18 KB
/
Copy pathauth-utils.ts
File metadata and controls
58 lines (53 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { setEnvVar } from '../../lib';
import type { AgentCoreProjectSpec, CustomClaimValidation } from '../../schema';
import { computeDefaultCredentialEnvVarName, computeManagedOAuthCredentialName } from './credential-utils';
/** Flat JWT config from TUI/CLI (pre-schema-transformation). */
export interface JwtConfigOptions {
discoveryUrl: string;
allowedAudience?: string[];
allowedClients?: string[];
allowedScopes?: string[];
customClaims?: CustomClaimValidation[];
clientId?: string;
clientSecret?: string;
}
/**
* Build the nested authorizerConfiguration schema shape from flat JWT config.
*/
export function buildAuthorizerConfigFromJwtConfig(jwtConfig: JwtConfigOptions) {
return {
customJwtAuthorizer: {
discoveryUrl: jwtConfig.discoveryUrl,
...(jwtConfig.allowedAudience?.length ? { allowedAudience: jwtConfig.allowedAudience } : {}),
...(jwtConfig.allowedClients?.length ? { allowedClients: jwtConfig.allowedClients } : {}),
...(jwtConfig.allowedScopes?.length ? { allowedScopes: jwtConfig.allowedScopes } : {}),
...(jwtConfig.customClaims?.length ? { customClaims: jwtConfig.customClaims } : {}),
},
};
}
/**
* Create a managed OAuth credential for inbound auth.
* Adds the credential to the project spec and writes client secrets to .env.
*/
export async function createManagedOAuthCredential(
resourceName: string,
jwtConfig: JwtConfigOptions,
writeProjectSpec: (spec: AgentCoreProjectSpec) => Promise<void>,
readProjectSpec: () => Promise<AgentCoreProjectSpec>
): Promise<void> {
const credentialName = computeManagedOAuthCredentialName(resourceName);
const project = await readProjectSpec();
if (project.credentials.some(c => c.name === credentialName)) return;
project.credentials.push({
authorizerType: 'OAuthCredentialProvider',
name: credentialName,
discoveryUrl: jwtConfig.discoveryUrl,
vendor: 'CustomOauth2',
managed: true,
usage: 'inbound',
});
await writeProjectSpec(project);
const envVarPrefix = computeDefaultCredentialEnvVarName(credentialName);
await setEnvVar(`${envVarPrefix}_CLIENT_ID`, jwtConfig.clientId!);
await setEnvVar(`${envVarPrefix}_CLIENT_SECRET`, jwtConfig.clientSecret!);
}