|
| 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 extend = require('extend'); |
| 18 | +import { JwtTokenManagerV1 } from './jwt-token-manager-v1'; |
| 19 | +import { computeBasicAuthHeader } from './utils'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Check for only one of two elements being defined. |
| 23 | + * Returns true if a is defined and b is undefined, |
| 24 | + * or vice versa. Returns false if both are defined |
| 25 | + * or both are undefined. |
| 26 | + * |
| 27 | + * @param {any} a - The first object |
| 28 | + * @param {any} b - The second object |
| 29 | + * @returns {boolean} |
| 30 | + */ |
| 31 | +function onlyOne(a: any, b: any): boolean { |
| 32 | + return Boolean((a && !b) || (b && !a)); |
| 33 | +} |
| 34 | + |
| 35 | +const CLIENT_ID_SECRET_WARNING = 'Warning: Client ID and Secret must BOTH be given, or the defaults will be used.'; |
| 36 | + |
| 37 | +export type Options = { |
| 38 | + url?: string; |
| 39 | + iamUrl?: string; |
| 40 | + iamApikey?: string; |
| 41 | + accessToken?: string; |
| 42 | + iamAccessToken?: string; |
| 43 | + iamClientId?: string; |
| 44 | + iamClientSecret?: string; |
| 45 | +} |
| 46 | + |
| 47 | +// this interface is a representation of the response |
| 48 | +// object from the IAM service, hence the snake_case |
| 49 | +// parameter names |
| 50 | +export interface IamTokenData { |
| 51 | + access_token: string; |
| 52 | + refresh_token: string; |
| 53 | + token_type: string; |
| 54 | + expires_in: number; |
| 55 | + expiration: number; |
| 56 | +} |
| 57 | + |
| 58 | +export class IamTokenManagerV1 extends JwtTokenManagerV1 { |
| 59 | + private iamApikey: string; |
| 60 | + private iamClientId: string; |
| 61 | + private iamClientSecret: string; |
| 62 | + |
| 63 | + /** |
| 64 | + * IAM Token Manager Service |
| 65 | + * |
| 66 | + * Retreives and stores IAM access tokens. |
| 67 | + * |
| 68 | + * @param {Object} options |
| 69 | + * @param {String} options.iamApikey |
| 70 | + * @param {String} options.iamAccessToken |
| 71 | + * @param {String} options.iamUrl - url of the iam api to retrieve tokens from |
| 72 | + * @constructor |
| 73 | + */ |
| 74 | + constructor(options: Options) { |
| 75 | + super(options); |
| 76 | + |
| 77 | + this.url = this.url || options.iamUrl || 'https://iam.cloud.ibm.com/identity/token'; |
| 78 | + |
| 79 | + if (options.iamApikey) { |
| 80 | + this.iamApikey = options.iamApikey; |
| 81 | + } |
| 82 | + if (options.iamAccessToken) { |
| 83 | + this.userAccessToken = options.iamAccessToken; |
| 84 | + } |
| 85 | + if (options.iamClientId) { |
| 86 | + this.iamClientId = options.iamClientId; |
| 87 | + } |
| 88 | + if (options.iamClientSecret) { |
| 89 | + this.iamClientSecret = options.iamClientSecret; |
| 90 | + } |
| 91 | + if (onlyOne(options.iamClientId, options.iamClientSecret)) { |
| 92 | + // tslint:disable-next-line |
| 93 | + console.log(CLIENT_ID_SECRET_WARNING); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Set the IAM 'client_id' and 'client_secret' values. |
| 99 | + * These values are used to compute the Authorization header used |
| 100 | + * when retrieving the IAM access token. |
| 101 | + * If these values are not set, then a default Authorization header |
| 102 | + * will be used when interacting with the IAM token server. |
| 103 | + * |
| 104 | + * @param {string} iamClientId - The client id |
| 105 | + * @param {string} iamClientSecret - The client secret |
| 106 | + * @returns {void} |
| 107 | + */ |
| 108 | + public setIamAuthorizationInfo(iamClientId: string, iamClientSecret: string): void { |
| 109 | + this.iamClientId = iamClientId; |
| 110 | + this.iamClientSecret = iamClientSecret; |
| 111 | + if (onlyOne(iamClientId, iamClientSecret)) { |
| 112 | + // tslint:disable-next-line |
| 113 | + console.log(CLIENT_ID_SECRET_WARNING); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Callback for handling response. |
| 119 | + * |
| 120 | + * @callback requestTokenCallback |
| 121 | + * @param {Error} An error if there is one, null otherwise |
| 122 | + * @param {Object} The response if request is successful, null otherwise |
| 123 | + */ |
| 124 | + /** |
| 125 | + * Request an IAM token using an API key. |
| 126 | + * |
| 127 | + * @param {requestTokenCallback} callback - The callback that handles the response. |
| 128 | + * @returns {void} |
| 129 | + */ |
| 130 | + protected requestToken(callback: Function): void { |
| 131 | + // Use bx:bx as default auth header creds. |
| 132 | + let clientId = 'bx'; |
| 133 | + let clientSecret = 'bx'; |
| 134 | + |
| 135 | + // If both the clientId and secret were specified by the user, then use them. |
| 136 | + if (this.iamClientId && this.iamClientSecret) { |
| 137 | + clientId = this.iamClientId; |
| 138 | + clientSecret = this.iamClientSecret; |
| 139 | + } |
| 140 | + |
| 141 | + const parameters = { |
| 142 | + options: { |
| 143 | + url: this.url, |
| 144 | + method: 'POST', |
| 145 | + headers: { |
| 146 | + 'Content-type': 'application/x-www-form-urlencoded', |
| 147 | + Authorization: computeBasicAuthHeader(clientId, clientSecret), |
| 148 | + }, |
| 149 | + form: { |
| 150 | + grant_type: 'urn:ibm:params:oauth:grant-type:apikey', |
| 151 | + apikey: this.iamApikey, |
| 152 | + response_type: 'cloud_iam' |
| 153 | + }, |
| 154 | + } |
| 155 | + }; |
| 156 | + this.requestWrapperInstance.sendRequest(parameters, callback); |
| 157 | + } |
| 158 | +} |
0 commit comments