11/* tslint:disable */
22{ {> licenseInfo} }
33
4+ import globalAxios from 'axios';
5+
46interface AWSv4Configuration {
57 options?: {
68 region?: string
@@ -17,6 +19,8 @@ export interface ConfigurationParameters {
1719 apiKey?: string | Promise< string> | ((name: string) => string) | ((name: string) => Promise< string> );
1820 username?: string;
1921 password?: string;
22+ clientId?: string;
23+ clientSecret?: string;
2024 accessToken?: string | Promise< string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise< string> );
2125 awsv4?: AWSv4Configuration;
2226 basePath?: string;
@@ -39,12 +43,28 @@ export class Configuration {
3943 * parameter for basic security
4044 */
4145 password?: string;
46+ /**
47+ * parameter for client ID
48+ */
49+ clientId?: string;
50+ /**
51+ * parameter for client secret
52+ */
53+ clientSecret?: string;
4254 /**
4355 * parameter for oauth2 security
4456 * @param name security name
4557 * @param scopes oauth2 scope
4658 */
4759 accessToken?: string | Promise< string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise< string> );
60+ /**
61+ * temporary access token storage
62+ */
63+ tempAccessToken?: string;
64+ /**
65+ * when the temporary access token expires
66+ */
67+ tempAccessTokenExpiration?: number;
4868 /**
4969 * parameter for aws4 signature security
5070 * @param {Object} AWS4Signature - AWS4 Signature security
@@ -81,7 +101,29 @@ export class Configuration {
81101 this.apiKey = param.apiKey;
82102 this.username = param.username;
83103 this.password = param.password;
84- this.accessToken = param.accessToken;
104+ this.clientId = param.clientId;
105+ this.clientSecret = param.clientSecret;
106+ this.tempAccessToken;
107+ this.tempAccessTokenExpiration;
108+ this.accessToken = param.accessToken ?? (async (name, scopes) => {
109+ const now = Math.floor(Date.now() / 1000);
110+ if (this.tempAccessToken && (! this.tempAccessTokenExpiration || this.tempAccessTokenExpiration > now + 60)) {
111+ return this.tempAccessToken;
112+ } else if (this.clientId && this.clientSecret) {
113+ const tokenRequestArgs = {
114+ method: ' POST' ,
115+ headers: { ' Content-Type' : ' application/x-www-form-urlencoded' } ,
116+ auth: { username: this.clientId || ' ' , password: this.clientSecret || ' ' } ,
117+ data: 'grant_type=client_credentials',
118+ url: 'https://api.bandwidth.com/api/v1/oauth2/token'
119+ };
120+ const response = await globalAxios.request(tokenRequestArgs);
121+ this.tempAccessToken = response.data.access_token;
122+ this.tempAccessTokenExpiration = now + response.data.expires_in;
123+ return this.tempAccessToken!;
124+ }
125+ return undefined;
126+ });
85127 this.awsv4 = param.awsv4;
86128 this.basePath = param.basePath;
87129 this.serverIndex = param.serverIndex;
0 commit comments