-
Notifications
You must be signed in to change notification settings - Fork 81
/
index.js
149 lines (116 loc) · 2.94 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
/**
* Module dependencies.
*/
var InvalidArgumentError = require('oauth2-server/lib/errors/invalid-argument-error');
var NodeOAuthServer = require('oauth2-server');
var Request = require('oauth2-server').Request;
var Response = require('oauth2-server').Response;
var UnauthorizedRequestError = require('oauth2-server/lib/errors/unauthorized-request-error');
var co = require('co');
/**
* Constructor.
*/
function KoaOAuthServer(options) {
options = options || {};
if (!options.model) {
throw new InvalidArgumentError('Missing parameter: `model`');
}
for (var fn in options.model) {
options.model[fn] = co.wrap(options.model[fn]);
}
this.server = new NodeOAuthServer(options);
}
/**
* Authentication Middleware.
*
* Returns a middleware that will validate a token.
*
* (See: https://tools.ietf.org/html/rfc6749#section-7)
*/
KoaOAuthServer.prototype.authenticate = function() {
var server = this.server;
return function *(next) {
var request = new Request(this.request);
try {
this.state.oauth = {
token: yield server.authenticate(request)
};
} catch (e) {
return handleError.call(this, e);
}
yield* next;
};
};
/**
* Authorization Middleware.
*
* Returns a middleware that will authorize a client to request tokens.
*
* (See: https://tools.ietf.org/html/rfc6749#section-3.1)
*/
KoaOAuthServer.prototype.authorize = function() {
var server = this.server;
return function *(next) {
var request = new Request(this.request);
var response = new Response(this.response);
try {
this.state.oauth = {
code: yield server.authorize(request, response)
};
handleResponse.call(this, response);
} catch (e) {
return handleError.call(this, e, response);
}
yield* next;
};
};
/**
* Grant Middleware
*
* Returns middleware that will grant tokens to valid requests.
*
* (See: https://tools.ietf.org/html/rfc6749#section-3.2)
*/
KoaOAuthServer.prototype.token = function() {
var server = this.server;
return function *(next) {
var request = new Request(this.request);
var response = new Response(this.response);
try {
this.state.oauth = {
token: yield server.token(request, response)
};
handleResponse.call(this, response);
} catch (e) {
return handleError.call(this, e, response);
}
yield* next;
};
};
/**
* Handle response.
*/
var handleResponse = function(response) {
this.body = response.body;
this.status = response.status;
this.set(response.headers);
};
/**
* Handle error.
*/
var handleError = function(e, response) {
if (response) {
this.set(response.headers);
}
if (e instanceof UnauthorizedRequestError) {
this.status = e.code;
} else {
this.body = { error: e.name, error_description: e.message };
this.status = e.code;
}
return this.app.emit('error', e, this);
};
/**
* Export constructor.
*/
module.exports = KoaOAuthServer;