Skip to content

Commit

Permalink
chore: sso new enum addition (juspay#825)
Browse files Browse the repository at this point in the history
  • Loading branch information
Riddhiagrawal001 authored Jun 14, 2024
1 parent f206aa0 commit 6390602
Show file tree
Hide file tree
Showing 18 changed files with 75 additions and 56 deletions.
1 change: 1 addition & 0 deletions src/components/Navbar.res
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ let make = (
// </div>

| LoggedOut => React.null
| SSOPreLogin(_)
| PreLogin(_)
| CheckingAuthStatus =>
React.string("...")
Expand Down
10 changes: 9 additions & 1 deletion src/context/AuthInfoProvider.res
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ let make = (~children) => {
CommonAuthUtils.clearLocalStorage()
}
}
| TotpAuth(totpInfo) =>
| Auth(totpInfo) =>
if !(totpInfo.token->LogicUtils.isEmptyString) {
setAuth(_ => newAuthStatus)
TwoFaUtils.setTotpAuthResToStorage(totpInfo)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/context/UserPrefContext.res
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
| _ => ""
}
Expand Down
20 changes: 18 additions & 2 deletions src/entryPoints/AuthModule/AuthProviderTypes.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ type preLoginType = {
token_type: string,
email_token: option<string>,
}
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
27 changes: 27 additions & 0 deletions src/entryPoints/AuthModule/AuthUtils.res
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion src/entryPoints/AuthModule/AuthWrapper.res
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -107,6 +107,7 @@ let make = (~children) => {
<AuthHeaderWrapper>
<TwoFaAuthScreen setAuthStatus />
</AuthHeaderWrapper>
| SSOPreLogin(_) => <SSODecisionScreen />
| PreLogin(_) => <TwoFaDecisionScreen />
| LoggedIn(_token) => children
| CheckingAuthStatus => <PageLoaderWrapper.ScreenLoader />
Expand Down
1 change: 1 addition & 0 deletions src/entryPoints/AuthModule/BasicAuth/BasicAuthWrapper.res
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ let make = (~children) => {
<div className="font-inter-style">
{switch authStatus {
| LoggedOut => <BasicAuthScreen />
| SSOPreLogin(_)
| PreLogin(_)
| LoggedIn(_) => children
| CheckingAuthStatus => <Loader />
Expand Down
2 changes: 1 addition & 1 deletion src/entryPoints/AuthModule/Common/CommonAuthHooks.res
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/entryPoints/AuthModule/SSOAuth/SSODecisionScreen.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@react.component
let make = () => {
React.null
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let make = () => {
<TotpResetPassword flowType />
| ACCEPT_INVITATION_FROM_EMAIL => <TotpAcceptInviteScreen />
| VERIFY_EMAIL => <TotpEmailVerifyScreen />
| USER_INFO => <TotpUserInfoScreen />
| USER_INFO => <UserInfoScreen />
| ERROR => <CommonAuthError onClick=onClickErrorPageButton />
}
}
10 changes: 0 additions & 10 deletions src/entryPoints/AuthModule/TwoFaAuth/TwoFaTypes.res
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 2 additions & 29 deletions src/entryPoints/AuthModule/TwoFaAuth/TwoFaUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/AuthHooks.res
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Home/Home.res
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/screens/Home/HomeV2.res
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 2 additions & 4 deletions src/screens/SwitchMerchant/SwitchMerchant.res
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
| _ => ""
}
Expand Down Expand Up @@ -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)))
}
Expand Down

0 comments on commit 6390602

Please sign in to comment.