-
Notifications
You must be signed in to change notification settings - Fork 0
/
procountor-authentication.js
86 lines (79 loc) · 2.48 KB
/
procountor-authentication.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
"use strict";
const fetch = require("node-fetch");
class ProcountorAuthentication {
constructor(baseUrl, clientId, clientSecret, redirectUri) {
this.baseUrl = baseUrl;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.redirectUri = redirectUri;
}
loginUrl() {
return (
this.baseUrl +
"login?response_type=code&client_id=" +
this.clientId +
"&redirect_uri=" +
this.redirectUri +
"&state=test"
);
}
getToken(code) {
var encodedRedirectUri = encodeURIComponent(this.redirectUri);
return fetch(
this.baseUrl +
"api/oauth/token?grant_type=authorization_code&redirect_uri=" +
encodedRedirectUri +
"&code=" +
code +
"&client_id=" +
this.clientId +
"&client_secret=" +
this.clientSecret,
{
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" }
}
)
.then(tokenResponse => {
if (!tokenResponse.ok) {
throw new Error(tokenResponse);
}
return tokenResponse.json();
})
.then(json => {
return {
token: json.access_token,
lifetimeInSeconds: json.expires_in,
refreshToken: json.refresh_token
};
});
}
getTokenWithRefreshToken(refreshToken) {
return fetch(
this.baseUrl +
"api/oauth/token?grant_type=refresh_token&refresh_token=" +
refreshToken +
"&client_id=" +
this.clientId +
"&client_secret=" +
this.clientSecret,
{
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" }
}
)
.then(tokenResponse => {
if (!tokenResponse.ok) {
throw new Error(tokenResponse);
}
return tokenResponse.json();
})
.then(json => {
return {
token: json.access_token,
lifetimeInSeconds: json.expires_in
};
});
}
}
module.exports = ProcountorAuthentication;