@asgardeo/auth-react 5.1.1 → 5.2.0

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.
Files changed (45) hide show
  1. package/LICENSE +201 -0
  2. package/dist/main.js +1 -1
  3. package/dist/types/src/actions.d.ts.map +1 -0
  4. package/dist/types/src/api.d.ts.map +1 -0
  5. package/dist/types/src/authenticate.d.ts.map +1 -0
  6. package/dist/types/src/authenticated-component.d.ts.map +1 -0
  7. package/dist/types/src/index.d.ts.map +1 -0
  8. package/dist/types/src/models.d.ts.map +1 -0
  9. package/dist/types/src/public-api.d.ts.map +1 -0
  10. package/dist/types/src/reducer.d.ts.map +1 -0
  11. package/dist/types/src/secure-app.d.ts.map +1 -0
  12. package/package.json +9 -2
  13. package/.eslintignore +0 -2
  14. package/.eslintrc.js +0 -166
  15. package/babel.config.js +0 -43
  16. package/dist/src/actions.d.ts.map +0 -1
  17. package/dist/src/api.d.ts.map +0 -1
  18. package/dist/src/authenticate.d.ts.map +0 -1
  19. package/dist/src/authenticated-component.d.ts.map +0 -1
  20. package/dist/src/index.d.ts.map +0 -1
  21. package/dist/src/models.d.ts.map +0 -1
  22. package/dist/src/public-api.d.ts.map +0 -1
  23. package/dist/src/reducer.d.ts.map +0 -1
  24. package/dist/src/secure-app.d.ts.map +0 -1
  25. package/dist/tsconfig.tsbuildinfo +0 -1
  26. package/src/actions.ts +0 -51
  27. package/src/api.ts +0 -466
  28. package/src/authenticate.tsx +0 -325
  29. package/src/authenticated-component.tsx +0 -43
  30. package/src/index.ts +0 -23
  31. package/src/models.ts +0 -138
  32. package/src/public-api.ts +0 -22
  33. package/src/reducer.ts +0 -68
  34. package/src/secure-app.tsx +0 -83
  35. package/tsconfig.json +0 -26
  36. package/webpack.config.js +0 -94
  37. /package/dist/{src → types/src}/actions.d.ts +0 -0
  38. /package/dist/{src → types/src}/api.d.ts +0 -0
  39. /package/dist/{src → types/src}/authenticate.d.ts +0 -0
  40. /package/dist/{src → types/src}/authenticated-component.d.ts +0 -0
  41. /package/dist/{src → types/src}/index.d.ts +0 -0
  42. /package/dist/{src → types/src}/models.d.ts +0 -0
  43. /package/dist/{src → types/src}/public-api.d.ts +0 -0
  44. /package/dist/{src → types/src}/reducer.d.ts +0 -0
  45. /package/dist/{src → types/src}/secure-app.d.ts +0 -0
package/src/api.ts DELETED
@@ -1,466 +0,0 @@
1
- /**
2
- * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
3
- *
4
- * WSO2 Inc. licenses this file to you under the Apache License,
5
- * Version 2.0 (the "License"); you may not use this file except
6
- * in compliance with the License.
7
- * You may obtain a copy of the License at
8
- *
9
- * http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing,
12
- * software distributed under the License is distributed on an
13
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- * KIND, either express or implied. See the License for the
15
- * specific language governing permissions and limitations
16
- * under the License.
17
- */
18
-
19
- import {
20
- AsgardeoSPAClient,
21
- AuthClientConfig,
22
- BasicUserInfo,
23
- Config,
24
- DecodedIDTokenPayload,
25
- FetchResponse,
26
- Hooks,
27
- HttpClientInstance,
28
- HttpRequestConfig,
29
- HttpResponse,
30
- OIDCEndpoints,
31
- SignInConfig
32
- } from "@asgardeo/auth-spa";
33
- import { SPACustomGrantConfig } from "@asgardeo/auth-spa/src/models/request-custom-grant";
34
- import { AuthStateInterface } from "./models";
35
-
36
- class AuthAPI {
37
- static DEFAULT_STATE: AuthStateInterface;
38
-
39
- private _authState = AuthAPI.DEFAULT_STATE;
40
- private _client: AsgardeoSPAClient;
41
-
42
- constructor(spaClient?: AsgardeoSPAClient) {
43
- this._client = spaClient ?? AsgardeoSPAClient.getInstance();
44
-
45
- this.getState = this.getState.bind(this);
46
- this.init = this.init.bind(this);
47
- this.signIn = this.signIn.bind(this);
48
- this.signOut = this.signOut.bind(this);
49
- this.updateState = this.updateState.bind(this);
50
- }
51
-
52
- /**
53
- * Method to return Auth Client instance authentication state.
54
- *
55
- * @return {AuthStateInterface} Authentication State.
56
- */
57
- public getState(): AuthStateInterface {
58
- return this._authState;
59
- }
60
-
61
- /**
62
- * Method to initialize the AuthClient instance.
63
- *
64
- * @param {Config} config - `dispatch` function from React Auth Context.
65
- */
66
- public init(config: AuthClientConfig<Config>): Promise<boolean> {
67
- return this._client.initialize(config);
68
- }
69
-
70
- /**
71
- * Method to handle user Sign In requests.
72
- *
73
- * @param {any} dispatch - `dispatch` function from React Auth Context.
74
- * @param {AuthStateInterface} state - Current authentication state in React Auth Context.
75
- * @param {any} callback - Action to trigger on successful sign in.
76
- */
77
- public async signIn(
78
- dispatch: (state: AuthStateInterface) => void,
79
- state: AuthStateInterface,
80
- config: SignInConfig,
81
- authorizationCode: string,
82
- sessionState: string,
83
- authState?: string,
84
- callback?: (response: BasicUserInfo) => void,
85
- tokenRequestConfig?: {
86
- params: Record<string, unknown>
87
- }
88
- ): Promise<BasicUserInfo> {
89
- return this._client
90
- .signIn(config, authorizationCode, sessionState, authState, tokenRequestConfig)
91
- .then(async (response: BasicUserInfo) => {
92
- if (!response) {
93
- return;
94
- }
95
-
96
- if (await this._client.isAuthenticated()) {
97
- const stateToUpdate ={
98
- allowedScopes: response.allowedScopes,
99
- displayName: response.displayName,
100
- email: response.email,
101
- isAuthenticated: true,
102
- isLoading: false,
103
- isSigningOut: false,
104
- sub: response.sub,
105
- username: response.username
106
- };
107
-
108
- this.updateState(stateToUpdate);
109
-
110
- dispatch({ ...state, ...stateToUpdate });
111
-
112
- if (callback) {
113
- callback(response);
114
- }
115
- }
116
-
117
- return response;
118
- })
119
- .catch((error) => {
120
- return Promise.reject(error);
121
- });
122
- }
123
-
124
- /**
125
- * Method to handle user Sign Out requests.
126
- *
127
- * @param {any} dispatch - `dispatch` function from React Auth Context.
128
- * @param {AuthStateInterface} state - Current authentication state in React Auth Context.
129
- * @param {any} callback - Action to trigger on successful sign out.
130
- */
131
- public signOut(
132
- dispatch: (state: AuthStateInterface) => void,
133
- state: AuthStateInterface,
134
- callback?: (response?: boolean) => void
135
- ): Promise<boolean> {
136
- return this._client
137
- .signOut()
138
- .then((response) => {
139
- if (callback) {
140
- callback(response);
141
- }
142
-
143
- return response;
144
- })
145
- .catch((error) => {
146
- return Promise.reject(error);
147
- });
148
- }
149
-
150
- /**
151
- * Method to update Auth Client instance authentication state.
152
- *
153
- * @param {AuthStateInterface} state - State values to update in authentication state.
154
- */
155
- public updateState(state: AuthStateInterface): void {
156
- this._authState = { ...this._authState, ...state };
157
- }
158
-
159
- /**
160
- * This method returns a Promise that resolves with the basic user information obtained from the ID token.
161
- *
162
- * @return {Promise<BasicUserInfo>} - A promise that resolves with the user information.
163
- */
164
- public async getBasicUserInfo(): Promise<BasicUserInfo> {
165
- return this._client.getBasicUserInfo();
166
- }
167
-
168
- /**
169
- * This method sends an API request to a protected endpoint.
170
- * The access token is automatically attached to the header of the request.
171
- * This is the only way by which protected endpoints can be accessed
172
- * when the web worker is used to store session information.
173
- *
174
- * @param {HttpRequestConfig} config - The config object containing attributes necessary to send a request.
175
- *
176
- * @return {Promise<FetchResponse>} - Returns a Promise that resolves with the response to the request.
177
- */
178
- public async httpRequest(config: HttpRequestConfig): Promise<HttpResponse<any>> {
179
- return this._client.httpRequest(config);
180
- }
181
-
182
- /**
183
- * This method sends multiple API requests to a protected endpoint.
184
- * The access token is automatically attached to the header of the request.
185
- * This is the only way by which multiple requests can be sent to protected endpoints
186
- * when the web worker is used to store session information.
187
- *
188
- * @param {HttpRequestConfig[]} config - The config object containing attributes necessary to send a request.
189
- *
190
- * @return {Promise<FetchResponse[]>} - Returns a Promise that resolves with the responses to the requests.
191
- */
192
- public async httpRequestAll(configs: HttpRequestConfig[]): Promise<HttpResponse<any>[]> {
193
- return this._client.httpRequestAll(configs);
194
- }
195
-
196
- /**
197
- * This method allows you to send a request with a custom grant.
198
- *
199
- * @param {CustomGrantRequestParams} config - The request parameters.
200
- *
201
- * @return {Promise<FetchResponse<any> | SignInResponse>} - A Promise that resolves with
202
- * the value returned by the custom grant request.
203
- */
204
- public requestCustomGrant(
205
- config: SPACustomGrantConfig,
206
- callback: (response: BasicUserInfo | FetchResponse<any>) => void,
207
- dispatch: (state: AuthStateInterface) => void
208
- ): Promise<BasicUserInfo | FetchResponse<any>> {
209
- return this._client
210
- .requestCustomGrant(config)
211
- .then((response: BasicUserInfo | FetchResponse<any>) => {
212
- if (!response) {
213
- return;
214
- }
215
-
216
- if (config.returnsSession) {
217
- this.updateState(
218
- {
219
- ...this.getState(),
220
- ...(response as BasicUserInfo), isAuthenticated: true, isLoading: false
221
- }
222
- );
223
-
224
- dispatch({ ...(response as BasicUserInfo), isAuthenticated: true, isLoading: false });
225
- }
226
-
227
- callback && callback(response);
228
-
229
- return response;
230
- })
231
- .catch((error) => {
232
- return Promise.reject(error);
233
- });
234
- }
235
-
236
- /**
237
- * This method ends a user session. The access token is revoked and the session information is destroyed.
238
- *
239
- * @return {Promise<boolean>} - A promise that resolves with `true` if the process is successful.
240
- */
241
- public async revokeAccessToken(dispatch: (state: AuthStateInterface) => void): Promise<boolean> {
242
- return this._client
243
- .revokeAccessToken()
244
- .then(() => {
245
- this.updateState({ ...AuthAPI.DEFAULT_STATE, isLoading: false });
246
- dispatch(AuthAPI.DEFAULT_STATE);
247
- return true;
248
- })
249
- .catch((error) => {
250
- return Promise.reject(error);
251
- });
252
- }
253
-
254
- /**
255
- * This method returns a Promise that resolves with an object containing the service endpoints.
256
- *
257
- * @return {Promise<ServiceResourcesType} - A Promise that resolves with an object containing the service endpoints.
258
- */
259
- public async getOIDCServiceEndpoints(): Promise<OIDCEndpoints> {
260
- return this._client.getOIDCServiceEndpoints();
261
- }
262
-
263
- /**
264
- * This methods returns the Axios http client.
265
- *
266
- * @return {HttpClientInstance} - The Axios HTTP client.
267
- */
268
- public async getHttpClient(): Promise<HttpClientInstance> {
269
- return this._client.getHttpClient();
270
- }
271
-
272
- /**
273
- * This method decodes the payload of the id token and returns it.
274
- *
275
- * @return {Promise<DecodedIDTokenPayloadInterface>} - A Promise that resolves with
276
- * the decoded payload of the id token.
277
- */
278
- public async getDecodedIDToken(): Promise<DecodedIDTokenPayload> {
279
- return this._client.getDecodedIDToken();
280
- }
281
-
282
- /**
283
- * This method decodes the payload of the idp id token and returns it.
284
- *
285
- * @return {Promise<DecodedIDTokenPayloadInterface>} - A Promise that resolves with
286
- * the decoded payload of the idp id token.
287
- */
288
- public async getDecodedIDPIDToken(): Promise<DecodedIDTokenPayload> {
289
- return this._client.getDecodedIDToken();
290
- }
291
-
292
- /**
293
- * This method returns the ID token.
294
- *
295
- * @return {Promise<string>} - A Promise that resolves with the id token.
296
- */
297
- public async getIDToken(): Promise<string> {
298
- return this._client.getIDToken();
299
- }
300
-
301
- /**
302
- * This method return a Promise that resolves with the access token.
303
- *
304
- * **This method will not return the access token if the storage type is set to `webWorker`.**
305
- *
306
- * @return {Promise<string>} - A Promise that resolves with the access token.
307
- */
308
- public async getAccessToken(): Promise<string> {
309
- return this._client.getAccessToken();
310
- }
311
-
312
- /**
313
- * This method return a Promise that resolves with the idp access token.
314
- *
315
- * **This method will not return the idp access token if the storage type is set to `webWorker`.**
316
- * **This can be used to access the IDP access token when custom auth grant functionalities are used**
317
- *
318
- * @return {Promise<string>} - A Promise that resolves with the idp access token.
319
- */
320
- public async getIDPAccessToken(): Promise<string> {
321
- return this._client.getIDPAccessToken();
322
- }
323
-
324
- /**
325
- * This method refreshes the access token.
326
- *
327
- * @return {TokenResponseInterface} - A Promise that resolves with an object containing
328
- * information about the refreshed access token.
329
- */
330
- public async refreshAccessToken(): Promise<BasicUserInfo> {
331
- return this._client.refreshAccessToken();
332
- }
333
-
334
- /**
335
- * This method specifies if the user is authenticated or not.
336
- *
337
- * @return {Promise<boolean>} - A Promise that resolves with `true` if teh user is authenticated.
338
- */
339
- public async isAuthenticated(): Promise<boolean> {
340
- return this._client.isAuthenticated();
341
- }
342
-
343
- /**
344
- * This method specifies if the session is active or not.
345
- *
346
- * @return {Promise<boolean>} - A Promise that resolves with `true` if there is an active session.
347
- */
348
- public async isSessionActive(): Promise<boolean> {
349
- return this._client.isSessionActive();
350
- }
351
-
352
- /**
353
- * This method enables callback functions attached to the http client.
354
- *
355
- * @return {Promise<boolean>} - A promise that resolves with True.
356
- *
357
- */
358
- public async enableHttpHandler(): Promise<boolean> {
359
- return this._client.enableHttpHandler();
360
- }
361
-
362
- /**
363
- * This method disables callback functions attached to the http client.
364
- *
365
- * @return {Promise<boolean>} - A promise that resolves with True.
366
- */
367
- public async disableHttpHandler(): Promise<boolean> {
368
- return this._client.disableHttpHandler();
369
- }
370
-
371
- /**
372
- * This method updates the configuration that was passed into the constructor when instantiating this class.
373
- *
374
- * @param {Partial<AuthClientConfig<T>>} config - A config object to update the SDK configurations with.
375
- */
376
- public async updateConfig(config: Partial<AuthClientConfig<Config>>): Promise<void> {
377
- return this._client.updateConfig(config);
378
- }
379
-
380
- /**
381
- * This method attaches a callback function to an event hook that fires the callback when the event happens.
382
- *
383
- * @param {Hooks.CustomGrant} hook - The name of the hook.
384
- * @param {(response?: any) => void} callback - The callback function.
385
- * @param {string} id (optional) - The id of the hook. This is used when multiple custom grants are used.
386
- *
387
- */
388
- public on(hook: Hooks.CustomGrant, callback: (response?: any) => void, id: string): Promise<void>;
389
- public on(
390
- hook: Exclude<Hooks, Hooks.CustomGrant>,
391
- callback: (response?: any) => void
392
- ): Promise<void>;
393
- public on(hook: Hooks, callback: (response?: any) => void, id?: string): Promise<void> {
394
- if (hook === Hooks.CustomGrant) {
395
- return this._client.on(hook, callback, id);
396
- }
397
-
398
- return this._client.on(hook, callback);
399
- }
400
-
401
- /**
402
- * This method allows you to sign in silently.
403
- * First, this method sends a prompt none request to see if there is an active user session in the identity server.
404
- * If there is one, then it requests the access token and stores it. Else, it returns false.
405
- *
406
- * @return {Promise<BasicUserInfo | boolean>} - A Promise that resolves with the user information after signing in
407
- * or with `false` if the user is not signed in.
408
- *
409
- * @example
410
- *```
411
- * client.trySignInSilently()
412
- *```
413
- */
414
- public async trySignInSilently(
415
- state: AuthStateInterface,
416
- dispatch: (state: AuthStateInterface) => void,
417
- additionalParams?: Record<string, string | boolean>,
418
- tokenRequestConfig?: { params: Record<string, unknown> }
419
- ): Promise<BasicUserInfo | boolean | undefined> {
420
- return this._client
421
- .trySignInSilently(additionalParams, tokenRequestConfig)
422
- .then(async (response: BasicUserInfo | boolean) => {
423
- if (!response) {
424
- this.updateState({ ...this.getState(), isLoading: false });
425
- dispatch({ ...state, isLoading: false });
426
-
427
- return false;
428
- }
429
-
430
- if (await this._client.isAuthenticated()) {
431
- const basicUserInfo = response as BasicUserInfo;
432
- const stateToUpdate = {
433
- allowedScopes: basicUserInfo.allowedScopes,
434
- displayName: basicUserInfo.displayName,
435
- email: basicUserInfo.email,
436
- isAuthenticated: true,
437
- isLoading: false,
438
- isSigningOut: false,
439
- sub: basicUserInfo.sub,
440
- username: basicUserInfo.username
441
- };
442
-
443
- this.updateState(stateToUpdate);
444
-
445
- dispatch({ ...state, ...stateToUpdate });
446
- }
447
-
448
- return response;
449
- })
450
- .catch((error) => {
451
- return Promise.reject(error);
452
- });
453
- }
454
- }
455
-
456
- AuthAPI.DEFAULT_STATE = {
457
- allowedScopes: "",
458
- displayName: "",
459
- email: "",
460
- isAuthenticated: false,
461
- isLoading: true,
462
- sub: "",
463
- username: ""
464
- };
465
-
466
- export default AuthAPI;