|
| 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:cookietokenmanager'); |
| 17 | +const TokenManager = require('./TokenManager'); |
| 18 | + |
| 19 | +class CookieTokenManager extends TokenManager { |
| 20 | + constructor(client, jar, sessionUrl, username, password) { |
| 21 | + super(client, jar, sessionUrl); |
| 22 | + |
| 23 | + this._username = username; |
| 24 | + this._password = password; |
| 25 | + } |
| 26 | + |
| 27 | + _getToken(callback) { |
| 28 | + debug('Submitting cookie token request.'); |
| 29 | + this._client({ |
| 30 | + url: this._sessionUrl, |
| 31 | + method: 'POST', |
| 32 | + form: { |
| 33 | + name: this._username, |
| 34 | + password: this._password |
| 35 | + }, |
| 36 | + jar: this._jar |
| 37 | + }, (error, response, body) => { |
| 38 | + if (error) { |
| 39 | + debug(error); |
| 40 | + callback(error); |
| 41 | + } else if (response.statusCode === 200) { |
| 42 | + debug('Successfully renewed session cookie.'); |
| 43 | + callback(null, response); |
| 44 | + } else { |
| 45 | + let msg = `Failed to get cookie. Status code: ${response.statusCode}`; |
| 46 | + debug(msg); |
| 47 | + callback(new Error(msg), response); |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +module.exports = CookieTokenManager; |
0 commit comments