-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.js
92 lines (80 loc) · 3 KB
/
auth.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
import axios from "axios";
import { Buffer } from 'buffer/';
/*This config object is just for this demo project.
In a real-world scenario, you should store client id and client secret securely as per React best practices.
Client secret must never be stored on the frontend.
The base url and redirect uri should be loaded from .env file.*/
const config = {
clientId: "_your_client_id_",
clientSecret: "_your_client_secret_",
vedaAuthBaseUrl: "http://localhost:8081/api/v1",
redirectUri: "http://localhost:5173/callback/",
}
const axiosInstance = axios.create({
baseURL: config.vedaAuthBaseUrl,
withCredentials: false,
headers: {
'Accept': '*/*',
'Content-Type': 'application/json'
}
});
const getClientSecret = async (clientId) => {
const {data: {custos_client_secret}} = await axiosInstance.get(
`/identity-management/credentials`,
{
headers: {
'Authorization': `Bearer ${sessionStorage.getItem('access_token')}`
},
params: {
'client_id': clientId
}
}
);
return custos_client_secret;
}
const getClientAuthBase64 = async (clientId = null, clientSec = null) => {
if (clientId === null && clientSec === null) {
clientId = config.clientId;
clientSec = config.clientSecret;
} else if (clientId !== null && clientSec === null) {
clientSec = await getClientSecret(clientId);
}
let clientAuthBase64 = `${clientId}:${clientSec}`;
clientAuthBase64 = Buffer.from(clientAuthBase64).toString('base64');
clientAuthBase64 = `Bearer ${clientAuthBase64}`
return clientAuthBase64;
}
const fetchAuthorizationEndpoint = async () => {
// const openIdConfigEndpoint = "/identity-management/.well-known/openid-configuration";
const redirectUri = config.redirectUri;
const authorizeEndpoint = `/identity-management/authorize?response_type=code&client_id=${config.clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=user:email&kc_idp_hint=oidc&state=LDh1iNYlJcSujbgfFakT2iwhB6PIhgBidrFBmYNTBMw`;
const { data: { loginURI } } = await axiosInstance.get(authorizeEndpoint,{
});
window.location.href = loginURI;
}
const fetchToken = async ({code}) => {
const clientAuthBase64 = await getClientAuthBase64();
const {data} = await axiosInstance.post("/identity-management/token", {
code: code,
redirect_uri: config.redirectUri,
grant_type: 'authorization_code'
}, {
headers: {
'Authorization': clientAuthBase64
}
});
return data;
}
const fetchUserInfo = async () => {
const clientAuthBase64 = await getClientAuthBase64();
const {data} = await axiosInstance.get("/user-management/userinfo", {
params: {
'access_token': sessionStorage.getItem('access_token')
},
headers: {
'Authorization': clientAuthBase64
}
});
return data;
}
export {fetchAuthorizationEndpoint, fetchToken, fetchUserInfo}