forked from osmlab/osm-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
279 lines (238 loc) · 9.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
'use strict';
var ohauth = require('ohauth');
var resolveUrl = require('resolve-url');
var store = require('store');
var xtend = require('xtend');
// # osm-auth
//
// This code is only compatible with IE10+ because the [XDomainRequest](http://bit.ly/LfO7xo)
// object, IE<10's idea of [CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing),
// does not support custom headers, which this uses everywhere.
module.exports = function(o) {
var oauth = {};
// authenticated users will also have a request token secret, but it's
// not used in transactions with the server
oauth.authenticated = function() {
return !!(token('oauth_token') && token('oauth_token_secret'));
};
oauth.logout = function() {
token('oauth_token', '');
token('oauth_token_secret', '');
token('oauth_request_token_secret', '');
return oauth;
};
// TODO: detect lack of click event
oauth.authenticate = function(callback) {
if (oauth.authenticated()) callback({
oauth_token: token('oauth_request_token'),
oauth_token_secret: token('oauth_request_token_secret')
}, oauth);
oauth.logout();
// ## Getting a request token
var params = timenonce(getAuth(o)),
url = o.url + '/oauth/request_token';
params.oauth_signature = ohauth.signature(
o.oauth_secret, '',
ohauth.baseString('POST', url, params));
if (!o.singlepage) {
// Create a 600x550 popup window in the center of the screen
var w = 600, h = 550,
settings = [
['width', w], ['height', h],
['left', screen.width / 2 - w / 2],
['top', screen.height / 2 - h / 2]].map(function(x) {
return x.join('=');
}).join(','),
popup = window.open('about:blank', 'oauth_window', settings);
}
// Request a request token. When this is complete, the popup
// window is redirected to OSM's authorization page.
ohauth.xhr('POST', url, params, null, {}, reqTokenDone);
o.loading();
function reqTokenDone(err, xhr) {
o.done();
if (err) return callback(err);
var resp = ohauth.stringQs(xhr.response);
token('oauth_request_token', resp.oauth_token);
token('oauth_request_token_secret', resp.oauth_token_secret);
var authorize_url = o.url + '/oauth/authorize?' + ohauth.qsString({
oauth_token: resp.oauth_token,
oauth_callback: resolveUrl(o.landing)
});
if (o.singlepage) {
location.href = authorize_url;
} else {
popup.location = authorize_url;
}
}
// Called by a function in a landing page, in the popup window. The
// window closes itself.
window.authComplete = function(_token) {
var oauth_token = ohauth.stringQs(_token.split('?')[1]);
//get_access_token(oauth_token.oauth_token);
callback({
oauth_token: token('oauth_request_token'),
oauth_token_secret: token('oauth_request_token_secret')
}, oauth);
delete window.authComplete;
};
// ## Getting an request token
//
// At this point we have an `oauth_token`, brought in from a function
// call on a landing page popup.
function get_access_token(oauth_token) {
var url = o.url + '/oauth/access_token',
params = timenonce(getAuth(o)),
request_token_secret = token('oauth_request_token_secret');
params.oauth_token = oauth_token;
params.oauth_signature = ohauth.signature(
o.oauth_secret,
request_token_secret,
ohauth.baseString('POST', url, params));
// ## Getting an access token
//
// The final token required for authentication. At this point
// we have a `request token secret`
ohauth.xhr('POST', url, params, null, {}, accessTokenDone);
o.loading();
}
function accessTokenDone(err, xhr) {
o.done();
if (err) return callback(err);
var access_token = ohauth.stringQs(xhr.response);
token('oauth_token', access_token.oauth_token);
token('oauth_token_secret', access_token.oauth_token_secret);
callback({
oauth_token: access_token.oauth_token,
oauth_token_secret: access_token.oauth_token_secret
}, oauth);
}
};
oauth.bootstrapToken = function(oauth_token, callback) {
// ## Getting an request token
// At this point we have an `oauth_token`, brought in from a function
// call on a landing page popup.
function get_access_token(oauth_token) {
var url = o.url + '/oauth/access_token',
params = timenonce(getAuth(o)),
request_token_secret = token('oauth_request_token_secret');
params.oauth_token = oauth_token;
params.oauth_signature = ohauth.signature(
o.oauth_secret,
request_token_secret,
ohauth.baseString('POST', url, params));
// ## Getting an access token
// The final token required for authentication. At this point
// we have a `request token secret`
ohauth.xhr('POST', url, params, null, {}, accessTokenDone);
o.loading();
}
function accessTokenDone(err, xhr) {
o.done();
if (err) return callback(err);
var access_token = ohauth.stringQs(xhr.response);
token('oauth_token', access_token.oauth_token);
token('oauth_token_secret', access_token.oauth_token_secret);
callback(null, oauth);
}
get_access_token(oauth_token);
};
// # xhr
//
// A single XMLHttpRequest wrapper that does authenticated calls if the
// user has logged in.
oauth.xhr = function(options, callback) {
if (!oauth.authenticated()) {
if (o.auto) {
return oauth.authenticate(run);
} else {
callback('not authenticated', null);
return;
}
} else {
return run();
}
function run() {
var params = timenonce(getAuth(o)),
oauth_token_secret = token('oauth_token_secret'),
url = (options.prefix !== false) ? o.url + options.path : options.path,
url_parts = url.replace(/#.*$/, '').split('?', 2),
base_url = url_parts[0],
query = (url_parts.length === 2) ? url_parts[1] : '';
// https://tools.ietf.org/html/rfc5849#section-3.4.1.3.1
if ((!options.options || !options.options.header ||
options.options.header['Content-Type'] === 'application/x-www-form-urlencoded') &&
options.content) {
params = xtend(params, ohauth.stringQs(options.content));
}
params.oauth_token = token('oauth_token');
params.oauth_signature = ohauth.signature(
o.oauth_secret,
oauth_token_secret,
ohauth.baseString(options.method, base_url, xtend(params, ohauth.stringQs(query)))
);
return ohauth.xhr(options.method, url, params, options.content, options.options, done);
}
function done(err, xhr) {
if (err) return callback(err);
else if (xhr.responseXML) return callback(err, xhr.responseXML);
else return callback(err, xhr.response);
}
};
// pre-authorize this object, if we can just get a token and token_secret
// from the start
oauth.preauth = function(c) {
if (!c) return;
if (c.oauth_token) token('oauth_token', c.oauth_token);
if (c.oauth_token_secret) token('oauth_token_secret', c.oauth_token_secret);
return oauth;
};
oauth.options = function(_) {
if (!arguments.length) return o;
o = _;
o.url = o.url || 'https://www.openstreetmap.org';
o.landing = o.landing || 'land.html';
o.singlepage = o.singlepage || false;
// Optional loading and loading-done functions for nice UI feedback.
// by default, no-ops
o.loading = o.loading || function() {};
o.done = o.done || function() {};
return oauth.preauth(o);
};
// 'stamp' an authentication object from `getAuth()`
// with a [nonce](http://en.wikipedia.org/wiki/Cryptographic_nonce)
// and timestamp
function timenonce(o) {
o.oauth_timestamp = ohauth.timestamp();
o.oauth_nonce = ohauth.nonce();
return o;
}
// get/set tokens. These are prefixed with the base URL so that `osm-auth`
// can be used with multiple APIs and the keys in `localStorage`
// will not clash
var token;
if (store.enabled) {
token = function (x, y) {
if (arguments.length === 1) return store.get(o.url + x);
else if (arguments.length === 2) return store.set(o.url + x, y);
};
} else {
var storage = {};
token = function (x, y) {
if (arguments.length === 1) return storage[o.url + x];
else if (arguments.length === 2) return storage[o.url + x] = y;
};
}
// Get an authentication object. If you just add and remove properties
// from a single object, you'll need to use `delete` to make sure that
// it doesn't contain undesired properties for authentication
function getAuth(o) {
return {
oauth_consumer_key: o.oauth_consumer_key,
oauth_signature_method: 'HMAC-SHA1'
};
}
// potentially pre-authorize
oauth.options(o);
return oauth;
};