@frontegg/rest-api 2.10.46 → 2.10.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/auth/index.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import {getTenants} from "../tenants";
2
+
1
3
  export * from './secutiry-poilicy';
2
4
  export * from './enums';
3
5
 
@@ -21,7 +23,8 @@ import {
21
23
  IGetUserPasswordConfig,
22
24
  ILogin,
23
25
  ILoginResponse,
24
- ILoginViaSocialLogin, ILoginViaSocialLoginResponse,
26
+ ILoginViaSocialLogin,
27
+ ILoginViaSocialLoginResponse,
25
28
  ILoginWithMfa,
26
29
  IOidcPostLogin,
27
30
  IOidcConfiguration,
@@ -46,7 +49,20 @@ import {
46
49
  IUserIdResponse,
47
50
  IVendorConfig,
48
51
  IVerifyMfa,
49
- IVerifyMfaResponse, TestConfig, ISSOPublicConfiguration, IPreLoginWithIdpTypeResponse, IPasswordlessPreLogin, IPasswordlessPostLogin, ICreateSSODomain, IVerifyInviteToken, ISSODomain, ISSOConfigurationDefaultRoles, ISSOConfiguration, IUpdateSSOConfiguration, IOidcPostLoginV2
52
+ IVerifyMfaResponse,
53
+ TestConfig,
54
+ ISSOPublicConfiguration,
55
+ IPreLoginWithIdpTypeResponse,
56
+ IPasswordlessPreLogin,
57
+ IPasswordlessPostLogin,
58
+ ICreateSSODomain,
59
+ IVerifyInviteToken,
60
+ ISSODomain,
61
+ ISSOConfigurationDefaultRoles,
62
+ ISSOConfiguration,
63
+ IUpdateSSOConfiguration,
64
+ IOidcPostLoginV2,
65
+ IExchangeOAuthTokens, IOAuthTokenResponse, ISocialLoginProviderConfigurationV2, ILoginResponseV2,
50
66
  } from './interfaces';
51
67
 
52
68
  /*****************************************
@@ -69,6 +85,37 @@ export async function generateLoginResponse(loginResponse: ILoginResponse): Prom
69
85
  return user;
70
86
  }
71
87
 
88
+ export async function generateLoginResponseV2(loginResponse: ILoginResponse): Promise<ILoginResponseV2> {
89
+ if (!loginResponse.accessToken) {
90
+ return {user: loginResponse};
91
+ }
92
+ ContextHolder.setAccessToken(loginResponse.accessToken);
93
+ const [me, tenants] = await Promise.all([Get(`${urls.identity.users.v2}/me`), getTenants()]);
94
+ const decodedContent: any = loginResponse.accessToken ? jwtDecode(loginResponse.accessToken) : {};
95
+ const user = {
96
+ ...loginResponse,
97
+ ...decodedContent,
98
+ ...me,
99
+ };
100
+ ContextHolder.setUser(user);
101
+ return {user, tenants};
102
+ }
103
+
104
+ export async function generateLoginResponseFromOAuthResponse(oauthResponse: IOAuthTokenResponse): Promise<ILoginResponse> {
105
+ ContextHolder.setAccessToken(oauthResponse.id_token);
106
+ const me = await Get(`${urls.identity.users.v2}/me`);
107
+ const decodedContent: any = oauthResponse.id_token ? jwtDecode(oauthResponse.id_token) : {};
108
+ const user = {
109
+ mfaRequired: false,
110
+ accessToken: oauthResponse.id_token,
111
+ refreshToken: oauthResponse.refresh_token,
112
+ ...decodedContent,
113
+ ...me,
114
+ };
115
+ ContextHolder.setUser(user);
116
+ return user;
117
+ }
118
+
72
119
  /**
73
120
  * Check if requested email address has sso configuration
74
121
  * If true, this function will return the sso address to navigate to
@@ -196,6 +243,16 @@ export async function refreshToken(): Promise<ILoginResponse> {
196
243
  return generateLoginResponse(data);
197
244
  }
198
245
 
246
+ /**
247
+ * refresh token called as authenticated use, access and refresh tokens resolved by the cookies.
248
+ * the server will return ILoginResponseV2 with new access Token and refresh token and store it in the browser cookies, as well as the tenants.
249
+ */
250
+ export async function refreshTokenV2(): Promise<ILoginResponseV2> {
251
+ console.debug('refreshTokenV2()');
252
+ const data = await Post(`${urls.identity.auth.v1}/user/token/refresh`);
253
+ return generateLoginResponseV2(data);
254
+ }
255
+
199
256
  /**
200
257
  * logout from server, invalidate access and refresh token, remove it from cookies.
201
258
  */
@@ -422,6 +479,15 @@ export async function getSocialLoginProviders(): Promise<ISocialLoginProviderCon
422
479
  return Get(urls.identity.sso.v1);
423
480
  }
424
481
 
482
+ /**
483
+ * Get social logins providers configurations V2 supports dev credentials as well
484
+ * @return array of providers configurations
485
+ */
486
+ export async function getSocialLoginProvidersV2(): Promise<ISocialLoginProviderConfigurationV2[]> {
487
+ console.debug('getSocialLoginProvidersV2()');
488
+ return Get(urls.identity.sso.v2);
489
+ }
490
+
425
491
  /**
426
492
  * Login using social login
427
493
  * @return cookie with refresh token
@@ -433,12 +499,14 @@ export async function loginViaSocialLogin({
433
499
  codeVerifier,
434
500
  metadata,
435
501
  invitationToken,
502
+ state,
436
503
  }: ILoginViaSocialLogin): Promise<ILoginViaSocialLoginResponse> {
437
504
  console.debug('loginViaSocialLogin()');
438
505
  const params: {
439
506
  code: string;
440
507
  redirectUri?: string;
441
508
  code_verifier?: string;
509
+ state?: string;
442
510
  } = {code};
443
511
  if (redirectUri) {
444
512
  params.redirectUri = redirectUri;
@@ -446,6 +514,9 @@ export async function loginViaSocialLogin({
446
514
  if (codeVerifier) {
447
515
  params.code_verifier = codeVerifier;
448
516
  }
517
+ if(state) {
518
+ params.state = state
519
+ }
449
520
 
450
521
  return Post(`${urls.identity.auth.v1}/user/sso/${provider}/postlogin`, {metadata, invitationToken}, {params});
451
522
  }
@@ -728,3 +799,9 @@ export async function oidcPostLoginV2(body: IOidcPostLoginV2): Promise<ILoginRes
728
799
  const data = await Post(`${urls.identity.auth.v2}/user/oidc/postlogin`, body);
729
800
  return generateLoginResponse(data);
730
801
  }
802
+
803
+ export async function exchangeOAuthTokens(body: IExchangeOAuthTokens): Promise<ILoginResponse> {
804
+ console.debug('exchangeOauthTokens()');
805
+ const data = await Post(`${urls.oauth.v1}/token`, body);
806
+ return generateLoginResponseFromOAuthResponse(data);
807
+ }
@@ -1,4 +1,4 @@
1
- import {IUserProfile} from '..';
1
+ import {ITenantsResponse, IUserProfile} from '..';
2
2
  import {AuthStrategyEnum, SocialLoginProviders} from "./enums";
3
3
  import { ISamlRolesGroup } from '../teams/interfaces';
4
4
 
@@ -42,6 +42,11 @@ export type ILoginResponse = IUserProfile & {
42
42
  redirectLocation?: string;
43
43
  };
44
44
 
45
+ export type ILoginResponseV2 = {
46
+ user: ILoginResponse,
47
+ tenants?: ITenantsResponse[]
48
+ }
49
+
45
50
  export type ILoginWithMfa = {
46
51
  mfaToken: string;
47
52
  value: string;
@@ -143,6 +148,15 @@ export interface ISocialLoginProviderConfiguration {
143
148
  active: boolean;
144
149
  }
145
150
 
151
+ export interface ISocialLoginProviderConfigurationV2 {
152
+ type: SocialLoginProviders;
153
+ clientId?: string | null;
154
+ redirectUrl: string;
155
+ active: boolean;
156
+ authorizationUrl?: string | null;
157
+ customised: boolean;
158
+ }
159
+
146
160
  export interface ILoginViaSocialLogin {
147
161
  code: string;
148
162
  redirectUri?: string;
@@ -151,6 +165,7 @@ export interface ILoginViaSocialLogin {
151
165
  codeVerifier?: string;
152
166
  metadata?: string;
153
167
  invitationToken?: string;
168
+ state?: string
154
169
  }
155
170
 
156
171
  export interface ILoginViaSocialLoginResponse {
@@ -315,4 +330,17 @@ export interface ISSOConfiguration {
315
330
  groups: ISamlRolesGroup[]
316
331
  }
317
332
 
333
+ export interface IExchangeOAuthTokens {
334
+ code: string;
335
+ redirect_uri: string;
336
+ code_verifier: string;
337
+ }
338
+
339
+ export interface IOAuthTokenResponse {
340
+ access_token: string;
341
+ expires_in?: number;
342
+ id_token: string;
343
+ refresh_token: string;
344
+ }
345
+
318
346
  export type IUpdateSSOConfiguration = Partial<Omit<ISSOConfiguration, 'id' | 'createdAt' | 'updatedAt' | 'domains'>>
package/src/constants.ts CHANGED
@@ -16,7 +16,8 @@ export const urls = {
16
16
  v1: '/identity/resources/configurations/v1'
17
17
  },
18
18
  sso: {
19
- v1: '/identity/resources/sso/v1'
19
+ v1: '/identity/resources/sso/v1',
20
+ v2: '/identity/resources/sso/v2'
20
21
  },
21
22
  permissions: {
22
23
  v1: '/identity/resources/permissions/v1'
@@ -137,5 +138,8 @@ export const urls = {
137
138
  },
138
139
  webhooks: {
139
140
  v1: '/webhook'
140
- }
141
+ },
142
+ oauth: {
143
+ v1: '/oauth'
144
+ },
141
145
  }
package/src/fetch.ts CHANGED
@@ -13,7 +13,7 @@ interface RequestOptions {
13
13
  credentials?: RequestCredentials;
14
14
  }
15
15
 
16
- async function getBaseUrl(context: ContextOptions): Promise<string> {
16
+ export function getBaseUrl(context: ContextOptions): string {
17
17
  let baseUrl = context.baseUrl;
18
18
  const prefix = context.urlPrefix || 'frontegg';
19
19
  if (!baseUrl.endsWith('/')) {
@@ -26,7 +26,7 @@ async function getBaseUrl(context: ContextOptions): Promise<string> {
26
26
  }
27
27
 
28
28
  async function prepareUrl(context: ContextOptions, url: string, params?: any): Promise<string> {
29
- const baseUrl = await getBaseUrl(context);
29
+ const baseUrl = getBaseUrl(context);
30
30
  const paramsToSend = await buildQueryParams(context, params);
31
31
 
32
32
  let finalUrl = url.startsWith('http') ? url : `${baseUrl}${url}`;
package/src/interfaces.ts CHANGED
@@ -17,6 +17,7 @@ export type LogLevel = 'warn' | 'error';
17
17
 
18
18
  export interface ContextOptions {
19
19
  baseUrl: string;
20
+ clientId?: string;
20
21
  tokenResolver?: () => Promise<string> | string; // custom resolve Authorization Header value
21
22
  additionalQueryParamsResolver?: () => Promise<KeyValuePair[]> | KeyValuePair[];
22
23
  additionalHeadersResolver?: () => Promise<KeyValuePair[]> | KeyValuePair[];
package/src/jwt.ts CHANGED
@@ -87,6 +87,6 @@ export const jwtDecode = (token: string, options: { header?: boolean } = {}) =>
87
87
  try {
88
88
  return JSON.parse(base64UrlDecode(token.split('.')[pos]));
89
89
  } catch (e) {
90
- throw new InvalidTokenError('Invalid token specified: ' + e.message);
90
+ throw new InvalidTokenError('Invalid token specified: ' + (e as Error).message);
91
91
  }
92
92
  };