forked from ssharwan/express-oauth-server
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
156 lines (129 loc) · 3.32 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
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
/**
* Module dependencies.
*/
var InvalidArgumentError = require('oauth2-server/lib/errors/invalid-argument-error');
var NodeOAuthServer = require('oauth2-server');
var Promise = require('bluebird');
var Request = require('oauth2-server').Request;
var Response = require('oauth2-server').Response;
var UnauthorizedRequestError = require('oauth2-server/lib/errors/unauthorized-request-error');
/**
* Constructor.
*/
function ExpressOAuthServer(options) {
options = options || {};
if (!options.model) {
throw new InvalidArgumentError('Missing parameter: `model`');
}
this.server = new NodeOAuthServer(options);
}
/**
* Authentication Middleware.
*
* Returns a middleware that will validate a token.
*
* (See: https://tools.ietf.org/html/rfc6749#section-7)
*/
ExpressOAuthServer.prototype.authenticate = function(options) {
var server = this.server;
return function(req, res, next) {
var request = new Request(req);
var response = new Response(res);
return Promise.bind(this)
.then(function() {
return server.authenticate(request, response, options);
})
.tap(function(token) {
res.locals.oauth = { token: token };
req.token = token;
})
.then(function(){
next();
})
.catch(function(e) {
return next(e);
})
};
};
/**
* Authorization Middleware.
*
* Returns a middleware that will authorize a client to request tokens.
*
* (See: https://tools.ietf.org/html/rfc6749#section-3.1)
*/
ExpressOAuthServer.prototype.authorize = function(options) {
var server = this.server;
return function(req, res, next) {
var request = new Request(req);
var response = new Response(res);
return Promise.bind(this)
.then(function() {
return server.authorize(request, response, options);
})
.tap(function(code) {
res.locals.oauth = { code: code };
next();
})
.then(function() {
return handleResponse(req, res, response);
})
.catch(function(e) {
return handleError(e, req, res, response);
})
.finally(next);
};
};
/**
* Grant Middleware.
*
* Returns middleware that will grant tokens to valid requests.
*
* (See: https://tools.ietf.org/html/rfc6749#section-3.2)
*/
ExpressOAuthServer.prototype.token = function(options) {
var server = this.server;
return function(req, res, next) {
var request = new Request(req);
var response = new Response(res);
return Promise.bind(this)
.then(function() {
return server.token(request, response, options);
})
.tap(function(token) {
res.locals.oauth = { token: token };
req.token = token;
})
.then(function() {
next();
})
.catch(function(e) {
return next(e);
});
};
};
/**
* Handle response.
*/
var handleResponse = function(req, res, response) {
res.set(response.headers);
res.send(response.body);
res.status(response.status);
};
/**
* Handle error.
*/
var handleError = function(e, req, res, response) {
if (response) {
res.set(response.headers);
}
if (e instanceof UnauthorizedRequestError) {
return res.status(e.code);
}
res.send({ error: e.name, error_description: e.message });
res.status(e.code);
};
/**
* Export constructor.
*/
module.exports = ExpressOAuthServer;