@@ -7,10 +7,10 @@ import BW (ApiService, CryptoService, Services)
77import BW as WB
88import BW.Logic (decodeCipher, decrypt, hashPassword, liftPromise)
99import BW.Logic as Logic
10−import BW.Types (CipherResponse, Email(..), Password(..), PreloginResponse, Urls, cipherTypeCard, cipherTypeIdentity, cipherTypeLogin, cipherTypeSecureNote)
10+import BW.Types (CipherResponse, Email(..), Password(..), Urls, cipherTypeCard, cipherTypeIdentity, cipherTypeLogin, cipherTypeSecureNote)
1111import Bridge as Bridge
1212import Control.El (class HasL, Al, L(..), l)
13−import Control.Monad.Error.Class (throwError)
13+import Control.Monad.Error.Class (catchError, throwError)
1414import Control.Monad.Reader (runReaderT)
1515import Data.Argonaut (class DecodeJson)
1616import Data.Array as Array
@@ -56,15 +56,15 @@ main = do
5656 runElmAff app $ flip runReaderT { app: app, crypto: services.crypto, storage: storage }
5757 $ case maybeKey of
5858 Just masterKey -> do
59− token <- getOrReset storage TokenKey
59+ token <- getOrReset TokenKey
6060 key <- Logic.makeDecryptionKey masterKey token.key
6161 liftEffect $ runElmAff app
6262 $ runReaderT act
63− { app: app
64− , storage: storage
65− , services: services
63+ { app
64+ , storage
65+ , services
6666 , crypto: services.crypto
67− , key: key
67+ , key
6868 }
6969 Nothing -> requestMasterPassword
7070 Elm.subscribe app \cmd -> case cmd of
@@ -73,23 +73,19 @@ main = do
7373 urls = baseUrl server
7474 runElmAff app do
7575 unauthedApi <- liftPromise $ services.getApi urls jnull
76− flip runReaderT
77− { api: unauthedApi
78− , crypto: services.crypto
79− , storage:
80− storage
81− , cryptoFunctions: services.cryptoFunctions
82− } do
83− { token, prelogin: (prelogin :: PreloginResponse) } <-
84− try
85− ( do
86− prelogin <- liftPromise $ unauthedApi.postPrelogin { email }
87− token <- (Logic.getLogInRequestToken prelogin (Email email) (Password password))
88− pure { token, prelogin }
89− )
90− >>= \et -> case et of
91− Left _ -> liftEffect $ Exc.throw "Could not log in. Please check your data and try again."
92− Right t -> pure t
76+ let
77+ env =
78+ { api: unauthedApi
79+ , crypto: services.crypto
80+ , storage
81+ , cryptoFunctions: services.cryptoFunctions
82+ }
83+ flip runReaderT env do
84+ prelogin <- liftPromise $ unauthedApi.postPrelogin { email }
85+ token <-
86+ catchError
87+ (Logic.getLogInRequestToken prelogin (Email email) (Password password))
88+ (const $ liftEffect $ Exc.throw "Could not log in. Please check your data and try again.")
9389 masterKey <- Logic.makePreloginKey prelogin (Email email) (Password password)
9490 api <- liftPromise $ services.getApi urls (nullify token)
9591 sync <- liftPromise $ api.getSync unit
@@ -102,11 +98,9 @@ main = do
10298 liftEffect $ Storage.store storage SyncKey sync
10399 liftEffect $ Storage.store storage TokenKey token
104100 liftEffect $ Elm.send app Bridge.LoginSuccessful
105− pure unit
106− pure unit
107101 Bridge.NeedCiphersList ->
108102 run do
109− sync <- getOrReset storage SyncKey
103+ sync <- getOrReset SyncKey
110104 ciphers <- traverse processCipher sync.ciphers
111105 let
112106 sortedCiphers = Array.sortWith (_.date >>> Down) ciphers
@@ -122,11 +116,11 @@ main = do
122116 $ do
123117 flip runReaderT { app: app, storage: storage, crypto: services.crypto, cryptoFunctions: services.cryptoFunctions }
124118 $ do
125− hash <- getOrReset storage MasterPasswordHashKey
119+ hash <- getOrReset MasterPasswordHashKey
126120 newHash <- hashPassword (Password masterPassword)
127121 if hash == newHash then do
128− sync <- getOrReset storage SyncKey
129− prelogin <- getOrReset storage PreloginResponseKey
122+ sync <- getOrReset SyncKey
123+ prelogin <- getOrReset PreloginResponseKey
130124 masterKey <- Logic.makePreloginKey prelogin (Email sync.profile.email) (Password masterPassword)
131125 liftEffect $ Ref.write (Just masterKey) masterKeyRef
132126 liftEffect $ Elm.send app Bridge.LoginSuccessful
@@ -141,7 +135,7 @@ main = do
141135 Nothing -> Elm.send app Bridge.NeedsLogin
142136 Bridge.RequestCipher id ->
143137 run do
144− sync <- getOrReset storage SyncKey
138+ sync <- getOrReset SyncKey
145139 case Array.find (\c -> c.id == id) sync.ciphers of
146140 Nothing -> pure unit
147141 Just cipherResponse -> do
@@ -151,7 +145,7 @@ main = do
151145 pure unit
152146 Bridge.NeedEmail ->
153147 run do
154− sync <- getOrReset storage SyncKey
148+ sync <- getOrReset SyncKey
155149 send $ Bridge.RecieveEmail sync.profile.email
156150 pure unit
157151 Bridge.Copy text ->
@@ -213,8 +207,12 @@ baseUrl server =
213207getOrReset ::
214208 forall k t r.
215209 HasL "app" Elm r =>
216− StorageKey k t => DecodeJson t => Storage -> k -> Al r t
217−getOrReset storage key = do
210+ HasL "storage" Storage r =>
211+ StorageKey k t =>
212+ DecodeJson t =>
213+ k -> Al r t
214+getOrReset key = do
215+ storage <- l (L :: L "storage")
218216 let
219217 handleError = do
220218 app <- l (L :: L "app")
@@ -236,18 +234,16 @@ getAuthedApi ::
236234 HasL "app" Elm r =>
237235 Al r ApiService
238236getAuthedApi = do
239− storage <- l (L :: L "storage")
240237 services <- l (L :: L "services")
241− token <- getOrReset storage TokenKey
242− url <- getOrReset storage UrlsKey
238+ token <- getOrReset TokenKey
239+ url <- getOrReset UrlsKey
243240 liftPromise $ services.getApi (baseUrl url) (nullify token)
244241
245242requestMasterPassword :: forall r. HasL "storage" Storage r => HasL "app" Elm r => Al r Unit
246243requestMasterPassword = do
247− storage <- l (L :: L "storage")
248244 app <- l (L :: L "app")
249− url <- getOrReset storage UrlsKey
250− sync <- getOrReset storage SyncKey
245+ url <- getOrReset UrlsKey
246+ sync <- getOrReset SyncKey
251247 liftEffect $ Elm.send app $ Bridge.NeedsMasterPassword
252248 $ Bridge.Sub_NeedsMasterPassword { server: url, login: sync.profile.email }
253249