-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathoauth2.js
154 lines (133 loc) · 4.76 KB
/
oauth2.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
import OAuthPopup from './popup.js'
import { camelCase, isFunction, isString, objectExtend, joinUrl } from '../utils.js'
/**
* Default provider configuration
* @type {Object}
*/
const defaultProviderConfig = {
name: null,
url: null,
clientId: null,
authorizationEndpoint: null,
redirectUri: null,
scope: null,
scopePrefix: null,
scopeDelimiter: null,
state: null,
requiredUrlParams: null,
defaultUrlParams: ['response_type', 'client_id', 'redirect_uri'],
responseType: 'code',
responseParams: {
code: 'code',
clientId: 'clientId',
redirectUri: 'redirectUri'
},
oauthType: '2.0',
popupOptions: {}
}
export default class OAuth2 {
constructor($http, storage, providerConfig, options) {
this.$http = $http
this.storage = storage
this.providerConfig = objectExtend({}, defaultProviderConfig)
this.providerConfig = objectExtend(this.providerConfig, providerConfig)
this.options = options
}
init(userData) {
let stateName = this.providerConfig.name + '_state';
if (isFunction(this.providerConfig.state)) {
this.storage.setItem(stateName, this.providerConfig.state())
} else if (isString(this.providerConfig.state)) {
this.storage.setItem(stateName, this.providerConfig.state)
}
let url = [this.providerConfig.authorizationEndpoint, this._stringifyRequestParams()].join('?')
this.oauthPopup = new OAuthPopup(url, this.providerConfig.name, this.providerConfig.popupOptions)
return new Promise((resolve, reject) => {
this.oauthPopup.open(this.providerConfig.redirectUri).then((response) => {
if (this.providerConfig.responseType === 'token' || !this.providerConfig.url) {
return resolve(response)
}
if (response.state && response.state !== this.storage.getItem(stateName)) {
return reject(new Error('State parameter value does not match original OAuth request state value'))
}
resolve(this.exchangeForToken(response, userData))
}).catch((err) => {
reject(err)
})
})
}
/**
* Exchange temporary oauth data for access token
* @author Sahat Yalkabov <https://github.com/sahat>
* @copyright Method taken from https://github.com/sahat/satellizer
*
* @param {[type]} oauth [description]
* @param {[type]} userData [description]
* @return {[type]} [description]
*/
exchangeForToken(oauth, userData) {
let payload = objectExtend({}, userData)
for (let key in this.providerConfig.responseParams) {
let value = this.providerConfig.responseParams[key]
switch(key) {
case 'code':
payload[value] = oauth.code
break
case 'clientId':
payload[value] = this.providerConfig.clientId
break
case 'redirectUri':
payload[value] = this.providerConfig.redirectUri
break
default:
payload[value] = oauth[key]
}
}
if (oauth.state) {
payload.state = oauth.state
}
let exchangeTokenUrl
if (this.options.baseUrl) {
exchangeTokenUrl = joinUrl(this.options.baseUrl, this.providerConfig.url)
} else {
exchangeTokenUrl = this.providerConfig.url
}
return this.$http.post(exchangeTokenUrl, payload, {
withCredentials: this.options.withCredentials
})
}
/**
* Stringify oauth params
* @author Sahat Yalkabov <https://github.com/sahat>
* @copyright Method taken from https://github.com/sahat/satellizer
*
* @return {String}
*/
_stringifyRequestParams() {
let keyValuePairs = []
let paramCategories = ['defaultUrlParams', 'requiredUrlParams', 'optionalUrlParams']
paramCategories.forEach((categoryName) => {
if (!this.providerConfig[categoryName]) return
if (!Array.isArray(this.providerConfig[categoryName])) return
this.providerConfig[categoryName].forEach((paramName) => {
let camelCaseParamName = camelCase(paramName)
let paramValue = isFunction(this.providerConfig[paramName]) ? this.providerConfig[paramName]() : this.providerConfig[camelCaseParamName]
if (paramName === 'redirect_uri' && !paramValue) return
if (paramName === 'state') {
let stateName = this.providerConfig.name + '_state';
paramValue = encodeURIComponent(this.storage.getItem(stateName));
}
if (paramName === 'scope' && Array.isArray(paramValue)) {
paramValue = paramValue.join(this.providerConfig.scopeDelimiter);
if (this.providerConfig.scopePrefix) {
paramValue = [this.providerConfig.scopePrefix, paramValue].join(this.providerConfig.scopeDelimiter);
}
}
keyValuePairs.push([paramName, paramValue])
})
})
return keyValuePairs.map((param) => {
return param.join('=')
}).join('&')
}
}