From 6390602c9da68c2b830e882f7e36ef9f5a57fc8d Mon Sep 17 00:00:00 2001 From: Riddhiagrawal001 <50551695+Riddhiagrawal001@users.noreply.github.com> Date: Fri, 14 Jun 2024 11:34:59 +0530 Subject: [PATCH] chore: sso new enum addition (#825) --- src/components/Navbar.res | 1 + src/context/AuthInfoProvider.res | 10 +++++- src/context/UserPrefContext.res | 2 +- .../AuthModule/AuthProviderTypes.res | 20 ++++++++++-- src/entryPoints/AuthModule/AuthUtils.res | 27 ++++++++++++++++ src/entryPoints/AuthModule/AuthWrapper.res | 3 +- .../AuthModule/BasicAuth/BasicAuthWrapper.res | 1 + .../AuthModule/Common/CommonAuthHooks.res | 2 +- .../AuthModule/SSOAuth/SSODecisionScreen.res | 4 +++ .../TwoFaAuth/TwoFaDecisionScreen.res | 2 +- .../AuthModule/TwoFaAuth/TwoFaTypes.res | 10 ------ .../AuthModule/TwoFaAuth/TwoFaUtils.res | 31 ++----------------- ...pUserInfoScreen.res => UserInfoScreen.res} | 4 +-- src/hooks/AuthHooks.res | 2 +- src/screens/Home/Home.res | 2 +- src/screens/Home/HomeV2.res | 2 +- .../HSwitchProfile/HSwitchProfileSettings.res | 2 +- src/screens/SwitchMerchant/SwitchMerchant.res | 6 ++-- 18 files changed, 75 insertions(+), 56 deletions(-) create mode 100644 src/entryPoints/AuthModule/AuthUtils.res create mode 100644 src/entryPoints/AuthModule/SSOAuth/SSODecisionScreen.res rename src/entryPoints/AuthModule/{TwoFaAuth/Totp/TotpUserInfoScreen.res => UserInfoScreen.res} (92%) diff --git a/src/components/Navbar.res b/src/components/Navbar.res index 91e1221bc..22ae8fdfb 100644 --- a/src/components/Navbar.res +++ b/src/components/Navbar.res @@ -123,6 +123,7 @@ let make = ( // | LoggedOut => React.null + | SSOPreLogin(_) | PreLogin(_) | CheckingAuthStatus => React.string("...") diff --git a/src/context/AuthInfoProvider.res b/src/context/AuthInfoProvider.res index 8515468f5..17016c6ed 100644 --- a/src/context/AuthInfoProvider.res +++ b/src/context/AuthInfoProvider.res @@ -39,7 +39,7 @@ let make = (~children) => { CommonAuthUtils.clearLocalStorage() } } - | TotpAuth(totpInfo) => + | Auth(totpInfo) => if !(totpInfo.token->LogicUtils.isEmptyString) { setAuth(_ => newAuthStatus) TwoFaUtils.setTotpAuthResToStorage(totpInfo) @@ -56,6 +56,14 @@ let make = (~children) => { setAuth(_ => LoggedOut) CommonAuthUtils.clearLocalStorage() } + | SSOPreLogin(ssoPreloginInfo) => + if !(ssoPreloginInfo.token->LogicUtils.isEmptyString) { + setAuth(_ => newAuthStatus) + TwoFaUtils.setTotpAuthResToStorage(ssoPreloginInfo) + } else { + setAuth(_ => LoggedOut) + CommonAuthUtils.clearLocalStorage() + } | LoggedOut => { setAuth(_ => LoggedOut) diff --git a/src/context/UserPrefContext.res b/src/context/UserPrefContext.res index 76514a74b..a9326932c 100644 --- a/src/context/UserPrefContext.res +++ b/src/context/UserPrefContext.res @@ -45,7 +45,7 @@ let make = (~children) => { | LoggedIn(authType) => switch authType { | BasicAuth(basicAuthInfo) => basicAuthInfo.name->Option.getOr("") - | TotpAuth(totpAuthInfo) => totpAuthInfo.name + | Auth(totpAuthInfo) => totpAuthInfo.name } | _ => "" } diff --git a/src/entryPoints/AuthModule/AuthProviderTypes.res b/src/entryPoints/AuthModule/AuthProviderTypes.res index a0127ea57..4f346887b 100644 --- a/src/entryPoints/AuthModule/AuthProviderTypes.res +++ b/src/entryPoints/AuthModule/AuthProviderTypes.res @@ -3,6 +3,22 @@ type preLoginType = { token_type: string, email_token: option, } -type authType = BasicAuth(BasicAuthTypes.basicAuthInfo) | TotpAuth(TwoFaTypes.twoFaAuthInfo) -type authStatus = LoggedOut | PreLogin(preLoginType) | LoggedIn(authType) | CheckingAuthStatus +type authInfo = { + token: string, + merchant_id: string, + name: string, + email: string, + role_id: string, + is_two_factor_auth_setup: bool, + recovery_codes_left: int, +} + +type authType = BasicAuth(BasicAuthTypes.basicAuthInfo) | Auth(authInfo) + +type authStatus = + | LoggedOut + | SSOPreLogin(preLoginType) + | PreLogin(preLoginType) + | LoggedIn(authType) + | CheckingAuthStatus diff --git a/src/entryPoints/AuthModule/AuthUtils.res b/src/entryPoints/AuthModule/AuthUtils.res new file mode 100644 index 000000000..bb61e5c2f --- /dev/null +++ b/src/entryPoints/AuthModule/AuthUtils.res @@ -0,0 +1,27 @@ +let storeEmailTokenTmp = emailToken => { + LocalStorage.setItem("email_token", emailToken) +} + +let getAuthInfo = (~email_token=None, json) => { + open LogicUtils + open AuthProviderTypes + let dict = json->JsonFlattenUtils.flattenObject(false) + let totpInfo = { + email: getString(dict, "email", ""), + merchant_id: getString(dict, "merchant_id", ""), + name: getString(dict, "name", ""), + token: getString(dict, "token", ""), + role_id: getString(dict, "role_id", ""), + is_two_factor_auth_setup: getBool(dict, "is_two_factor_auth_setup", false), + recovery_codes_left: getInt( + dict, + "recovery_codes_left", + HSwitchGlobalVars.maximumRecoveryCodes, + ), + } + switch email_token { + | Some(emailTk) => emailTk->storeEmailTokenTmp + | None => () + } + totpInfo +} diff --git a/src/entryPoints/AuthModule/AuthWrapper.res b/src/entryPoints/AuthModule/AuthWrapper.res index dd7549a18..94362236b 100644 --- a/src/entryPoints/AuthModule/AuthWrapper.res +++ b/src/entryPoints/AuthModule/AuthWrapper.res @@ -60,7 +60,7 @@ let make = (~children) => { loggedInInfo.merchant_id->isNonEmptyString && loggedInInfo.email->isNonEmptyString ) { - setAuthStatus(LoggedIn(TotpAuth(loggedInInfo))) + setAuthStatus(LoggedIn(Auth(loggedInInfo))) } else if preLoginInfo.token->isNonEmptyString && preLoginInfo.token_type->isNonEmptyString { setAuthStatus(PreLogin(preLoginInfo)) } else { @@ -107,6 +107,7 @@ let make = (~children) => { + | SSOPreLogin(_) => | PreLogin(_) => | LoggedIn(_token) => children | CheckingAuthStatus => diff --git a/src/entryPoints/AuthModule/BasicAuth/BasicAuthWrapper.res b/src/entryPoints/AuthModule/BasicAuth/BasicAuthWrapper.res index 9999446b8..e140a66e8 100644 --- a/src/entryPoints/AuthModule/BasicAuth/BasicAuthWrapper.res +++ b/src/entryPoints/AuthModule/BasicAuth/BasicAuthWrapper.res @@ -27,6 +27,7 @@ let make = (~children) => {
{switch authStatus { | LoggedOut => + | SSOPreLogin(_) | PreLogin(_) | LoggedIn(_) => children | CheckingAuthStatus => diff --git a/src/entryPoints/AuthModule/Common/CommonAuthHooks.res b/src/entryPoints/AuthModule/Common/CommonAuthHooks.res index e74622a36..e84f70768 100644 --- a/src/entryPoints/AuthModule/Common/CommonAuthHooks.res +++ b/src/entryPoints/AuthModule/Common/CommonAuthHooks.res @@ -77,7 +77,7 @@ let useCommonAuthInfo = () => { email: email->Option.getOr(""), user_role: user_role->Option.getOr(""), }) - | TotpAuth({token, merchant_id, name, email, role_id}) => + | Auth({token, merchant_id, name, email, role_id}) => Some({ token, merchant_id, diff --git a/src/entryPoints/AuthModule/SSOAuth/SSODecisionScreen.res b/src/entryPoints/AuthModule/SSOAuth/SSODecisionScreen.res new file mode 100644 index 000000000..0e948fce8 --- /dev/null +++ b/src/entryPoints/AuthModule/SSOAuth/SSODecisionScreen.res @@ -0,0 +1,4 @@ +@react.component +let make = () => { + React.null +} diff --git a/src/entryPoints/AuthModule/TwoFaAuth/TwoFaDecisionScreen.res b/src/entryPoints/AuthModule/TwoFaAuth/TwoFaDecisionScreen.res index a984c82a8..afe675722 100644 --- a/src/entryPoints/AuthModule/TwoFaAuth/TwoFaDecisionScreen.res +++ b/src/entryPoints/AuthModule/TwoFaAuth/TwoFaDecisionScreen.res @@ -21,7 +21,7 @@ let make = () => { | ACCEPT_INVITATION_FROM_EMAIL => | VERIFY_EMAIL => - | USER_INFO => + | USER_INFO => | ERROR => } } diff --git a/src/entryPoints/AuthModule/TwoFaAuth/TwoFaTypes.res b/src/entryPoints/AuthModule/TwoFaAuth/TwoFaTypes.res index 9831d9789..ccc61eddf 100644 --- a/src/entryPoints/AuthModule/TwoFaAuth/TwoFaTypes.res +++ b/src/entryPoints/AuthModule/TwoFaAuth/TwoFaTypes.res @@ -9,16 +9,6 @@ type twoFaFlowType = | USER_INFO | ERROR -type twoFaAuthInfo = { - token: string, - merchant_id: string, - name: string, - email: string, - role_id: string, - is_two_factor_auth_setup: bool, - recovery_codes_left: int, -} - type twoFaPageState = TOTP_SHOW_QR | TOTP_SHOW_RC | TOTP_INPUT_RECOVERY_CODE type twoFaStatus = TWO_FA_NOT_SET | TWO_FA_SET diff --git a/src/entryPoints/AuthModule/TwoFaAuth/TwoFaUtils.res b/src/entryPoints/AuthModule/TwoFaAuth/TwoFaUtils.res index 7c12211d2..7f600b253 100644 --- a/src/entryPoints/AuthModule/TwoFaAuth/TwoFaUtils.res +++ b/src/entryPoints/AuthModule/TwoFaAuth/TwoFaUtils.res @@ -60,10 +60,6 @@ let getEmailTmpToken = () => { LocalStorage.getItem("email_token")->Nullable.toOption } -let storeEmailTokenTmp = emailToken => { - LocalStorage.setItem("email_token", emailToken) -} - let getEmailTokenValue = email_token => { let tmpEmailToken = getEmailTmpToken() switch email_token { @@ -72,29 +68,6 @@ let getEmailTokenValue = email_token => { } } -let getTotpAuthInfo = (~email_token=None, json) => { - open LogicUtils - let dict = json->JsonFlattenUtils.flattenObject(false) - let totpInfo = { - email: getString(dict, "email", ""), - merchant_id: getString(dict, "merchant_id", ""), - name: getString(dict, "name", ""), - token: getString(dict, "token", ""), - role_id: getString(dict, "role_id", ""), - is_two_factor_auth_setup: getBool(dict, "is_two_factor_auth_setup", false), - recovery_codes_left: getInt( - dict, - "recovery_codes_left", - HSwitchGlobalVars.maximumRecoveryCodes, - ), - } - switch email_token { - | Some(emailTk) => emailTk->storeEmailTokenTmp - | None => () - } - totpInfo -} - let getPreLoginInfo = (~email_token=None, json) => { open LogicUtils let dict = json->JsonFlattenUtils.flattenObject(false) @@ -104,7 +77,7 @@ let getPreLoginInfo = (~email_token=None, json) => { email_token: email_token->getEmailTokenValue, } switch email_token { - | Some(emailTk) => emailTk->storeEmailTokenTmp + | Some(emailTk) => emailTk->AuthUtils.storeEmailTokenTmp | None => () } preLoginInfo @@ -123,7 +96,7 @@ let getTotpPreLoginInfoFromStorage = () => { let getTotpAuthInfoFromStrorage = () => { open LogicUtils let json = LocalStorage.getItem("USER_INFO")->getValFromNullableValue("")->safeParse - json->getTotpAuthInfo + json->AuthUtils.getAuthInfo } let getEmailToken = (authStatus: AuthProviderTypes.authStatus) => { diff --git a/src/entryPoints/AuthModule/TwoFaAuth/Totp/TotpUserInfoScreen.res b/src/entryPoints/AuthModule/UserInfoScreen.res similarity index 92% rename from src/entryPoints/AuthModule/TwoFaAuth/Totp/TotpUserInfoScreen.res rename to src/entryPoints/AuthModule/UserInfoScreen.res index 65634008f..9e03afad2 100644 --- a/src/entryPoints/AuthModule/TwoFaAuth/Totp/TotpUserInfoScreen.res +++ b/src/entryPoints/AuthModule/UserInfoScreen.res @@ -20,8 +20,8 @@ let make = () => { let response = await fetchDetails(url) let dict = response->getDictFromJsonObject dict->setOptionString("token", token) - let info = TwoFaUtils.getTotpAuthInfo(dict->JSON.Encode.object) - setAuthStatus(LoggedIn(TotpAuth(info))) + let info = AuthUtils.getAuthInfo(dict->JSON.Encode.object) + setAuthStatus(LoggedIn(Auth(info))) setIsSidebarDetails("isPinned", false->JSON.Encode.bool) setScreenState(_ => PageLoaderWrapper.Success) } catch { diff --git a/src/hooks/AuthHooks.res b/src/hooks/AuthHooks.res index 8582c1391..acc41a355 100644 --- a/src/hooks/AuthHooks.res +++ b/src/hooks/AuthHooks.res @@ -49,7 +49,7 @@ let useApiFetcher = () => { | LoggedIn(info) => switch info { | BasicAuth(basicInfo) => basicInfo.token - | TotpAuth(totpInfo) => Some(totpInfo.token) + | Auth(info) => Some(info.token) } | _ => None } diff --git a/src/screens/Home/Home.res b/src/screens/Home/Home.res index 838ec67e0..664eca486 100644 --- a/src/screens/Home/Home.res +++ b/src/screens/Home/Home.res @@ -8,7 +8,7 @@ let make = () => { let {authStatus} = React.useContext(AuthInfoProvider.authStatusContext) let recovery_codes_left = switch authStatus { - | LoggedIn(TotpAuth(totpInfo)) => totpInfo.recovery_codes_left + | LoggedIn(Auth(totpInfo)) => totpInfo.recovery_codes_left | _ => HSwitchGlobalVars.maximumRecoveryCodes } diff --git a/src/screens/Home/HomeV2.res b/src/screens/Home/HomeV2.res index 2ee476980..c4c44c474 100644 --- a/src/screens/Home/HomeV2.res +++ b/src/screens/Home/HomeV2.res @@ -423,7 +423,7 @@ let make = () => { let {authStatus} = React.useContext(AuthInfoProvider.authStatusContext) let recovery_codes_left = switch authStatus { - | LoggedIn(TotpAuth(totpInfo)) => totpInfo.recovery_codes_left + | LoggedIn(Auth(info)) => info.recovery_codes_left | _ => HSwitchGlobalVars.maximumRecoveryCodes } diff --git a/src/screens/Settings/HSwitchProfile/HSwitchProfileSettings.res b/src/screens/Settings/HSwitchProfile/HSwitchProfileSettings.res index 93566b3d2..ea469a44a 100644 --- a/src/screens/Settings/HSwitchProfile/HSwitchProfileSettings.res +++ b/src/screens/Settings/HSwitchProfile/HSwitchProfileSettings.res @@ -197,7 +197,7 @@ let make = () => { let {authStatus} = React.useContext(AuthInfoProvider.authStatusContext) let showTwoFaSettings = switch authStatus { - | LoggedIn(TotpAuth(totpAuthInfo)) => totpAuthInfo.is_two_factor_auth_setup + | LoggedIn(Auth(authInfo)) => authInfo.is_two_factor_auth_setup | _ => false } diff --git a/src/screens/SwitchMerchant/SwitchMerchant.res b/src/screens/SwitchMerchant/SwitchMerchant.res index a8ef5cad8..7224570d3 100644 --- a/src/screens/SwitchMerchant/SwitchMerchant.res +++ b/src/screens/SwitchMerchant/SwitchMerchant.res @@ -256,7 +256,7 @@ let make = (~userRole, ~isAddMerchantEnabled=false) => { | LoggedIn(info) => switch info { | BasicAuth(basicInfo) => basicInfo.merchant_id->Option.getOr("") - | TotpAuth(totpInfo) => totpInfo.merchant_id + | Auth(totpInfo) => totpInfo.merchant_id } | _ => "" } @@ -298,9 +298,7 @@ let make = (~userRole, ~isAddMerchantEnabled=false) => { // TODO: When BE changes the response of this api re-evaluate the below conditions if featureFlagDetails.totp { let responseDict = res->getDictFromJsonObject - setAuthStatus( - LoggedIn(TotpAuth(TwoFaUtils.getTotpAuthInfo(responseDict->JSON.Encode.object))), - ) + setAuthStatus(LoggedIn(Auth(AuthUtils.getAuthInfo(responseDict->JSON.Encode.object)))) } else { setAuthStatus(LoggedIn(BasicAuth(res->BasicAuthUtils.getBasicAuthInfo))) }