From eb047e0bff4d36602d2da91d3d7f06d55ced3a89 Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Fri, 24 Mar 2023 15:12:59 +0100 Subject: [PATCH] Utilize /whoami for better token evaluation --- src/Internal/Api/VaultUpdate.elm | 30 +++++++++++++++++++++-- src/Internal/Tools/LoginValues.elm | 39 ++++++++++++++++++++++++------ 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/Internal/Api/VaultUpdate.elm b/src/Internal/Api/VaultUpdate.elm index 0b3e2f3..e360b89 100644 --- a/src/Internal/Api/VaultUpdate.elm +++ b/src/Internal/Api/VaultUpdate.elm @@ -16,6 +16,7 @@ import Internal.Api.SendStateKey.Main as SendStateKey import Internal.Api.Sync.Main as Sync import Internal.Api.Versions.Main as Versions import Internal.Api.Versions.V1.Versions as V +import Internal.Api.WhoAmI.Main as WhoAmI import Internal.Tools.Context as Context exposing (VB, VBA, VBAT) import Internal.Tools.Exceptions as X import Internal.Tools.LoginValues exposing (AccessToken(..)) @@ -41,6 +42,7 @@ type VaultUpdate -- Updates as a result of getting data early | UpdateAccessToken String | UpdateVersions V.Versions + | UpdateRawAccessToken String WhoAmI.WhoAmIOutput type alias FutureTask = @@ -82,18 +84,42 @@ accessToken ctoken = |> Task.fail |> always - AccessToken t -> + RawAccessToken t -> { contextChange = Context.setAccessToken { accessToken = t, loginParts = Nothing } , messages = [] } |> Chain.TaskChainPiece |> Task.succeed |> always + |> Chain.andThen + (toChain + (\output -> + Chain.TaskChainPiece + { contextChange = identity + , messages = [ UpdateRawAccessToken t output ] + } + ) + WhoAmI.whoAmI + () + ) + + DetailedAccessToken data -> + { contextChange = Context.setAccessToken { accessToken = data.accessToken, loginParts = Nothing } + , messages = [] + } + |> Chain.TaskChainPiece + |> Task.succeed + |> always UsernameAndPassword { username, password, token, deviceId, initialDeviceDisplayName } -> case token of Just t -> - accessToken (AccessToken t) + { contextChange = Context.setAccessToken { accessToken = t, loginParts = Nothing } + , messages = [] + } + |> Chain.TaskChainPiece + |> Task.succeed + |> always Nothing -> loginWithUsernameAndPassword diff --git a/src/Internal/Tools/LoginValues.elm b/src/Internal/Tools/LoginValues.elm index 9624919..92db395 100644 --- a/src/Internal/Tools/LoginValues.elm +++ b/src/Internal/Tools/LoginValues.elm @@ -3,7 +3,12 @@ module Internal.Tools.LoginValues exposing (..) type AccessToken = NoAccess - | AccessToken String + | RawAccessToken String + | DetailedAccessToken + { accessToken : String + , userId : String + , deviceId : String + } | UsernameAndPassword { deviceId : Maybe String , initialDeviceDisplayName : Maybe String @@ -20,7 +25,7 @@ defaultAccessToken = fromAccessToken : String -> AccessToken fromAccessToken = - AccessToken + RawAccessToken fromUsernameAndPassword : String -> String -> AccessToken @@ -40,9 +45,12 @@ getToken t = NoAccess -> Nothing - AccessToken token -> + RawAccessToken token -> Just token + DetailedAccessToken { accessToken } -> + Just accessToken + UsernameAndPassword { token } -> token @@ -51,10 +59,13 @@ addToken : String -> AccessToken -> AccessToken addToken s t = case t of NoAccess -> - AccessToken s + RawAccessToken s - AccessToken _ -> - AccessToken s + RawAccessToken _ -> + RawAccessToken s + + DetailedAccessToken _ -> + RawAccessToken s UsernameAndPassword data -> UsernameAndPassword @@ -67,7 +78,7 @@ addUsernameAndPassword { username, password } t = NoAccess -> fromUsernameAndPassword username password - AccessToken a -> + RawAccessToken a -> UsernameAndPassword { username = username , password = password @@ -76,6 +87,15 @@ addUsernameAndPassword { username, password } t = , initialDeviceDisplayName = Nothing } + DetailedAccessToken { accessToken, deviceId } -> + UsernameAndPassword + { username = username + , password = password + , token = Just accessToken + , deviceId = Just deviceId + , initialDeviceDisplayName = Nothing + } + UsernameAndPassword data -> UsernameAndPassword { data | username = username, password = password } @@ -87,7 +107,10 @@ removeToken t = NoAccess -> NoAccess - AccessToken _ -> + RawAccessToken _ -> + NoAccess + + DetailedAccessToken _ -> NoAccess UsernameAndPassword data ->