@@ -2,21 +2,27 @@ module Main
5import Prelude
6import Prelude
7+import BW (CryptoService)
6import BW as WB
8import BW as WB
7−import BW.Logic (decrypt)
9+import BW.Logic (decrypt, liftPromise)
8import BW.Logic as Logic
10import BW.Logic as Logic
9−import BW.Types (Email(..), EncryptedString(..), Password(..), Urls)
11+import BW.Types (Email(..), EncryptedString(..), Password(..), Urls, CipherResponse)
10import Bridge as Bridge
12import Bridge as Bridge
13+import Control.El (class HasL, Al, L(..), l)
11import Control.Monad.Reader (runReaderT)
14import Control.Monad.Reader (runReaderT)
12import Control.Promise (Promise)
15import Control.Promise (Promise)
13import Control.Promise as Promise
16import Control.Promise as Promise
14import Data.Argonaut (class EncodeJson, encodeJson)
17import Data.Argonaut (class EncodeJson, encodeJson)
15import Data.Argonaut as Json
18import Data.Argonaut as Json
19+import Data.Array as A
16import Data.Either (Either(..))
20import Data.Either (Either(..))
17import Data.Maybe (Maybe(..))
21import Data.Maybe (Maybe(..))
18import Data.Nullable (Nullable, null, toNullable)
22import Data.Nullable (Nullable, null, toNullable)
19import Data.Nullable as Nullable
23import Data.Nullable as Nullable
24+import Data.SymmetricCryptoKey (SymmetricCryptoKey)
25+import Data.Timestamp as Timestamp
20import Data.Traversable (traverse)
26import Data.Traversable (traverse)
21import Effect (Effect)
27import Effect (Effect)
22import Effect.Aff (Aff, runAff_, try)
28import Effect.Aff (Aff, runAff_, try)
@@ -41,7 +47,7 @@ main = do
41 urls = baseUrl server
47 urls = baseUrl server
42 runElmAff app do
48 runElmAff app do
43 unauthedApi <- liftPromise $ services.getApi urls null
49 unauthedApi <- liftPromise $ services.getApi urls null
44− { masterKey, token } <-
50+ { masterKey, token, key } <-
45 flip runReaderT { api: unauthedApi, crypto: services.crypto } do
51 flip runReaderT { api: unauthedApi, crypto: services.crypto } do
47 try (Logic.getLogInRequestToken (Email email) (Password password))
53 try (Logic.getLogInRequestToken (Email email) (Password password))
@@ -49,21 +55,28 @@ main = do
49 Left _ -> liftEffect $ Exc.throw "Could not log in. Please check your data and try again."
55 Left _ -> liftEffect $ Exc.throw "Could not log in. Please check your data and try again."
50 Right t -> pure t
56 Right t -> pure t
51 masterKey <- Logic.makePreloginKey (Email email) (Password password)
57 masterKey <- Logic.makePreloginKey (Email email) (Password password)
58+ key <- Logic.makeDecryptionKey masterKey token.key
52− pure { token, masterKey }
59+ pure { token, masterKey, key }
53 api <- liftPromise $ services.getApi urls (nullify token)
60 api <- liftPromise $ services.getApi urls (nullify token)
55− flip runReaderT { api: api, crypto: services.crypto } do
62+ flip runReaderT { api: api, crypto: services.crypto, key: key } do
56 sync <- liftPromise $ api.getSync unit
63 sync <- liftPromise $ api.getSync unit
57− key <- Logic.makeDecryptionKey masterKey sync.profile.key
64+ processedCiphers <- traverse processCipher sync.ciphers
58− log $ show key.keyB64
65+ liftEffect $ Elm.send app $ LoadCiphers $ Bridge.Sub_LoadCiphers_List (A.take 3 processedCiphers)
59− ciphersNames <- traverse (decrypt key) $ map _.name sync.ciphers
60− liftEffect $ Elm.send app $ Bridge.Hello $ Bridge.Sub_Hello { first: "hello", second: show ciphersNames }
63 Bridge.Init -> Elm.send app (Bridge.NeedsLogin)
68 Bridge.Init -> Elm.send app (Bridge.NeedsLogin)
65−liftPromise ∷ forall m a. MonadAff m ⇒ Promise a → m a
70+processCipher ::
66−liftPromise = Aff.liftAff <<< Promise.toAff
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
78+ $ Bridge.Sub_LoadCiphers
79+ { name: name, date: Timestamp.toLocalDateTimeString cipher.revisionDate }
68runElmAff :: FFI.Elm -> Aff Unit -> Effect Unit
81runElmAff :: FFI.Elm -> Aff Unit -> Effect Unit
69runElmAff app =
82runElmAff app =