|
| 1 | +/** |
| 2 | + * Copyright 2019 IBM Corp. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { OutgoingHttpHeaders } from 'http'; |
| 18 | +import { IamTokenManager } from '../token-managers'; |
| 19 | +import { BaseOptions, TokenRequestBasedAuthenticator } from './token-request-based-authenticator'; |
| 20 | + |
| 21 | +export interface Options extends BaseOptions { |
| 22 | + apikey: string; |
| 23 | + clientId?: string; |
| 24 | + clientSecret?: string; |
| 25 | +} |
| 26 | + |
| 27 | +export class IamAuthenticator extends TokenRequestBasedAuthenticator { |
| 28 | + protected requiredOptions = ['apikey']; |
| 29 | + private apikey: string; |
| 30 | + private clientId: string; |
| 31 | + private clientSecret: string; |
| 32 | + |
| 33 | + /** |
| 34 | + * IAM Authenticator Class |
| 35 | + * |
| 36 | + * Handles the IAM authentication pattern. |
| 37 | + * |
| 38 | + * @param {Object} options |
| 39 | + * @constructor |
| 40 | + */ |
| 41 | + constructor(options: Options) { |
| 42 | + super(options); |
| 43 | + |
| 44 | + this.validate(options, this.requiredOptions); |
| 45 | + |
| 46 | + this.apikey = options.apikey; |
| 47 | + this.clientId = options.clientId; |
| 48 | + this.clientSecret = options.clientSecret; |
| 49 | + |
| 50 | + // the param names are shared between the authenticator and the token manager |
| 51 | + // so we can just pass along the options object |
| 52 | + this.tokenManager = new IamTokenManager(options); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Setter for the Client ID and the Client Secret. Both should be provided. |
| 57 | + * |
| 58 | + * @param {string} clientId |
| 59 | + * @param {string} clientSecret |
| 60 | + * @returns {void} |
| 61 | + */ |
| 62 | + public setClientIdAndSecret(clientId: string, clientSecret: string): void { |
| 63 | + this.clientId = clientId; |
| 64 | + this.clientSecret = clientSecret; |
| 65 | + |
| 66 | + // update properties in token manager |
| 67 | + this.tokenManager.setClientIdAndSecret(clientId, clientSecret); |
| 68 | + } |
| 69 | +} |
0 commit comments