@@ -11,13 +11,14 @@ import Data.EncString as EncString
1111import Data.Function.Uncurried (runFn2, runFn3, runFn4)
1212import Data.JNullable (JNullable, fromJNullable, jnull, nullify)
1313import Data.JNullable as JNullable
14−import Data.JOpt (fromJOpt)
14+import Data.JOpt (JOpt(..), fromJOpt)
1515import Data.Maybe (Maybe(..), fromMaybe)
16−import Data.Newtype (class Newtype, wrap)
16+import Data.Newtype (class Newtype, unwrap, wrap)
1717import Data.String as String
1818import Data.SymmetricCryptoKey (SymmetricCryptoKey)
1919import Data.SymmetricCryptoKey as SymmetricCryptoKey
2020import Data.Traversable (traverse)
21+import Data.Undefined.NoProblem (opt, undefined)
2122import Effect (Effect)
2223import Effect.Aff (Aff)
2324import Effect.Aff.Class (class MonadAff, liftAff)
@@ -147,9 +148,159 @@ decrypt input = do
147148 key <- askAt (Proxy :: _ "key")
148149 liftPromise $ runFn2 crypto.decryptToUtf8 (EncString.fromString input) key
149150
151+encrypt ::
152+ forall r.
153+ String ->
154+ Run
155+ ( key :: Reader SymmetricCryptoKey
156+ , crypto :: Reader CryptoService
157+ , aff :: Aff
158+ , effect :: Effect
159+ | r
160+ )
161+ EncryptedString
162+encrypt input = do
163+ crypto <- askAt (Proxy :: _ "crypto")
164+ key <- askAt (Proxy :: _ "key")
165+ map EncString.toString $ liftPromise $ runFn2 crypto.encrypt input key
166+
150167liftPromise ∷ forall m a. MonadAff m ⇒ Promise a → m a
151168liftPromise = liftAff <<< Promise.toAff
152169
170+encodeCipher ::
171+ forall r.
172+ Bridge.FullCipher ->
173+ Run
174+ ( key :: Reader SymmetricCryptoKey
175+ , crypto :: Reader CryptoService
176+ , aff :: Aff
177+ , effect :: Effect
178+ | r
179+ )
180+ CipherResponse
181+encodeCipher (Bridge.FullCipher { name, cipher, id, favorite, reprompt }) = do
182+ let
183+ f :: CipherResponse -> Run _ CipherResponse
184+ f x = case cipher of
185+ Bridge.CardCipher (Bridge.Cipher_CardCipher c) -> do
186+ cardholderName <- encryptNullable c.cardholderName
187+ brand <- encryptNullable c.brand
188+ number <- encryptNullable c.number
189+ expMonth <- encryptNullable c.expMonth
190+ expYear <- encryptNullable c.expYear
191+ code <- encryptNullable c.code
192+ pure
193+ x
194+ { card =
195+ nullify
196+ { cardholderName
197+ , brand
198+ , number
199+ , expMonth
200+ , expYear
201+ , code
202+ }
203+ , type = cipherTypeCard
204+ }
205+ Bridge.IdentityCipher (Bridge.Cipher_IdentityCipher identity) -> do
206+ address1 <- encryptNullable identity.address1
207+ address2 <- encryptNullable identity.address2
208+ address3 <- encryptNullable identity.address3
209+ city <- encryptNullable identity.city
210+ company <- encryptNullable identity.company
211+ country <- encryptNullable identity.country
212+ email <- encryptNullable identity.email
213+ firstName <- encryptNullable identity.firstName
214+ lastName <- encryptNullable identity.lastName
215+ licenseNumber <- encryptNullable identity.licenseNumber
216+ middleName <- encryptNullable identity.middleName
217+ passportNumber <- encryptNullable identity.passportNumber
218+ phone <- encryptNullable identity.phone
219+ postalCode <- encryptNullable identity.postalCode
220+ ssn <- encryptNullable identity.ssn
221+ state <- encryptNullable identity.state
222+ title <- encryptNullable identity.title
223+ username <- encryptNullable identity.username
224+ pure
225+ x
226+ { identity =
227+ nullify
228+ { address1
229+ , address2
230+ , address3
231+ , city
232+ , company
233+ , country
234+ , email
235+ , firstName
236+ , lastName
237+ , licenseNumber
238+ , middleName
239+ , passportNumber
240+ , phone
241+ , postalCode
242+ , ssn
243+ , state
244+ , title
245+ , username
246+ }
247+ , type = cipherTypeIdentity
248+ }
249+ Bridge.LoginCipher (Bridge.Cipher_LoginCipher login) -> do
250+ password <- encryptNullable login.password
251+ uris <-
252+ traverse
253+ ( \uri -> do
254+ uriEnc <- encrypt uri
255+ pure { uri: uriEnc, match: jnull }
256+ )
257+ (unwrap login.uris)
258+ username <- encryptNullable login.username
259+ pure
260+ x
261+ { login =
262+ nullify
263+ { password
264+ , uris: JOpt $ opt uris
265+ , username
266+ , passwordRevisionDate: jnull
267+ , totp: jnull
268+ , autofillOnPageLoad: JOpt undefined
269+ }
270+ , type = cipherTypeLogin
271+ }
272+ Bridge.NoteCipher note -> do
273+ encNote <- encrypt note
274+ pure
275+ x
276+ { notes = nullify encNote
277+ , type = cipherTypeSecureNote
278+ }
279+ encName <- encrypt name
280+ f
281+ { id
282+ , organizationId: jnull
283+ , folderId: jnull
284+ , type: 0
285+ , name: encName
286+ , notes: jnull
287+ , fields: jnull
288+ , login: jnull
289+ , card: jnull
290+ , identity: jnull
291+ , secureNote: jnull
292+ , favorite: favorite
293+ , edit: jnull
294+ , viewPassword: jnull
295+ , organizationUseTotp: jnull
296+ , revisionDate: jnull
297+ , attachments: jnull
298+ , passwordHistory: jnull
299+ , collectionIds: jnull
300+ , deletedDate: jnull
301+ , reprompt
302+ }
303+
153304decodeCipher ::
154305 forall r.
155306 CipherResponse ->
@@ -160,7 +311,7 @@ decodeCipher ::
160311 , effect :: Effect
161312 | r
162313 )
163− Bridge.Sub_LoadCipher
314+ Bridge.FullCipher
164315decodeCipher cipher = do
165316 name <- decrypt cipher.name
166317 cipherType <- case cipher.type of
@@ -242,9 +393,12 @@ decodeCipher cipher = do
242393 }
243394 n -> liftEffect $ throwError $ error $ "Unsupported cipher type: " <> show n
244395 pure
245− $ Bridge.Sub_LoadCipher
246− { cipher: Bridge.FullCipher { cipher: cipherType, name }
396+ $ wrap
397+ { cipher: cipherType
398+ , name
247399 , id: cipher.id
400+ , favorite: cipher.favorite
401+ , reprompt: cipher.reprompt
248402 }
249403
250404decryptNullable ::
@@ -260,3 +414,19 @@ decryptNullable ::
260414 )
261415 x
262416decryptNullable x = wrap <<< fromJNullable Nothing <<< map Just <$> (traverse decrypt x)
417+
418+encryptNullable ::
419+ forall x r.
420+ Newtype x (Maybe String) =>
421+ x ->
422+ Run
423+ ( key :: Reader SymmetricCryptoKey
424+ , crypto :: Reader CryptoService
425+ , aff :: Aff
426+ , effect :: Effect
427+ | r
428+ )
429+ (JNullable EncryptedString)
430+encryptNullable x = case unwrap x of
431+ Nothing -> pure jnull
432+ Just y -> nullify <$> encrypt y