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.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

View File

@ -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 ->