This repository was archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfilters.js
More file actions
186 lines (169 loc) · 5.16 KB
/
filters.js
File metadata and controls
186 lines (169 loc) · 5.16 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
var Promise = require("bluebird");
var pkg = require('./package.json');
var _ = require('underscore');
function handleError(handler,err,req,res,next){
var self = this;
if(typeof self.errorHandler==='function'){
self.errorHandler(err,req,res,next);
}
else if(typeof handler==='function'){
handler(err,req,res,next);
}else{
res.json(err.status || 500,err);
}
}
function createOauthFilter(_options){
var self = this;
var opts = typeof _options === 'object' ? _options : {};
return function _authenticateApiRequest(req,res,next){
self.getApplication.then(function(application){
application.authenticateApiRequest(_.extend({
request:req
},opts),function(err,authResult){
if(err){
self.handleError(opts.errorHandler,err,req,res,next);
}else if(authResult.tokenResponse){
res.send(authResult.tokenResponse);
}else{
authResult.getAccount(function(err,account){
if(err){
self.handleError(opts.errorHandler,err,req,res,next);
}else{
req.account = account;
next();
}
});
}
});
});
};
}
function newAccountFilter(_options){
var self = this;
var opts = typeof _options === 'object' ? _options : {};
return function _handleAccountCreationRequest(req,res,next){
self.getApplication.then(function(application){
application.createAccount(req.body,function(err){
if(err){
self.handleError(opts.errorHandler,err,req,res,next);
}else{
res.json(201,{message:'Please check your email for verification'});
}
});
});
};
}
function accountVerificationFilter(_options){
var self = this;
var opts = typeof _options === 'object' ? _options : {};
return function _handleAccountVerificationRequest(req,res,next){
self.getCurrentTenant.then(function(tenant){
tenant.verifyAccountEmail(req.params.sptoken,function(err,account){
if(err){
self.handleError(opts.errorHandler,err,req,res,next);
}else{
account.createApiKey(function(err,apiKey){
if(err){
self.handleError(opts.errorHandler,err,req,res,next);
}else{
var body = 'Api Key Id: ' + apiKey.id + '\n' + 'Api Key Secret: ' + apiKey.secret;
res.writeHead(200, {
'Content-Length': Buffer.byteLength(body),
'Content-Type': 'text/plain'
});
res.write(body);
}
});
}
});
});
};
}
function createGroupFilter(_options){
var self = this;
var opts = typeof _options === 'object' ? _options : {};
if(!opts.inGroup){
throw new Error('inGroup is the only supported filter at this time. PR welcome!');
}
opts.inGroup = typeof opts.inGroup === 'string' ? [opts.inGroup] : opts.inGroup;
return function groupFilter(req,res,next){
req.account.getGroups(function(err,collection){
if(err){
next(err);
}else{
collection.detect(function(group,cb){
cb(opts.inGroup.indexOf(group.name) >= 0);
},function(result){
if(result){
next();
}else{
if(typeof errorHandler==='function'){
self.handleError(opts.errorHandler,err,req,res,next);
}else{
res.send(403);
}
}
});
}
});
};
}
function createFilterSet(_options){
var opts = typeof _options === 'object' ? _options : {};
var self = this;
var stormpath = opts.stormpathLib || require('stormpath');
opts.userAgent = 'restify-stormpath/' + pkg.version + ' ' + 'restify/' + require('restify/package').version;
if(typeof opts.errorHandler==='function'){
self.errorHandler = opts.errorHandler;
}
if(opts.spClient){
self.spClient = opts.spClient;
}else{
self.spClient = new stormpath.Client(opts);
}
console.log('Initializing Stormpath');
self.getApplication = new Promise(function(resolve,reject){
self.spClient.on('ready', function(){
self.spClient.getApplication(self.spClient.config.application.href,
function(err,app){
if(err){
reject(err);
}else{
resolve(app);
}
}
);
});
});
self.getApplication.then(function(app){
console.log('Using Stormpath application \'' + app.name + '\'');
});
self.getApplication.catch(function(err){
throw err;
});
self.getCurrentTenant = new Promise(function(resolve,reject){
self.spClient.on('ready', function(){
self.spClient.getCurrentTenant(
function(err,tenant){
if(err){
reject(err);
}else{
resolve(tenant);
}
}
);
});
});
self.getCurrentTenant.catch(function(err){
throw err;
});
self.createOauthFilter = createOauthFilter.bind(self);
self.createGroupFilter = createGroupFilter.bind(self);
self.newAccountFilter = newAccountFilter.bind(self);
self.accountVerificationFilter = accountVerificationFilter.bind(self);
self.handleError = handleError.bind(self);
return this;
}
module.exports = {
createFilterSet: createFilterSet
};