From d2c16aa20604199ae7105736113fa77b81f16244 Mon Sep 17 00:00:00 2001 From: Movin Silva Date: Mon, 22 Apr 2024 11:22:24 +0530 Subject: [PATCH 01/12] feat(core): :sparkles: add me function --- packages/core/src/api/me.ts | 59 +++++++++++++++++++ packages/core/src/models/me-api-response.ts | 63 +++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 packages/core/src/api/me.ts create mode 100644 packages/core/src/models/me-api-response.ts diff --git a/packages/core/src/api/me.ts b/packages/core/src/api/me.ts new file mode 100644 index 00000000..d96fb916 --- /dev/null +++ b/packages/core/src/api/me.ts @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved. + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import {AuthClient} from 'src/auth-client'; +import AsgardeoUIException from 'src/exception'; +import {MeAPIResponse} from 'src/models/me-api-response'; + +const me = async (): Promise => { + let baseUrl: string; + let accessToken: string; + try { + baseUrl = (await AuthClient.getInstance().getDataLayer().getConfigData()).baseUrl; + accessToken = await AuthClient.getInstance().getAccessToken(); + } catch (error) { + throw new AsgardeoUIException('JS_UI_CORE-ME-M-NF', 'Failed in getting the base URL and access token', error.stack); + } + + if (!accessToken) { + throw new AsgardeoUIException('JS_UI_CORE-ME-M-IV', 'Access token is null'); + } + + const headers: Headers = new Headers(); + headers.append('Authorization', `Bearer ${accessToken}`); + headers.append('Content-Type', 'application/json'); + + const requestOptions: RequestInit = { + headers, + method: 'GET', + }; + let response: Response; + try { + // process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; + response = await fetch(new Request(`${baseUrl}/scim2/Me`, requestOptions)); + // process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1'; + } catch (error) { + throw new AsgardeoUIException('JS_UI_CORE-ME-M-NE', 'Me API call failed', error); + } + if (response.ok) { + return (await response.json()) as MeAPIResponse; + } + throw new AsgardeoUIException('JS_UI_CORE-ME-M-HE', 'Me response is not OK'); +}; + +export default me; diff --git a/packages/core/src/models/me-api-response.ts b/packages/core/src/models/me-api-response.ts new file mode 100644 index 00000000..9cde4456 --- /dev/null +++ b/packages/core/src/models/me-api-response.ts @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved. + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * The interface for the response from the "me" API endpoint. + */ +export interface MeAPIResponse { + /** + * The user's email addresses. + */ + emails: string[]; + /** + * The user's id. + */ + id: string; + /** + * The user's names. + */ + name: Name; + /** + * When signed in using a social login, the photos field will be populated instead of profile URL. + */ + photos?: Photos[]; + /** + * The user's profile URL. + */ + profileUrl: string; + /** + * The user's username. + */ + userName: string; +} + +/** + * The interface for the name field in the "me" API response. + */ +export interface Name { + familyName?: string; + givenName?: string; +} + +/** + * The interface for the photos field in the "me" API response. + */ +export interface Photos { + type: string; + value: string; +} From 14a5d9a509c900b2626caef96f48c8ffe71408d4 Mon Sep 17 00:00:00 2001 From: Movin Silva Date: Mon, 22 Apr 2024 11:25:07 +0530 Subject: [PATCH 02/12] refactor(core): --- packages/core/src/branding/branding.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/core/src/branding/branding.ts b/packages/core/src/branding/branding.ts index 688db1f7..bae28eed 100644 --- a/packages/core/src/branding/branding.ts +++ b/packages/core/src/branding/branding.ts @@ -55,3 +55,5 @@ export const getBranding = async (props: GetBranding): Promise => return mergedBranding; }; + +export default getBranding; From a387166eb7215da6f45bf43a45ceec754a038c5b Mon Sep 17 00:00:00 2001 From: Movin Silva Date: Mon, 22 Apr 2024 11:28:19 +0530 Subject: [PATCH 03/12] feat(core): :sparkles: add public api expose api functions --- packages/core/src/api/public-api.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 packages/core/src/api/public-api.ts diff --git a/packages/core/src/api/public-api.ts b/packages/core/src/api/public-api.ts new file mode 100644 index 00000000..601d2e91 --- /dev/null +++ b/packages/core/src/api/public-api.ts @@ -0,0 +1,23 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved. + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export {default as authorize} from './authorize'; +export {default as authenticate} from './authenticate'; +export {default as branding} from './branding'; +export {default as me} from './me'; +export {default as brandingText} from './branding-text'; From c3333d121896cea83c35dac4a5379216c24bbf6d Mon Sep 17 00:00:00 2001 From: Movin Silva Date: Mon, 22 Apr 2024 11:32:42 +0530 Subject: [PATCH 04/12] feat(core): :sparkles: export required functions from js-ui-core --- packages/core/src/i18n/public.ts | 1 + packages/core/src/index.ts | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packages/core/src/i18n/public.ts b/packages/core/src/i18n/public.ts index 09e800ac..34ffb7ba 100644 --- a/packages/core/src/i18n/public.ts +++ b/packages/core/src/i18n/public.ts @@ -18,3 +18,4 @@ export * from './screens/model'; export * from './screens/keys'; +export {default as getLocalization} from './i18n'; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index e69de29b..9a0a3d46 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -0,0 +1,22 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved. + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export * from './api/public-api'; +export {default as getBranding} from './branding/branding'; +export * from './i18n/public'; +export * from './auth-client'; From 7f7d88ac6950af5d096ec166cdb0ac0da97e9d2d Mon Sep 17 00:00:00 2001 From: Movin Silva Date: Mon, 22 Apr 2024 11:37:34 +0530 Subject: [PATCH 05/12] feat(core): :sparkles: export required models from js-ui-core --- packages/core/src/index.ts | 1 + packages/core/src/models/public-models.ts | 25 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 packages/core/src/models/public-models.ts diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 9a0a3d46..1f9e05a8 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -20,3 +20,4 @@ export * from './api/public-api'; export {default as getBranding} from './branding/branding'; export * from './i18n/public'; export * from './auth-client'; +export * from './models/public-models'; diff --git a/packages/core/src/models/public-models.ts b/packages/core/src/models/public-models.ts new file mode 100644 index 00000000..6fdce93d --- /dev/null +++ b/packages/core/src/models/public-models.ts @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved. + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export * from './auth-api-request'; +export * from './auth-api-response'; +export * from './auth-config'; +export * from './branding-api-response'; +export * from './customization'; +export * from './me-api-response'; +export * from './screen-type'; From b8aef0d03d3d0ae0d8efd6bd9f479a2b8d3c2404 Mon Sep 17 00:00:00 2001 From: Movin Silva Date: Mon, 29 Apr 2024 19:21:18 +0530 Subject: [PATCH 06/12] docs(core): :memo: update internal headers --- packages/core/src/api/me.ts | 2 +- packages/core/src/api/public-api.ts | 2 +- packages/core/src/index.ts | 2 +- packages/core/src/models/me-api-response.ts | 2 +- packages/core/src/models/public-models.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/core/src/api/me.ts b/packages/core/src/api/me.ts index d96fb916..66f1e93d 100644 --- a/packages/core/src/api/me.ts +++ b/packages/core/src/api/me.ts @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved. + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/packages/core/src/api/public-api.ts b/packages/core/src/api/public-api.ts index 601d2e91..7089a90a 100644 --- a/packages/core/src/api/public-api.ts +++ b/packages/core/src/api/public-api.ts @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved. + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 1f9e05a8..588ab8b7 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved. + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/packages/core/src/models/me-api-response.ts b/packages/core/src/models/me-api-response.ts index 9cde4456..8b9fe3e7 100644 --- a/packages/core/src/models/me-api-response.ts +++ b/packages/core/src/models/me-api-response.ts @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved. + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/packages/core/src/models/public-models.ts b/packages/core/src/models/public-models.ts index 6fdce93d..f770cf47 100644 --- a/packages/core/src/models/public-models.ts +++ b/packages/core/src/models/public-models.ts @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved. + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except From b9bbc59fe766a1a050f3ab5badc99a5fb3f66e82 Mon Sep 17 00:00:00 2001 From: Movin Silva Date: Mon, 29 Apr 2024 19:24:06 +0530 Subject: [PATCH 07/12] refactor(core): :recycle: update exports and paths in branding --- packages/core/src/api/branding-preference-text.ts | 4 +++- packages/core/src/api/public-api.ts | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/core/src/api/branding-preference-text.ts b/packages/core/src/api/branding-preference-text.ts index 127d0822..38a1a7d3 100644 --- a/packages/core/src/api/branding-preference-text.ts +++ b/packages/core/src/api/branding-preference-text.ts @@ -30,7 +30,7 @@ import {BrandingPreferenceTextAPIResponse} from 'src/models/branding-text-api-re * @returns A Promise that resolves to the response from the server. * @throws {AsgardeoUIException} If the API call fails or when the response is not successful. */ -export const getBrandingPreferenceText = async ( +const getBrandingPreferenceText = async ( locale: string, name: string, screen: string, @@ -71,3 +71,5 @@ export const getBrandingPreferenceText = async ( 'Failed to receive a successful response from the branding text endpoint', ); }; + +export default getBrandingPreferenceText; diff --git a/packages/core/src/api/public-api.ts b/packages/core/src/api/public-api.ts index 7089a90a..35398d34 100644 --- a/packages/core/src/api/public-api.ts +++ b/packages/core/src/api/public-api.ts @@ -18,6 +18,6 @@ export {default as authorize} from './authorize'; export {default as authenticate} from './authenticate'; -export {default as branding} from './branding'; +export {default as getBrandingPreference} from './branding-preference'; export {default as me} from './me'; -export {default as brandingText} from './branding-text'; +export {default as getBrandingPreferenceText} from './branding-preference-text'; From 8fcc99c33c72ac4f258a1b8df5450fd1b263e62d Mon Sep 17 00:00:00 2001 From: Movin Silva Date: Mon, 29 Apr 2024 19:28:20 +0530 Subject: [PATCH 08/12] fix(core): :bug: update import style --- packages/core/src/i18n/i18n.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/i18n/i18n.ts b/packages/core/src/i18n/i18n.ts index e9994c09..6347f17b 100644 --- a/packages/core/src/i18n/i18n.ts +++ b/packages/core/src/i18n/i18n.ts @@ -17,7 +17,7 @@ */ import merge from 'lodash.merge'; -import {getBrandingPreferenceText} from 'src/api/branding-preference-text'; +import getBrandingPreferenceText from 'src/api/branding-preference-text'; import {AuthClient} from 'src/auth-client'; import {BrandingPreferenceTextAPIResponse} from 'src/models/branding-text-api-response'; import {GetLocalization, TextObject} from './screens/model'; From d453b073db1ff5c18d8afb2794300aa9a0d11c88 Mon Sep 17 00:00:00 2001 From: Movin Silva Date: Mon, 29 Apr 2024 20:27:32 +0530 Subject: [PATCH 09/12] style(core): :art: improve formatting of me function --- packages/core/src/api/me.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/core/src/api/me.ts b/packages/core/src/api/me.ts index 66f1e93d..2a8cf1bf 100644 --- a/packages/core/src/api/me.ts +++ b/packages/core/src/api/me.ts @@ -23,15 +23,21 @@ import {MeAPIResponse} from 'src/models/me-api-response'; const me = async (): Promise => { let baseUrl: string; let accessToken: string; + let response: Response; + try { baseUrl = (await AuthClient.getInstance().getDataLayer().getConfigData()).baseUrl; accessToken = await AuthClient.getInstance().getAccessToken(); } catch (error) { - throw new AsgardeoUIException('JS_UI_CORE-ME-M-NF', 'Failed in getting the base URL and access token', error.stack); + throw new AsgardeoUIException( + 'JS_UI_CORE-ME-M-NF', + 'Failed in getting the base URL and access token.', + error.stack, + ); } if (!accessToken) { - throw new AsgardeoUIException('JS_UI_CORE-ME-M-IV', 'Access token is null'); + throw new AsgardeoUIException('JS_UI_CORE-ME-M-IV', 'Access token is null.'); } const headers: Headers = new Headers(); @@ -42,18 +48,18 @@ const me = async (): Promise => { headers, method: 'GET', }; - let response: Response; + try { - // process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; response = await fetch(new Request(`${baseUrl}/scim2/Me`, requestOptions)); - // process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1'; } catch (error) { - throw new AsgardeoUIException('JS_UI_CORE-ME-M-NE', 'Me API call failed', error); + throw new AsgardeoUIException('JS_UI_CORE-ME-M-NE', 'Me API call failed.', error.stack); } + if (response.ok) { return (await response.json()) as MeAPIResponse; } - throw new AsgardeoUIException('JS_UI_CORE-ME-M-HE', 'Me response is not OK'); + + throw new AsgardeoUIException('JS_UI_CORE-ME-M-HE', 'Failed to receive a successful response from the Me API.'); }; export default me; From 53b413a81e5e325f90cc4792410cde40218825b9 Mon Sep 17 00:00:00 2001 From: Movin Silva Date: Tue, 30 Apr 2024 08:00:00 +0530 Subject: [PATCH 10/12] refactor(core): :recycle: rename me function and the file --- packages/core/src/api/{me.ts => profile.ts} | 4 ++-- packages/core/src/api/public-api.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename packages/core/src/api/{me.ts => profile.ts} (94%) diff --git a/packages/core/src/api/me.ts b/packages/core/src/api/profile.ts similarity index 94% rename from packages/core/src/api/me.ts rename to packages/core/src/api/profile.ts index 2a8cf1bf..21bc5ef9 100644 --- a/packages/core/src/api/me.ts +++ b/packages/core/src/api/profile.ts @@ -20,7 +20,7 @@ import {AuthClient} from 'src/auth-client'; import AsgardeoUIException from 'src/exception'; import {MeAPIResponse} from 'src/models/me-api-response'; -const me = async (): Promise => { +const getProfileInformation = async (): Promise => { let baseUrl: string; let accessToken: string; let response: Response; @@ -62,4 +62,4 @@ const me = async (): Promise => { throw new AsgardeoUIException('JS_UI_CORE-ME-M-HE', 'Failed to receive a successful response from the Me API.'); }; -export default me; +export default getProfileInformation; diff --git a/packages/core/src/api/public-api.ts b/packages/core/src/api/public-api.ts index 35398d34..f87cf4a4 100644 --- a/packages/core/src/api/public-api.ts +++ b/packages/core/src/api/public-api.ts @@ -19,5 +19,5 @@ export {default as authorize} from './authorize'; export {default as authenticate} from './authenticate'; export {default as getBrandingPreference} from './branding-preference'; -export {default as me} from './me'; +export {default as getProfileInformation} from './profile'; export {default as getBrandingPreferenceText} from './branding-preference-text'; From b7bfa979ac040673bcece61fb66bf3fa76616953 Mon Sep 17 00:00:00 2001 From: Movin Silva Date: Tue, 30 Apr 2024 09:12:50 +0530 Subject: [PATCH 11/12] docs(core): :memo: add js docs for getProfileInformation function --- packages/core/src/api/profile.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/core/src/api/profile.ts b/packages/core/src/api/profile.ts index 21bc5ef9..32add783 100644 --- a/packages/core/src/api/profile.ts +++ b/packages/core/src/api/profile.ts @@ -20,6 +20,15 @@ import {AuthClient} from 'src/auth-client'; import AsgardeoUIException from 'src/exception'; import {MeAPIResponse} from 'src/models/me-api-response'; +/** + * Fetch the profile information of the authenticated user. + * + * This function uses the `AuthClient` instance to get the base URL and access token, + * and then makes a GET request to the `/scim2/Me` endpoint to fetch the user's profile information. + * + * @returns {Promise} A promise that resolves to an object containing the user's profile information. + * @throws {AsgardeoUIException} Throws an exception if there's an error getting the base URL and access token, or if the fetch request fails. + */ const getProfileInformation = async (): Promise => { let baseUrl: string; let accessToken: string; From 43e3cf4f7d0a929d0a60334888e65dd5830691f2 Mon Sep 17 00:00:00 2001 From: Movin Silva Date: Tue, 30 Apr 2024 10:48:50 +0530 Subject: [PATCH 12/12] refactor(core): :recycle: update error codes in getProfileInformation --- packages/core/src/api/profile.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/core/src/api/profile.ts b/packages/core/src/api/profile.ts index 32add783..c515f96c 100644 --- a/packages/core/src/api/profile.ts +++ b/packages/core/src/api/profile.ts @@ -39,14 +39,14 @@ const getProfileInformation = async (): Promise => { accessToken = await AuthClient.getInstance().getAccessToken(); } catch (error) { throw new AsgardeoUIException( - 'JS_UI_CORE-ME-M-NF', + 'JS_UI_CORE-ME-GPI-NF', 'Failed in getting the base URL and access token.', error.stack, ); } if (!accessToken) { - throw new AsgardeoUIException('JS_UI_CORE-ME-M-IV', 'Access token is null.'); + throw new AsgardeoUIException('JS_UI_CORE-ME-GPI-IV', 'Access token is null.'); } const headers: Headers = new Headers(); @@ -61,14 +61,14 @@ const getProfileInformation = async (): Promise => { try { response = await fetch(new Request(`${baseUrl}/scim2/Me`, requestOptions)); } catch (error) { - throw new AsgardeoUIException('JS_UI_CORE-ME-M-NE', 'Me API call failed.', error.stack); + throw new AsgardeoUIException('JS_UI_CORE-ME-GPI-NE', 'Me API call failed.', error.stack); } if (response.ok) { return (await response.json()) as MeAPIResponse; } - throw new AsgardeoUIException('JS_UI_CORE-ME-M-HE', 'Failed to receive a successful response from the Me API.'); + throw new AsgardeoUIException('JS_UI_CORE-ME-GPI-HE', 'Failed to receive a successful response from the Me API.'); }; export default getProfileInformation;