-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathoauth_gateway.js
104 lines (80 loc) · 2.92 KB
/
oauth_gateway.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
'use strict';
let Gateway = require('./gateway').Gateway;
let OAuthCredentials = require('./oauth_credentials').OAuthCredentials;
let AttributeSetter = require('./attribute_setter').AttributeSetter;
let Util = require('./util').Util;
let Digest = require('./digest').Digest;
let wrapPrototype = require('@braintree/wrap-promise').wrapPrototype;
class OAuthGateway extends Gateway {
constructor(gateway) {
super();
this.gateway = gateway;
this.config = this.gateway.config;
}
createTokenFromCode(attributes) {
attributes.grantType = 'authorization_code';
return this.gateway.http.post('/oauth/access_tokens', attributes).then(this.responseHandler());
}
createTokenFromRefreshToken(attributes) {
attributes.grantType = 'refresh_token';
return this.gateway.http.post('/oauth/access_tokens', attributes).then(this.responseHandler());
}
revokeAccessToken(accessToken) {
return this.gateway.http.post('/oauth/revoke_access_token', {token: accessToken}).then(this.createResponseHandler('result', AttributeSetter));
}
responseHandler() {
return this.createResponseHandler('credentials', OAuthCredentials);
}
connectUrl(params) {
params.clientId = this.config.clientId;
let url = this.config.baseUrl() + '/oauth/connect?' + this.buildQuery(params);
let signature = Digest.Sha256hexdigest(this.config.clientSecret, url);
return url + `&signature=${signature}&algorithm=SHA256`;
}
buildQuery(params) {
params = Util.convertObjectKeysToUnderscores(params);
let paramsArray = this.buildSubQuery('user', params.user);
paramsArray.push.apply(paramsArray, this.buildSubQuery('business', params.business));
paramsArray.push.apply(paramsArray, this.buildSubArrayQuery('payment_methods', params.payment_methods));
delete params.user;
delete params.business;
delete params.payment_methods;
paramsArray.push.apply(paramsArray, (() => {
let result = [];
for (let key in params) {
if (!params.hasOwnProperty(key)) {
continue;
}
let val = params[key];
result.push([key, val]);
}
return result;
})());
let queryStringParts = paramsArray.map((paramParts) => {
let key = paramParts[0];
let value = paramParts[1];
return `${this._encodeValue(key)}=${this._encodeValue(value)}`;
});
return queryStringParts.join('&');
}
buildSubQuery(key, subParams) {
let arr = [];
for (let subKey in subParams) {
if (!subParams.hasOwnProperty(subKey)) {
continue;
}
let value = subParams[subKey];
arr.push([`${key}[${subKey}]`, value]);
}
return arr;
}
_encodeValue(value) {
return encodeURIComponent(value)
.replace(/[!'()]/g, escape)
.replace(/\*/g, '%2A');
}
buildSubArrayQuery(key, values) {
return (values || []).map(value => [`${key}[]`, value]);
}
}
module.exports = {OAuthGateway: wrapPrototype(OAuthGateway)};