forked from oauthjs/express-oauth-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (136 loc) · 3.71 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
157
158
159
160
161
162
163
164
165
'use strict';
/**
* 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.useErrorHandler = options.useErrorHandler ? true : false;
delete options.useErrorHandler;
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 that = this;
return function(req, res, next) {
var request = new Request(req);
var response = new Response(res);
return Promise.bind(that)
.then(function() {
return this.server.authenticate(request, response, options);
})
.tap(function(token) {
res.locals.oauth = { token: token };
next();
})
.catch(function(e) {
return handleError.call(this, e, req, res, null, next);
});
};
};
/**
* 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 that = this;
return function(req, res, next) {
var request = new Request(req);
var response = new Response(res);
return Promise.bind(that)
.then(function() {
return this.server.authorize(request, response, options);
})
.tap(function(code) {
res.locals.oauth = { code: code };
})
.then(function() {
return handleResponse.call(this, req, res, response);
})
.catch(function(e) {
return handleError.call(this, e, req, res, response, 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 that = this;
return function(req, res, next) {
var request = new Request(req);
var response = new Response(res);
return Promise.bind(that)
.then(function() {
return this.server.token(request, response, options);
})
.tap(function(token) {
res.locals.oauth = { token: token };
})
.then(function() {
return handleResponse.call(this, req, res, response);
})
.catch(function(e) {
return handleError.call(this, e, req, res, response, next);
});
};
};
/**
* Handle response.
*/
var handleResponse = function(req, res, response) {
if (response.status === 302) {
var location = response.headers.location;
delete response.headers.location;
res.set(response.headers);
res.redirect(location);
} else {
res.set(response.headers);
res.status(response.status).send(response.body);
}
};
/**
* Handle error.
*/
var handleError = function(e, req, res, response, next) {
if (this.useErrorHandler === true) {
next(e);
} else {
if (response) {
res.set(response.headers);
}
res.status(e.code);
if (e instanceof UnauthorizedRequestError) {
return res.send();
}
res.send({ error: e.name, error_description: e.message });
}
};
/**
* Export constructor.
*/
module.exports = ExpressOAuthServer;