1
0
Fork 0
This repository has been archived on 2022-08-25. You can view files and clone it, but cannot push or open issues or pull requests.
office365-oauth2-authenticator/src/cookie-bag.js
2018-09-11 10:36:58 +02:00

54 lines
1 KiB
JavaScript

var trim = require('trim');
var CookieBag = function() {
this.bag = [];
}
CookieBag.prototype.add = function(name, value) {
this.bag[name] = name + '=' + value;
}
CookieBag.prototype.get = function(name) {
var str = this.bag[name] || null;
if (!str) {
return;
}
var index = str.indexOf('=');
return str.substr(index + 1);
}
CookieBag.prototype.toString = function() {
var data = [];
for (var i in this.bag) {
data.push(this.bag[i]);
}
return data.join('; ');
}
CookieBag.prototype.handleResponse = function(response) {
var headers = response.headers || {};
var cookies = headers['set-cookie'] || [];
var that = this;
cookies.forEach(function(cookie) {
var def = cookie.split(';')[0];
var index = def.indexOf('=');
var name = def.substr(0, index);
var value = def.substr(index + 1);
that.add(name, value);
});
}
CookieBag.prototype.clean = function() {
this.bag = [];
}
module.exports = CookieBag;