-
Notifications
You must be signed in to change notification settings - Fork 0
/
spark.js
99 lines (89 loc) · 2.92 KB
/
spark.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
const axios = require('axios');
const utilities = require('./utilities');
// Demo
//API Key (client_id): bqt7g0oqvo1hf4vv8ukrrb6ca
//Secret (client_secret): 3zbtc4pqw0u79b0bdl3ukdjwj
//Redirect URI (redirect_uri): https://sparkplatform.com/oauth2/callback
// App
//f199xn72zbpo1bhreekydm4g4
//6c77qp7i57jmssuw6xd4fugit
//https://sparkplatform.com/openid/authorize?client_id=1234&scope=openid&response_type=code&redirect_uri=https://example.com/callback&state=abcdefgh&nonce=zyxwvuts
exports.getAuthorizationUrl = function(clientId){
return new Promise(function(resolve, reject){
var baseUrl = "https://sparkplatform.com/openid/authorize";
var authUrl =
baseUrl +
"?client_id=" +
clientId +
"&scope=openid&response_type=code&redirect_uri=";
resolve(authUrl);
});
}
exports.getAccessToken = function(body){
return new Promise(function(resolve, reject){
var baseUrl = "https://sparkplatform.com/openid/token";
var data = {
client_id: body.clientId,
client_secret: process.env.SPARK_CLIENT_SECRET,
grant_type: "authorization_code",
code: body.code,
redirect_uri: body.redirect_uri
};
var options = {
url: baseUrl,
method: 'POST',
data: data
};
axios(options).then(function(result){
resolve(result);
}).catch(function(err){
reject(utilities.processAxiosError(err));
});
});
}
exports.refreshAccessToken = function(body){
return new Promise(function(resolve, reject){
var baseUrl = "https://sparkplatform.com/openid/token";
var data = {
client_id: body.clientId,
client_secret: process.env.SPARK_CLIENT_SECRET,
grant_type: "refresh_token",
refresh_token: body.refresh_token
};
var options = {
url: baseUrl,
method: 'POST',
data: data
};
axios(options).then(function(result){
resolve(result);
}).catch(function(err){
reject(utilities.processAxiosError(err));
});
});
}
exports.deleteAccessToken = function(token){
return new Promise(function(resolve, reject){
var url = "https://sparkapi.com/v1/oauth2/token/" + token;
var options = {
url: url,
method: 'DELETE'
}
axios(options).then(function(result){
resolve(result);
}).catch(function(err){
reject(utilities.processAxiosError(err));
});
});
}
exports.getLogoutUrl = function(clientId){
return new Promise(function(resolve, reject){
var baseUrl = "https://sparkplatform.com/openid/logout";
var logoutUrl =
baseUrl +
"?client_id=" +
clientId +
"&post_logout_redirect_uri=";
resolve(logoutUrl);
});
}