|
| 1 | +// Copyright © 2019 IBM Corp. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +const debug = require('debug')('cloudant:tokens:tokenmanager'); |
| 17 | +const cookie = require('cookie'); |
| 18 | +const EventEmitter = require('events'); |
| 19 | + |
| 20 | +class TokenManager { |
| 21 | + constructor(client, jar, sessionUrl) { |
| 22 | + this._client = client; |
| 23 | + this._jar = jar; |
| 24 | + this._sessionUrl = sessionUrl; |
| 25 | + |
| 26 | + this._attemptTokenRenewal = true; |
| 27 | + this._isTokenRenewing = false; |
| 28 | + |
| 29 | + this._tokenExchangeEE = new EventEmitter().setMaxListeners(Infinity); |
| 30 | + } |
| 31 | + |
| 32 | + _autoRenew(defaultMaxAgeSecs) { |
| 33 | + debug('Auto renewing token now...'); |
| 34 | + this._renew().then((response) => { |
| 35 | + let maxAgeSecs = cookie.parse(response.headers['set-cookie'][0])['Max-Age'] || defaultMaxAgeSecs; |
| 36 | + let delayMSecs = maxAgeSecs / 2 * 1000; |
| 37 | + debug(`Renewing token in ${delayMSecs} milliseconds.`); |
| 38 | + setTimeout(this._autoRenew.bind(this), delayMSecs).unref(); |
| 39 | + }).catch((error) => { |
| 40 | + debug(`Failed to auto renew token - ${error}. Retrying in 60 seconds.`); |
| 41 | + setTimeout(this._autoRenew.bind(this), 60000).unref(); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + _getToken(callback) { |
| 46 | + // ** Method to be implemented by _all_ subclasses of `TokenManager` ** |
| 47 | + throw new Error('Not implemented.'); |
| 48 | + } |
| 49 | + |
| 50 | + // Renew the token. |
| 51 | + _renew() { |
| 52 | + if (!this._isTokenRenewing) { |
| 53 | + this._isTokenRenewing = true; |
| 54 | + this._tokenExchangeEE.removeAllListeners(); |
| 55 | + debug('Starting token renewal.'); |
| 56 | + this._getToken((error, response) => { |
| 57 | + if (error) { |
| 58 | + this._tokenExchangeEE.emit('error', error, response); |
| 59 | + } else { |
| 60 | + this._tokenExchangeEE.emit('success', response); |
| 61 | + this._attemptTokenRenewal = false; |
| 62 | + } |
| 63 | + debug('Finished token renewal.'); |
| 64 | + this._isTokenRenewing = false; |
| 65 | + }); |
| 66 | + } |
| 67 | + return new Promise((resolve, reject) => { |
| 68 | + this._tokenExchangeEE.once('success', resolve); |
| 69 | + this._tokenExchangeEE.once('error', (error, response) => { |
| 70 | + error.response = response; |
| 71 | + reject(error); |
| 72 | + }); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + // Getter for `attemptTokenRenewal`. |
| 77 | + // - `true` A renewal attempt will be made on the next `renew` request. |
| 78 | + // - `false` No renewal attempt will be made on the next `renew` |
| 79 | + // request. Instead the last good renewal response will be returned |
| 80 | + // to the client. |
| 81 | + get attemptTokenRenewal() { |
| 82 | + return this._attemptTokenRenewal; |
| 83 | + } |
| 84 | + |
| 85 | + // Getter for `isTokenRenewing`. |
| 86 | + // - `true` A renewal attempt is in progress. |
| 87 | + // - `false` There are no in progress renewal attempts. |
| 88 | + get isTokenRenewing() { |
| 89 | + return this._isTokenRenewing; |
| 90 | + } |
| 91 | + |
| 92 | + // Settter for `attemptTokenRenewal`. |
| 93 | + set attemptTokenRenewal(newAttemptTokenRenewal) { |
| 94 | + this._attemptTokenRenewal = newAttemptTokenRenewal; |
| 95 | + } |
| 96 | + |
| 97 | + // Renew the token if `attemptTokenRenewal` is `true`. Otherwise this is a |
| 98 | + // no-op so just resolve the promise. |
| 99 | + renewIfRequired() { |
| 100 | + if (this._attemptTokenRenewal) { |
| 101 | + return this._renew(); |
| 102 | + } else { |
| 103 | + return new Promise((resolve) => { |
| 104 | + resolve(); |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Start the auto renewal timer. |
| 110 | + startAutoRenew(defaultMaxAgeSecs) { |
| 111 | + this._autoRenew(defaultMaxAgeSecs || 3600); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +module.exports = TokenManager; |
0 commit comments