-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (72 loc) · 2.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict'
const Logger = require('./lib/logger');
const AuthTrait = require('./lib/traits/auth');
const TokenTrait = require('./lib/traits/token');
const AttachTrait = require('./lib/traits/attach');
const CookieTrait = require('./lib/traits/cookies');
const SessionIdTrait = require('./lib/traits/session-id');
const SendRequestTrait = require('./lib/traits/send-request');
module.exports = SSOBroker;
function SSOBroker(window, url, brokerId, secret, cookieLifetime, debug) {
const self = this;
this.window = window;
this.url = url;
this.brokerId = brokerId;
this.secret = secret;
this.cookieLifetime = cookieLifetime ? cookieLifetime * 1000 : 1000 * 3600; //1 hour default (in milliseconds)
this.cookieName = null;
this.token = null;
this.userInfo = null;
this.logger = new Logger(debug);
/**
* Init broker on creation
* @return {[type]} [description]
*/
this.init = function() {
if (!this.window) throw "Window object is not specified";
if (!this.brokerId) throw "SSO broker id not specified";
if (!this.secret) throw "SSO broker secret not specified";
if (!this.url) throw "SSO server URL not specified";
if (this.url.substr(0, 1) === '/') {
this.url = this.window.location.hostname + this.url;
}
this.useTrait(new CookieTrait());
this.useTrait(new TokenTrait());
this.useTrait(new SessionIdTrait());
this.useTrait(new AttachTrait());
this.useTrait(new SendRequestTrait());
this.useTrait(new AuthTrait());
this.log('Debug mode enabled');
this.cookieName = this.createCookieName();
this.token = this.getCookie(this.cookieName);
}
/**
* Use methods and properties from trait (helper object)
* @param {object} object
*/
this.useTrait = function(object) {
for (var name in object) {
this[name] = object[name];
}
};
/**
* Get user info
* @return {Promise}
*/
this.getUserInfo = function() {
if (this.userInfo) {
return new Promise((resolve, reject) => {
resolve(self.userInfo);
});
}
return this.sendGETRequest(this.url, {command: 'userInfo'});
}
/**
* Perform debug logging
* @param {mixed} message
*/
this.log = function(message) {
this.logger.log(message);
}
this.init();
}