@@ -38,6 +38,7 @@ export interface UserOptions {
3838 use_unauthenticated ?: boolean ;
3939 headers ?: HeaderOptions ;
4040 token ?: string ;
41+ access_token ?: string ;
4142}
4243
4344export interface BaseServiceOptions extends UserOptions {
@@ -52,18 +53,19 @@ export interface Credentials {
5253 password : string ;
5354 api_key : string ;
5455 url : string ;
56+ access_token : string ;
5557}
5658
5759function hasCredentials ( obj : any ) : boolean {
58- return obj && ( ( obj . username && obj . password ) || obj . api_key ) ;
60+ return obj && ( ( obj . username && obj . password ) || obj . api_key || obj . access_token ) ;
5961}
6062
6163function hasBasicCredentials ( obj : any ) : boolean {
6264 return obj && obj . username && obj . password ;
6365}
6466
65- function acceptsApiKey ( name : string ) : boolean {
66- return name === 'visual_recognition' ;
67+ function hasAccessToken ( obj : any ) : boolean {
68+ return obj && obj . access_token ;
6769}
6870
6971export class BaseService {
@@ -132,9 +134,26 @@ export class BaseService {
132134 if ( this . _options . url ) {
133135 _credentials . url = this . _options . url ;
134136 }
137+ if ( this . _options . access_token ) {
138+ _credentials . access_token = this . _options . access_token ;
139+ }
135140 return _credentials ;
136141 }
137142
143+ /**
144+ * Set an IAM access token to authenticate the service with
145+ * The SDK will assume this token is valid and that this method
146+ * will be used to set a new token upon expiration.
147+ *
148+ * @param {string } accessToken - Valid IAM access token
149+ * @returns {void }
150+ */
151+ public setAccessToken ( accessToken : string ) {
152+ this . _options . access_token = accessToken ;
153+ this . _options . headers = this . _options . headers || { } ;
154+ this . _options . headers . Authorization = `Bearer ${ accessToken } ` ;
155+ }
156+
138157 /**
139158 * @private
140159 * @param {UserOptions } options
@@ -162,24 +181,12 @@ export class BaseService {
162181 _options
163182 ) ;
164183 if ( ! _options . use_unauthenticated ) {
165- if ( ! hasCredentials ( _options ) && acceptsApiKey ( this . name ) ) {
166- throw new Error (
167- `Argument error: api_key or username/password are required for ${ this . name
168- . toUpperCase ( )
169- . replace (
170- / _ / g,
171- ' '
172- ) } ${ this . serviceVersion . toUpperCase ( ) } unless use_unauthenticated is set`
173- ) ;
174- } else if ( ! hasCredentials ( _options ) ) {
175- throw new Error (
176- `Argument error: username and password are required for ${ this . name
177- . toUpperCase ( )
178- . replace (
179- / _ / g,
180- ' '
181- ) } ${ this . serviceVersion . toUpperCase ( ) } unless use_unauthenticated is set`
182- ) ;
184+ if ( ! hasCredentials ( _options ) ) {
185+ const errorMessage = 'Insufficient credentials provided in ' +
186+ 'constructor argument. Refer to the documentation for the ' +
187+ 'required parameters. Common examples are username/password, ' +
188+ 'api_key, and access_token.' ;
189+ throw new Error ( errorMessage ) ;
183190 }
184191 if ( hasBasicCredentials ( _options ) ) {
185192 // Calculate and add Authorization header to base options
@@ -188,6 +195,9 @@ export class BaseService {
188195 ) . toString ( 'base64' ) ;
189196 const authHeader = { Authorization : `Basic ${ encodedCredentials } ` } ;
190197 _options . headers = extend ( authHeader , _options . headers ) ;
198+ } else if ( hasAccessToken ( _options ) ) {
199+ const authHeader = { Authorization : `Bearer ${ _options . access_token } ` } ;
200+ _options . headers = extend ( authHeader , _options . headers ) ;
191201 } else {
192202 _options . qs = extend ( { api_key : _options . api_key } , _options . qs ) ;
193203 }
@@ -221,12 +231,14 @@ export class BaseService {
221231 const _password : string = process . env [ `${ _name } _PASSWORD` ] || process . env [ `${ _nameWithUnderscore } _PASSWORD` ] ;
222232 const _apiKey : string = process . env [ `${ _name } _API_KEY` ] || process . env [ `${ _nameWithUnderscore } _API_KEY` ] ;
223233 const _url : string = process . env [ `${ _name } _URL` ] || process . env [ `${ _nameWithUnderscore } _URL` ] ;
234+ const _accessToken : string = process . env [ `${ _name } _ACCESS_TOKEN` ] || process . env [ `${ _nameWithUnderscore } _ACCESS_TOKEN` ] ;
224235
225236 return {
226237 username : _username ,
227238 password : _password ,
228239 api_key : _apiKey ,
229- url : _url
240+ url : _url ,
241+ access_token : _accessToken
230242 } ;
231243 }
232244 /**
0 commit comments