-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeycloak.js
36 lines (33 loc) · 1.12 KB
/
keycloak.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
import axios from "axios";
// TODO: cache tokens and add a refresh token method
export class KeycloakConnector {
constructor(username, password, token_endpoint) {
this.token_endpoint = token_endpoint;
const scope = "openid";
var details = {
grant_type: "password",
scope: scope,
client_id: "frontend",
username: username,
password: password,
};
var formBody = [];
for (var property in details) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
this.formBody = formBody.join("&");
}
async getToken() {
const response = await axios.post(this.token_endpoint, this.formBody, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
});
return response.data.access_token;
}
}
// new KeycloakConnector("", "", "").getToken().then(token => {
// console.log("token: " + token);
// });