|
| 1 | +/** |
| 2 | + * (C) Copyright IBM Corp. 2022. |
| 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 from 'extend'; |
| 18 | +import { AxiosRequestConfig, AxiosResponse } from 'axios'; |
| 19 | +import { Cookie, CookieJar } from 'tough-cookie'; |
| 20 | +import logger from './logger'; |
| 21 | + |
| 22 | +export class CookieInterceptor { |
| 23 | + private readonly cookieJar: CookieJar; |
| 24 | + |
| 25 | + constructor(cookieJar: CookieJar | boolean) { |
| 26 | + if (cookieJar) { |
| 27 | + if (cookieJar === true) { |
| 28 | + logger.debug('CookieInterceptor: creating new CookieJar'); |
| 29 | + this.cookieJar = new CookieJar(); |
| 30 | + } else { |
| 31 | + logger.debug('CookieInterceptor: using supplied CookieJar'); |
| 32 | + this.cookieJar = cookieJar; |
| 33 | + } |
| 34 | + } else { |
| 35 | + throw new Error('Must supply a cookie jar or true.'); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public async requestInterceptor(config: AxiosRequestConfig) { |
| 40 | + logger.debug('CookieInterceptor: intercepting request'); |
| 41 | + if (config && config.url) { |
| 42 | + logger.debug(`CookieInterceptor: getting cookies for: ${config.url}`); |
| 43 | + const cookieHeaderValue = await this.cookieJar.getCookieString(config.url); |
| 44 | + if (cookieHeaderValue) { |
| 45 | + logger.debug('CookieInterceptor: setting cookie header'); |
| 46 | + const cookieHeader = { cookie: cookieHeaderValue }; |
| 47 | + config.headers = extend(true, {}, config.headers, cookieHeader); |
| 48 | + } else { |
| 49 | + logger.debug(`CookieInterceptor: no cookies for: ${config.url}`); |
| 50 | + } |
| 51 | + } else { |
| 52 | + logger.debug('CookieInterceptor: no request URL.'); |
| 53 | + } |
| 54 | + return config; |
| 55 | + } |
| 56 | + |
| 57 | + public async responseInterceptor(response: AxiosResponse) { |
| 58 | + logger.debug('CookieInterceptor: intercepting response.'); |
| 59 | + if (response && response.headers) { |
| 60 | + logger.debug('CookieInterceptor: checking for set-cookie headers.'); |
| 61 | + const cookies: string[] = response.headers['set-cookie']; |
| 62 | + if (cookies) { |
| 63 | + logger.debug(`CookieInterceptor: setting cookies in jar for URL ${response.config.url}.`); |
| 64 | + // Write cookies sequentially by chaining the promises in a reduce |
| 65 | + await cookies.reduce( |
| 66 | + (cookiePromise: Promise<Cookie>, cookie: string) => |
| 67 | + cookiePromise.then(() => this.cookieJar.setCookie(cookie, response.config.url)), |
| 68 | + Promise.resolve(null) |
| 69 | + ); |
| 70 | + } else { |
| 71 | + logger.debug('CookieInterceptor: no set-cookie headers.'); |
| 72 | + } |
| 73 | + } else { |
| 74 | + logger.debug('CookieInterceptor: no response headers.'); |
| 75 | + } |
| 76 | + return response; |
| 77 | + } |
| 78 | +} |
0 commit comments