This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
oauth-client.js
83 lines (70 loc) · 2.14 KB
/
oauth-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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
define([
'jquery',
'lib/promise',
'lib/session',
'lib/config-loader',
'lib/oauth-errors'
],
function ($, p, Session, ConfigLoader, OAuthErrors) {
var oauthUrl;
var GET_CLIENT = '/v1/client/';
var GET_CODE = '/v1/authorization';
function OAuthClient(options) {
if (!(this instanceof OAuthClient)) {
return new OAuthClient(options);
}
if (options && options.oauthUrl) {
oauthUrl = options.oauthUrl;
}
}
OAuthClient.prototype = {
_getOauthUrl: function _getOauthUrl() {
var configLoader = new ConfigLoader();
var promise;
if (oauthUrl) {
promise = p(oauthUrl);
} else if (Session.config && Session.config.oauthUrl) {
oauthUrl = Session.config.oauthUrl;
promise = p(oauthUrl);
} else {
promise = configLoader.fetch()
.then(function (data) {
oauthUrl = data.oauthUrl;
return oauthUrl;
});
}
return promise;
},
// params = { assertion, client_id, redirect_uri, scope, state }
getCode: function getCode(params) {
return this._getOauthUrl().then(function (url) {
return p.jQueryXHR($.post(url + GET_CODE, params))
.then(null, function(xhr) {
var err = OAuthErrors.normalizeXHRError(xhr);
throw err;
});
});
},
getClientInfo: function getClientInfo(id) {
return this._getOauthUrl().then(function (url) {
return p.jQueryXHR($.get(url + GET_CLIENT + id))
.then(null, function(xhr) {
var err = OAuthErrors.normalizeXHRError(xhr);
throw err;
});
});
},
// params = { assertion, client_id, scope }
getToken: function (params) {
/* jshint camelcase: false */
// Use the special 'token' response type
params.response_type = 'token';
return this.getCode(params);
}
};
return OAuthClient;
});