@@ -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,30 @@ 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 use when authenticating with the service.
145+ * The access token should be valid and not yet expired.
146+ *
147+ * By using this method, you accept responsibility for managing the
148+ * access token yourself. You must set a new access token before this
149+ * one expires. Failing to do so will result in authentication errors
150+ * after this token expires.
151+ *
152+ * @param {string } access_token - A valid, non-expired IAM access token
153+ * @returns {void }
154+ */
155+ public setAccessToken ( access_token : string ) { // tslint:disable-line variable-name
156+ this . _options . access_token = access_token ;
157+ this . _options . headers = this . _options . headers || { } ;
158+ this . _options . headers . Authorization = `Bearer ${ access_token } ` ;
159+ }
160+
138161 /**
139162 * @private
140163 * @param {UserOptions } options
@@ -162,24 +185,12 @@ export class BaseService {
162185 _options
163186 ) ;
164187 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- ) ;
188+ if ( ! hasCredentials ( _options ) ) {
189+ const errorMessage = 'Insufficient credentials provided in ' +
190+ 'constructor argument. Refer to the documentation for the ' +
191+ 'required parameters. Common examples are username/password, ' +
192+ 'api_key, and access_token.' ;
193+ throw new Error ( errorMessage ) ;
183194 }
184195 if ( hasBasicCredentials ( _options ) ) {
185196 // Calculate and add Authorization header to base options
@@ -188,6 +199,9 @@ export class BaseService {
188199 ) . toString ( 'base64' ) ;
189200 const authHeader = { Authorization : `Basic ${ encodedCredentials } ` } ;
190201 _options . headers = extend ( authHeader , _options . headers ) ;
202+ } else if ( hasAccessToken ( _options ) ) {
203+ const authHeader = { Authorization : `Bearer ${ _options . access_token } ` } ;
204+ _options . headers = extend ( authHeader , _options . headers ) ;
191205 } else {
192206 _options . qs = extend ( { api_key : _options . api_key } , _options . qs ) ;
193207 }
@@ -221,12 +235,14 @@ export class BaseService {
221235 const _password : string = process . env [ `${ _name } _PASSWORD` ] || process . env [ `${ _nameWithUnderscore } _PASSWORD` ] ;
222236 const _apiKey : string = process . env [ `${ _name } _API_KEY` ] || process . env [ `${ _nameWithUnderscore } _API_KEY` ] ;
223237 const _url : string = process . env [ `${ _name } _URL` ] || process . env [ `${ _nameWithUnderscore } _URL` ] ;
238+ const _accessToken : string = process . env [ `${ _name } _ACCESS_TOKEN` ] || process . env [ `${ _nameWithUnderscore } _ACCESS_TOKEN` ] ;
224239
225240 return {
226241 username : _username ,
227242 password : _password ,
228243 api_key : _apiKey ,
229- url : _url
244+ url : _url ,
245+ access_token : _accessToken
230246 } ;
231247 }
232248 /**
0 commit comments