-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (65 loc) · 1.87 KB
/
index.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
const axios = require("axios");
const uuidv4 = require("uuid/v4");
class Keycloack {
constructor(options = {}) {
const { url, realm, clientId } = options;
this.url = url;
this.realm = realm;
this.clientId = clientId;
this.token = null;
}
_get_current_uri() {
return window.location.href.split("#")[0];
}
login(options = {}) {
const redirectUri = options.redirectUri || this._get_current_uri();
const params = {
client_id: this.clientId,
redirect_uri: redirectUri,
state: uuidv4(),
response_mode: "fragment",
response_type: "code",
scope: "openid",
nonce: uuidv4()
};
const encodedParams = Object.keys(params).reduce((acc, key) => {
acc.push(`${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`);
return acc;
}, []);
window.location.replace(
`${this.url}/auth/realms/${
this.realm
}/protocol/openid-connect/auth?${encodedParams.join("&")}`
);
}
init() {
const urlParams = new URLSearchParams(window.location.hash);
const code = urlParams.get("code");
if (code) {
const data = new URLSearchParams();
data.append("code", code);
data.append("grant_type", "authorization_code");
data.append("client_id", this.clientId);
data.append("redirect_uri", this._get_current_uri());
return axios
.post(
`${this.url}/auth/realms/${this.realm}/protocol/openid-connect/token`,
data,
{
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}
)
.then(response => {
this.token = response.data.access_token;
return Promise.resolve(true);
})
.catch(error => {
return Promise.resolve(false);
});
}
return Promise.resolve(false);
}
}
module.exports = Keycloack;