-
Notifications
You must be signed in to change notification settings - Fork 36
/
core.js
119 lines (90 loc) · 3.13 KB
/
core.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// jshint laxbreak: true
var query = require('querystring');
/**
Gatekeeper
Keeps relation to server, step, and session data outside the realm of call systems.
@author Nijiko Yonskai
@copyright Mashape, 2013, MIT License
*/
module.exports = exports = function (opts) {
var $this = this;
// Setup invocation check
this.invoked = true;
// Protected Server Relations
var request = opts.req;
var response = opts.res;
var server = { req: request, res: response };
var session;
// Handle errors and exceptions, the clean way.
this.handleException = function (message) {
if (message) log.info(message); else log.info('Unknown error!');
if (!data.done.callback || data.done.callback == "oob") return response.json(500, {
error: message
});
return response.redirect(data.done.callback + '?' + query.stringify({ error: message }));
};
// So we don't have to do re-creation
this.refreshData = function (step) {
// Protected resource
session = request.session.data;
// Public data & extension
$this.step = parseInt(step, 10);
$this.data = JSON.parse(JSON.stringify(session));
$this.plugin = exports.requirePlugin(data.auth);
if (!$this.data)
return $this.handleException('Missing session data.');
if (!$this.step > $this.plugin.steps)
return $this.handleException('All steps have been completed, authentication should be done.');
session.step = $this.step;
};
this.generateNext = function () {
var handle = {
success: function (data) {
if (!$this.data.done.callback || $this.data.done.callback == "oob")
return response.json(data);
return response.redirect($this.data.done.callback + '?' + query.stringify(data));
},
next: function () {
return $this.invokeStep($this.step + 1);
}
};
return function () {
var args = Array.prototype.slice.call(arguments), info = {};
var token_based = (args.length > 3);
// Determine response information
info.options = $this.data;
info.error = args[0];
info[token_based ? 'token' : 'data'] = args[1];
info[token_based ? 'secret' : 'response'] = args[2];
if (token_based)
info.results = args[3];
return $this.plugin.step[$this.step].next(server, info, (
($this.step + 1) > $this.plugin.steps ? handle.success : handle.next
));
};
};
this.invokeStep = function (step) {
$this.refreshData(step);
if ($this.plugin.step[$this.step].next)
$this.data.next = $this.generateNext();
return $this.plugin.step[$this.step].invoke($this.data, server);
};
return this;
};
exports.generatePluginPath = function (opts) {
return {
flow: opts.flow
? '_' + opts.flow
: '',
version: opts.version && typeof opts.version === 'number'
? '_' + opts.version
: '',
leg: opts.leg && typeof opts.leg === 'number'
? '_' + opts.leg + '-legged'
: ''
};
};
exports.requirePlugin = function (opts) {
var path = exports.generatePluginPath(opts);
return require('../plugins/' + opts.type.toLowerCase() + path.flow + path.version + path.leg + '.js');
};