-
Notifications
You must be signed in to change notification settings - Fork 517
/
client.js
202 lines (159 loc) · 4.96 KB
/
client.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var express = require("express");
var bodyParser = require('body-parser');
var request = require("sync-request");
var url = require("url");
var qs = require("qs");
var querystring = require('querystring');
var cons = require('consolidate');
var randomstring = require("randomstring");
var jose = require('jsrsasign');
var base64url = require('base64url');
var __ = require('underscore');
__.string = require('underscore.string');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.engine('html', cons.underscore);
app.set('view engine', 'html');
app.set('views', 'files/client');
// client information
var client = {
"client_id": "oauth-client-1",
"client_secret": "oauth-client-secret-1",
"redirect_uris": ["http://localhost:9000/callback"],
"scope": "foo bar"
};
// authorization server information
var authServer = {
authorizationEndpoint: 'http://localhost:9001/authorize',
tokenEndpoint: 'http://localhost:9001/token',
revocationEndpoint: 'http://localhost:9001/revoke'
};
var protectedResource = 'http://localhost:9002/resource';
var state = null;
var access_token = null;
var refresh_token = null;
var scope = null;
var id_token = null;
var userInfo = null;
app.get('/', function (req, res) {
res.render('index', {access_token: access_token, refresh_token: refresh_token, scope: scope});
});
app.get('/authorize', function(req, res){
access_token = null;
refresh_token = null;
scope = null;
state = randomstring.generate();
var authorizeUrl = buildUrl(authServer.authorizationEndpoint, {
response_type: 'code',
scope: client.scope,
client_id: client.client_id,
redirect_uri: client.redirect_uris[0],
state: state
});
console.log("redirect", authorizeUrl);
res.redirect(authorizeUrl);
});
app.get("/callback", function(req, res){
if (req.query.error) {
// it's an error response, act accordingly
res.render('error', {error: req.query.error});
return;
}
var resState = req.query.state;
if (resState == state) {
console.log('State value matches: expected %s got %s', state, resState);
} else {
console.log('State DOES NOT MATCH: expected %s got %s', state, resState);
res.render('error', {error: 'State value did not match'});
return;
}
var code = req.query.code;
var form_data = qs.stringify({
grant_type: 'authorization_code',
code: code,
redirect_uri: client.redirect_uris[0]
});
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + encodeClientCredentials(client.client_id, client.client_secret)
};
var tokRes = request('POST', authServer.tokenEndpoint,
{
body: form_data,
headers: headers
}
);
console.log('Requesting access token for code %s',code);
if (tokRes.statusCode >= 200 && tokRes.statusCode < 300) {
var body = JSON.parse(tokRes.getBody());
access_token = body.access_token;
console.log('Got access token: %s', access_token);
if (body.refresh_token) {
refresh_token = body.refresh_token;
console.log('Got refresh token: %s', refresh_token);
}
scope = body.scope;
console.log('Got scope: %s', scope);
res.render('index', {access_token: access_token, refresh_token: refresh_token, scope: scope});
} else {
res.render('error', {error: 'Unable to fetch access token, server response: ' + tokRes.statusCode})
}
});
app.get('/fetch_resource', function(req, res) {
if (!access_token) {
res.render('error', {error: 'Missing access token.'});
return;
}
console.log('Making request with access token %s', access_token);
var headers = {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/x-www-form-urlencoded'
};
var resource = request('POST', protectedResource,
{headers: headers}
);
if (resource.statusCode >= 200 && resource.statusCode < 300) {
var body = JSON.parse(resource.getBody());
res.render('data', {resource: body});
return;
} else {
access_token = null;
if (refresh_token) {
// try to refresh and start again
refreshAccessToken(req, res);
return;
} else {
res.render('error', {error: 'Server returned response code: ' + resource.statusCode});
return;
}
}
});
app.post('/revoke', function(req, res) {
/*
* Call the token revocation endpoint and throw out all our tokens
*/
});
app.use('/', express.static('files/client'));
var buildUrl = function(base, options, hash) {
var newUrl = url.parse(base, true);
delete newUrl.search;
if (!newUrl.query) {
newUrl.query = {};
}
__.each(options, function(value, key, list) {
newUrl.query[key] = value;
});
if (hash) {
newUrl.hash = hash;
}
return url.format(newUrl);
};
var encodeClientCredentials = function(clientId, clientSecret) {
return new Buffer(querystring.escape(clientId) + ':' + querystring.escape(clientSecret)).toString('base64');
};
var server = app.listen(9000, 'localhost', function () {
var host = server.address().address;
var port = server.address().port;
console.log('OAuth Client is listening at http://%s:%s', host, port);
});