Utilize /whoami for better token evaluation

pull/1/head
Bram van den Heuvel 2023-03-24 15:12:59 +01:00
parent c45ecd2da3
commit eb047e0bff
2 changed files with 59 additions and 10 deletions

View File

@ -16,6 +16,7 @@ import Internal.Api.SendStateKey.Main as SendStateKey
import Internal.Api.Sync.Main as Sync import Internal.Api.Sync.Main as Sync
import Internal.Api.Versions.Main as Versions import Internal.Api.Versions.Main as Versions
import Internal.Api.Versions.V1.Versions as V 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.Context as Context exposing (VB, VBA, VBAT)
import Internal.Tools.Exceptions as X import Internal.Tools.Exceptions as X
import Internal.Tools.LoginValues exposing (AccessToken(..)) import Internal.Tools.LoginValues exposing (AccessToken(..))
@ -41,6 +42,7 @@ type VaultUpdate
-- Updates as a result of getting data early -- Updates as a result of getting data early
| UpdateAccessToken String | UpdateAccessToken String
| UpdateVersions V.Versions | UpdateVersions V.Versions
| UpdateRawAccessToken String WhoAmI.WhoAmIOutput
type alias FutureTask = type alias FutureTask =
@ -82,18 +84,42 @@ accessToken ctoken =
|> Task.fail |> Task.fail
|> always |> always
AccessToken t -> RawAccessToken t ->
{ contextChange = Context.setAccessToken { accessToken = t, loginParts = Nothing } { contextChange = Context.setAccessToken { accessToken = t, loginParts = Nothing }
, messages = [] , messages = []
} }
|> Chain.TaskChainPiece |> Chain.TaskChainPiece
|> Task.succeed |> Task.succeed
|> always |> 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 } -> UsernameAndPassword { username, password, token, deviceId, initialDeviceDisplayName } ->
case token of case token of
Just t -> Just t ->
accessToken (AccessToken t) { contextChange = Context.setAccessToken { accessToken = t, loginParts = Nothing }
, messages = []
}
|> Chain.TaskChainPiece
|> Task.succeed
|> always
Nothing -> Nothing ->
loginWithUsernameAndPassword loginWithUsernameAndPassword

View File

@ -3,7 +3,12 @@ module Internal.Tools.LoginValues exposing (..)
type AccessToken type AccessToken
= NoAccess = NoAccess
| AccessToken String | RawAccessToken String
| DetailedAccessToken
{ accessToken : String
, userId : String
, deviceId : String
}
| UsernameAndPassword | UsernameAndPassword
{ deviceId : Maybe String { deviceId : Maybe String
, initialDeviceDisplayName : Maybe String , initialDeviceDisplayName : Maybe String
@ -20,7 +25,7 @@ defaultAccessToken =
fromAccessToken : String -> AccessToken fromAccessToken : String -> AccessToken
fromAccessToken = fromAccessToken =
AccessToken RawAccessToken
fromUsernameAndPassword : String -> String -> AccessToken fromUsernameAndPassword : String -> String -> AccessToken
@ -40,9 +45,12 @@ getToken t =
NoAccess -> NoAccess ->
Nothing Nothing
AccessToken token -> RawAccessToken token ->
Just token Just token
DetailedAccessToken { accessToken } ->
Just accessToken
UsernameAndPassword { token } -> UsernameAndPassword { token } ->
token token
@ -51,10 +59,13 @@ addToken : String -> AccessToken -> AccessToken
addToken s t = addToken s t =
case t of case t of
NoAccess -> NoAccess ->
AccessToken s RawAccessToken s
AccessToken _ -> RawAccessToken _ ->
AccessToken s RawAccessToken s
DetailedAccessToken _ ->
RawAccessToken s
UsernameAndPassword data -> UsernameAndPassword data ->
UsernameAndPassword UsernameAndPassword
@ -67,7 +78,7 @@ addUsernameAndPassword { username, password } t =
NoAccess -> NoAccess ->
fromUsernameAndPassword username password fromUsernameAndPassword username password
AccessToken a -> RawAccessToken a ->
UsernameAndPassword UsernameAndPassword
{ username = username { username = username
, password = password , password = password
@ -76,6 +87,15 @@ addUsernameAndPassword { username, password } t =
, initialDeviceDisplayName = Nothing , initialDeviceDisplayName = Nothing
} }
DetailedAccessToken { accessToken, deviceId } ->
UsernameAndPassword
{ username = username
, password = password
, token = Just accessToken
, deviceId = Just deviceId
, initialDeviceDisplayName = Nothing
}
UsernameAndPassword data -> UsernameAndPassword data ->
UsernameAndPassword UsernameAndPassword
{ data | username = username, password = password } { data | username = username, password = password }
@ -87,7 +107,10 @@ removeToken t =
NoAccess -> NoAccess ->
NoAccess NoAccess
AccessToken _ -> RawAccessToken _ ->
NoAccess
DetailedAccessToken _ ->
NoAccess NoAccess
UsernameAndPassword data -> UsernameAndPassword data ->