-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
162 lines (151 loc) · 4.22 KB
/
Copy pathindex.ts
File metadata and controls
162 lines (151 loc) · 4.22 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
154
155
156
157
158
159
160
161
162
import config from '../config';
import { buildRequestHeaders, Get, parseHttpResponse, Post } from './utils';
import {
fronteggTenantsV3Url,
fronteggUsersUrl,
ILoginResponse,
GetCurrentUserTenantsResponse,
IPublicSettingsResponse,
IGetUserAuthorizationResponse,
fronteggEntitlementsV2Url,
} from '@frontegg/rest-api';
import { UserEntitlementsResponseV2 } from '@frontegg/types';
import { CommonUrls } from '../utils/common/urls';
/**
* Send HTTP GET to frontegg domain public route to download the JWT public key
*/
const loadPublicKey = async () => {
const response = await fetch(`${config.baseUrl}${CommonUrls.WellKnown.jwks}`, {
cache: 'force-cache',
});
const data = await response.json();
return data.keys[0];
};
/**
* Send HTTP post-request for Frontegg services to refresh token
* by providing client's fe_ cookies
*/
const refreshTokenEmbedded = async (headers: Record<string, string>) => {
return Post({
url: `${config.baseUrl}${CommonUrls.refreshToken.embedded}`,
body: '{}',
credentials: 'include',
headers: buildRequestHeaders(headers),
});
};
/**
* Send HTTP post request for Frontegg services to refresh `hosted login` token
* by providing client's fe_ as body with grant_type.
*/
const refreshTokenHostedLogin = async (
headers: Record<string, string>,
refreshToken: string,
clientId?: string,
clientSecret?: string
) => {
return Post({
url: `${config.baseUrl}${CommonUrls.refreshToken.hosted}`,
body: JSON.stringify({
grant_type: 'refresh_token',
refresh_token: refreshToken,
client_id: clientId,
client_secret: clientSecret,
}),
headers: buildRequestHeaders(headers),
});
};
/**
* Send HTTP post request for Frontegg services to exchange `hosted login` callback code
*/
export const exchangeHostedLoginToken = async (
headers: Record<string, string>,
code: string,
cliendId: string,
clientSecret: string
) => {
return Post({
url: `${config.baseUrl}${CommonUrls.refreshToken.hosted}`,
body: JSON.stringify({
redirect_uri: `${config.appUrl}/oauth/callback`,
grant_type: 'authorization_code',
code,
client_id: cliendId,
client_secret: clientSecret,
// code_verifier
}),
headers: buildRequestHeaders(headers),
});
};
/**
*
* @param headers
*/
export const getMe = async (headers: Record<string, string>): Promise<ILoginResponse | undefined> => {
const headersToSend = buildRequestHeaders(headers);
const res = await Get({
url: `${config.baseUrl}/frontegg${fronteggUsersUrl}`,
headers: headersToSend,
});
return parseHttpResponse(res);
};
/**
*
* @param headers
*/
export const getEntitlements = async (
headers: Record<string, string>
): Promise<UserEntitlementsResponseV2 | undefined> => {
const headersToSend = buildRequestHeaders(headers);
const res = await Get({
url: `${config.baseUrl}${fronteggEntitlementsV2Url}`,
headers: headersToSend,
});
return parseHttpResponse(res);
};
/**
*
* @param headers
*/
export const getTenants = async (
headers: Record<string, string>
): Promise<GetCurrentUserTenantsResponse | undefined> => {
const res = await Get({
url: `${config.baseUrl}/frontegg${fronteggTenantsV3Url}`,
headers: buildRequestHeaders(headers),
});
return parseHttpResponse(res);
};
/**
*
* @param headers
*/
export const getMeAuthorization = async (
headers: Record<string, string>
): Promise<IGetUserAuthorizationResponse | undefined> => {
const res = await Get({
// TODO: replace this with rest/api route
url: `${config.baseUrl}/frontegg/identity/resources/users/v1/me/authorization`,
headers: buildRequestHeaders(headers),
});
return parseHttpResponse(res);
};
export const getPublicSettings = async (
headers: Record<string, string>
): Promise<IPublicSettingsResponse | undefined> => {
const res = await Get({
// TODO: export the route url from rest-api and import from there
url: `${config.baseUrl}/frontegg/tenants/resources/account-settings/v1/public`,
headers: buildRequestHeaders(headers),
});
return parseHttpResponse(res);
};
export default {
loadPublicKey,
refreshTokenEmbedded,
refreshTokenHostedLogin,
getMe,
getTenants,
getPublicSettings,
getEntitlements,
exchangeHostedLoginToken,
};