@@ -2,21 +2,27 @@ module Main
22 ( main
33 ) where
44
5+import Bridge
56import Prelude
7+import BW (CryptoService)
68import BW as WB
7−import BW.Logic (decrypt)
9+import BW.Logic (decrypt, liftPromise)
810import BW.Logic as Logic
9−import BW.Types (Email(..), EncryptedString(..), Password(..), Urls)
11+import BW.Types (Email(..), EncryptedString(..), Password(..), Urls, CipherResponse)
1012import Bridge as Bridge
13+import Control.El (class HasL, Al, L(..), l)
1114import Control.Monad.Reader (runReaderT)
1215import Control.Promise (Promise)
1316import Control.Promise as Promise
1417import Data.Argonaut (class EncodeJson, encodeJson)
1518import Data.Argonaut as Json
19+import Data.Array as A
1620import Data.Either (Either(..))
1721import Data.Maybe (Maybe(..))
1822import Data.Nullable (Nullable, null, toNullable)
1923import Data.Nullable as Nullable
24+import Data.SymmetricCryptoKey (SymmetricCryptoKey)
25+import Data.Timestamp as Timestamp
2026import Data.Traversable (traverse)
2127import Effect (Effect)
2228import Effect.Aff (Aff, runAff_, try)
@@ -41,7 +47,7 @@ main = do
4147 urls = baseUrl server
4248 runElmAff app do
4349 unauthedApi <- liftPromise $ services.getApi urls null
44− { masterKey, token } <-
50+ { masterKey, token, key } <-
4551 flip runReaderT { api: unauthedApi, crypto: services.crypto } do
4652 token <-
4753 try (Logic.getLogInRequestToken (Email email) (Password password))
@@ -49,21 +55,28 @@ main = do
4955 Left _ -> liftEffect $ Exc.throw "Could not log in. Please check your data and try again."
5056 Right t -> pure t
5157 masterKey <- Logic.makePreloginKey (Email email) (Password password)
52− pure { token, masterKey }
58+ key <- Logic.makeDecryptionKey masterKey token.key
59+ pure { token, masterKey, key }
5360 api <- liftPromise $ services.getApi urls (nullify token)
5461 _ <-
55− flip runReaderT { api: api, crypto: services.crypto } do
62+ flip runReaderT { api: api, crypto: services.crypto, key: key } do
5663 sync <- liftPromise $ api.getSync unit
57− key <- Logic.makeDecryptionKey masterKey sync.profile.key
58− log $ show key.keyB64
59− ciphersNames <- traverse (decrypt key) $ map _.name sync.ciphers
60− liftEffect $ Elm.send app $ Bridge.Hello $ Bridge.Sub_Hello { first: "hello", second: show ciphersNames }
64+ processedCiphers <- traverse processCipher sync.ciphers
65+ liftEffect $ Elm.send app $ LoadCiphers $ Bridge.Sub_LoadCiphers_List (A.take 3 processedCiphers)
6166 pure unit
6267 pure unit
6368 Bridge.Init -> Elm.send app (Bridge.NeedsLogin)
6469
65−liftPromise ∷ forall m a. MonadAff m ⇒ Promise a → m a
66−liftPromise = Aff.liftAff <<< Promise.toAff
70+processCipher ::
71+ forall r.
72+ HasL "crypto" CryptoService r =>
73+ HasL "key" SymmetricCryptoKey r =>
74+ CipherResponse -> Al r Bridge.Sub_LoadCiphers
75+processCipher cipher = do
76+ name <- decrypt cipher.name
77+ pure
78+ $ Bridge.Sub_LoadCiphers
79+ { name: name, date: Timestamp.toLocalDateTimeString cipher.revisionDate }
6780
6881runElmAff :: FFI.Elm -> Aff Unit -> Effect Unit
6982runElmAff app =