forked from ifrn-oficial/cliente_suap_javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
153 lines (126 loc) · 4.48 KB
/
client.js
File metadata and controls
153 lines (126 loc) · 4.48 KB
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
"use strict";
class ClientParams{
constructor(authHost, clientID, redirectURI, scope){
this.resourceURL = authHost + '/api/eu/';
this.authorizationURL = authHost + '/o/authorize/';
this.accessTokenURL = authHost + '/o/token/';
this.logoutURL = authHost + '/o/revoke_token/';
this.responseType = 'code';
this.grantType = 'authorization_code';
this.clientID = clientID;
this.scope = scope;
this.redirectURI = redirectURI;
}
}
class Token{
constructor(clientParams){
this.clientParams = clientParams;
this.value = sessionStorage.getItem("oauthToken");
this.expirationTime = sessionStorage.getItem("oauthTokenExpirationTime");
this.scope = sessionStorage.getItem("oauthScope");
}
revokeSession(){
sessionStorage.removeItem("oauthToken")
sessionStorage.removeItem("oauthTokenExpirationTime")
sessionStorage.removeItem("oauthScope")
}
}
class OAuthClient {
constructor(clientParams){
this.clientParams = clientParams;
this.urlParams = new URLSearchParams(window.location.search);
this.token = new Token(this.clientParams);
}
async init(){
if (!this.isAuthenticated()){
if (this.urlParams.get("code")) {
if (this.urlParams.get("state") === sessionStorage.getItem("oauthState")) {
await this.authorize(this.urlParams.get("code"));
window.location = this.clientParams.redirectURI;
}
} else {
this.generateCode();
}
}
}
generateCode(){
sessionStorage.setItem("oauthState",random_string(16));
sessionStorage.setItem("oauthCodeVerifier",random_string(48));
}
getLoginURL(){
let loginUrl = this.clientParams.authorizationURL +
"?response_type=" + this.clientParams.responseType +
"&client_id=" + this.clientParams.clientID +
"&scope=" + this.clientParams.scope +
"&state=" + sessionStorage.getItem("oauthState") +
"&code_challenge=" + base64_urlencode(sha256bin(sessionStorage.getItem("oauthCodeVerifier"))) +
"&code_challenge_method=S256" +
"&redirect_uri=" + this.clientParams.redirectURI;
return loginUrl;
}
isAuthenticated(){
if (sessionStorage.getItem("oauthToken")){
return true;
} else {
return false;
}
}
async authorize(code){
if (sessionStorage.getItem("oauthToken")){
self.revokeSession();
}
let data = {'grant_type': this.clientParams.grantType,
'client_id': this.clientParams.clientID,
"code_verifier": sessionStorage.getItem("oauthCodeVerifier"),
"code": code,
"redirect_uri": this.clientParams.redirectURI};
try {
const response = await fetch(this.clientParams.accessTokenURL, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: Object.keys(data).map(key => key + '=' + data[key]).join('&')
});
let responseData = await response.json();
let startTime = new Date().getTime(); // O valor em milissegundos.
let finishTime = new Date(startTime + responseData.expires_in * 1000); // O objeto Date.
sessionStorage.setItem("oauthToken", responseData.access_token);
sessionStorage.setItem("oauthTokenExpirationTime", finishTime);
sessionStorage.setItem("oauthScope", responseData.scope);
} catch (e){
alert('Falha na comunicação com o Servidor');
console.log(e);
}
}
async logout(){
let data = {"token": this.token.value,
"client_id": this.clientParams.clientID,
"redirect_uri": this.clientParams.redirectURI};
try{
const response = await fetch(this.clientParams.logoutURL, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: Object.keys(data).map(key => key + '=' + data[key]).join('&')
});
this.token.revokeSession();
window.location = this.clientParams.redirectURI;
} catch (e){
alert('Falha na comunicação com o Servidor');
console.log(e);
}
}
async getResource(endpoint, callback){
let data = {"token": this.token.value,
"client_id": this.clientParams.clientID,
"redirect_uri": this.clientParams.redirectURI};
const response = await fetch(endpoint, {
method: 'GET',
headers: {"Authorization": "Bearer " + this.token.value,
"Accept": "application/json"}
});
callback(await response.json());
}
}