From 28d9d1d2274dea2b79d130eb8bbe18acdbcd7672 Mon Sep 17 00:00:00 2001 From: Shubham Tiwari Date: Thu, 5 Dec 2024 14:33:33 +0530 Subject: [PATCH] chore: add orgs credential provider --- .../NoAuthCredentialProvider.ts | 2 +- .../OrgsCredentialProvider.ts | 66 +++++++++++++++++++ src/http/bearer_token/OrgsTokenManager.ts | 40 +++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/credential_provider/OrgsCredentialProvider.ts create mode 100644 src/http/bearer_token/OrgsTokenManager.ts diff --git a/src/credential_provider/NoAuthCredentialProvider.ts b/src/credential_provider/NoAuthCredentialProvider.ts index bd2415e5a..c36d9013f 100644 --- a/src/credential_provider/NoAuthCredentialProvider.ts +++ b/src/credential_provider/NoAuthCredentialProvider.ts @@ -5,7 +5,7 @@ import NoAuthStrategy from "../auth_strategy/NoAuthStrategy"; namespace NoAuthCredentialProvider { export class NoAuthCredentialProvider extends CredentialProvider { constructor() { - super("client-credentials"); + super("noauth"); } public toAuthStrategy(): AuthStrategy { diff --git a/src/credential_provider/OrgsCredentialProvider.ts b/src/credential_provider/OrgsCredentialProvider.ts new file mode 100644 index 000000000..c1dfe3124 --- /dev/null +++ b/src/credential_provider/OrgsCredentialProvider.ts @@ -0,0 +1,66 @@ +import CredentialProvider from "./CredentialProvider"; +import TokenManager from "../http/bearer_token/TokenManager"; +import AuthStrategy from "../auth_strategy/AuthStrategy"; +import OrgsTokenManager from "../http/bearer_token/OrgsTokenManager"; +import TokenAuthStrategy from "../auth_strategy/TokenAuthStrategy"; + +class OrgsCredentialProvider extends CredentialProvider { + grantType: string; + clientId: string; + clientSecret: string; + tokenManager: TokenManager | null; + + constructor() { + super("client-credentials"); + this.grantType = "client_credentials"; + this.clientId = ""; + this.clientSecret = ""; + this.tokenManager = null; + } + + public toAuthStrategy(): AuthStrategy { + if (this.tokenManager == null) { + this.tokenManager = new OrgsTokenManager({ + grantType: this.grantType, + clientId: this.clientId, + clientSecret: this.clientSecret, + }); + } + return new TokenAuthStrategy(this.tokenManager); + } +} + +namespace OrgsCredentialProvider { + export class OrgsCredentialProviderBuilder { + private readonly instance: OrgsCredentialProvider; + + constructor() { + this.instance = new OrgsCredentialProvider(); + } + + public setClientId(clientId: string): OrgsCredentialProviderBuilder { + this.instance.clientId = clientId; + return this; + } + + public setClientSecret( + clientSecret: string + ): OrgsCredentialProviderBuilder { + this.instance.clientSecret = clientSecret; + return this; + } + + public setTokenManager( + tokenManager: TokenManager + ): OrgsCredentialProviderBuilder { + this.instance.tokenManager = tokenManager; + return this; + } + + public build(): OrgsCredentialProvider { + return this.instance; + } + } +} + +export = OrgsCredentialProvider; diff --git a/src/http/bearer_token/OrgsTokenManager.ts b/src/http/bearer_token/OrgsTokenManager.ts new file mode 100644 index 000000000..b620ad2e8 --- /dev/null +++ b/src/http/bearer_token/OrgsTokenManager.ts @@ -0,0 +1,40 @@ +import TokenManager from "./TokenManager"; +import { + TokenListInstance, + TokenListInstanceCreateOptions, +} from "../../rest/previewIam/v1/token"; +import PreviewIamBase from "../../rest/PreviewIamBase"; +import V1 from "../../rest/previewIam/V1"; +import NoAuthCredentialProvider from "../../credential_provider/NoAuthCredentialProvider"; +import { Client } from "../../base/BaseTwilio"; + +export default class OrgsTokenManager implements TokenManager { + private readonly params: TokenListInstanceCreateOptions; + + constructor(params: TokenListInstanceCreateOptions) { + this.params = params; + } + + getParams(): TokenListInstanceCreateOptions { + return this.params; + } + + async fetchToken(): Promise { + const noAuthCredentialProvider = + new NoAuthCredentialProvider.NoAuthCredentialProvider(); + const client = new Client(); + client.setCredentialProvider(noAuthCredentialProvider); + + const tokenListInstance = TokenListInstance( + new V1(new PreviewIamBase(client)) + ); + return tokenListInstance + .create(this.params) + .then((token) => { + return token.accessToken; + }) + .catch((error) => { + throw new Error(`Failed to fetch access token: ${error}`); + }); + } +}