1111 * Do not edit the class manually.
1212 */
1313
14+ import globalAxios from 'axios' ;
15+
1416interface AWSv4Configuration {
1517 options ?: {
1618 region ?: string
@@ -27,6 +29,8 @@ export interface ConfigurationParameters {
2729 apiKey ?: string | Promise < string > | ( ( name : string ) => string ) | ( ( name : string ) => Promise < string > ) ;
2830 username ?: string ;
2931 password ?: string ;
32+ clientId ?: string ;
33+ clientSecret ?: string ;
3034 accessToken ?: string | Promise < string > | ( ( name ?: string , scopes ?: string [ ] ) => string ) | ( ( name ?: string , scopes ?: string [ ] ) => Promise < string > ) ;
3135 awsv4 ?: AWSv4Configuration ;
3236 basePath ?: string ;
@@ -49,12 +53,28 @@ export class Configuration {
4953 * parameter for basic security
5054 */
5155 password ?: string ;
56+ /**
57+ * parameter for client ID
58+ */
59+ clientId ?: string ;
60+ /**
61+ * parameter for client secret
62+ */
63+ clientSecret ?: string ;
5264 /**
5365 * parameter for oauth2 security
5466 * @param name security name
5567 * @param scopes oauth2 scope
5668 */
5769 accessToken ?: string | Promise < string > | ( ( name ?: string , scopes ?: string [ ] ) => string ) | ( ( name ?: string , scopes ?: string [ ] ) => Promise < string > ) ;
70+ /**
71+ * temporary access token storage
72+ */
73+ tempAccessToken ?: string ;
74+ /**
75+ * when the temporary access token expires
76+ */
77+ tempAccessTokenExpiration ?: number ;
5878 /**
5979 * parameter for aws4 signature security
6080 * @param {Object } AWS4Signature - AWS4 Signature security
@@ -91,7 +111,29 @@ export class Configuration {
91111 this . apiKey = param . apiKey ;
92112 this . username = param . username ;
93113 this . password = param . password ;
94- this . accessToken = param . accessToken ;
114+ this . clientId = param . clientId ;
115+ this . clientSecret = param . clientSecret ;
116+ this . tempAccessToken ;
117+ this . tempAccessTokenExpiration ;
118+ this . accessToken = param . accessToken ?? ( async ( name , scopes ) => {
119+ const now = Math . floor ( Date . now ( ) / 1000 ) ;
120+ if ( this . tempAccessToken && ( ! this . tempAccessTokenExpiration || this . tempAccessTokenExpiration > now + 60 ) ) {
121+ return this . tempAccessToken ;
122+ } else if ( this . clientId && this . clientSecret ) {
123+ const tokenRequestArgs = {
124+ method : 'POST' ,
125+ headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } ,
126+ auth : { username : this . clientId || '' , password : this . clientSecret || '' } ,
127+ data : 'grant_type=client_credentials' ,
128+ url : 'https://api.bandwidth.com/api/v1/oauth2/token'
129+ } ;
130+ const response = await globalAxios . request ( tokenRequestArgs ) ;
131+ this . tempAccessToken = response . data . access_token ;
132+ this . tempAccessTokenExpiration = now + response . data . expires_in ;
133+ return this . tempAccessToken ! ;
134+ }
135+ return undefined ;
136+ } ) ;
95137 this . awsv4 = param . awsv4 ;
96138 this . basePath = param . basePath ;
97139 this . serverIndex = param . serverIndex ;
0 commit comments