From 7869c1cf3b0f528164fb004aeed31e7dbd729324 Mon Sep 17 00:00:00 2001 From: iko Date: Thu, 18 Aug 2022 13:28:14 +0300 Subject: [PATCH] Don't hard-code device ID --- backend/BW/Logic.purs | 29 +++++++++++++++++++++++++++-- backend/Storage.purs | 8 +++++++- packages.dhall | 4 ++-- spago.dhall | 2 ++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/backend/BW/Logic.purs b/backend/BW/Logic.purs index 497ede3..20b1da0 100644 --- a/backend/BW/Logic.purs +++ b/backend/BW/Logic.purs @@ -14,6 +14,7 @@ import Data.JNullable as JNullable import Data.JOpt (JOpt(..), fromJOpt) import Data.Maybe (Maybe(..), fromMaybe) import Data.Newtype (class Newtype, unwrap, wrap) +import Data.Number.Format as Number import Data.String as String import Data.SymmetricCryptoKey (SymmetricCryptoKey) import Data.SymmetricCryptoKey as SymmetricCryptoKey @@ -25,10 +26,14 @@ import Effect.Aff.Class (class MonadAff, liftAff) import Effect.Class (liftEffect) import Effect.Exception (error) import Effect.Exception as Exc +import Effect.Random (random) +import Localstorage as Storage import Run (Run) import Run.Reader (Reader, askAt) +import Storage (DeviceIDKey(..)) import Type.Prelude (Proxy(..)) import Untagged.Union (type (|+|)) +import Web.Storage.Storage (Storage) -- | Creates the master key makePreloginKey :: @@ -89,6 +94,7 @@ getLogInRequestToken :: , crypto :: Reader CryptoService , effect :: Effect , aff :: Aff + , storage :: Reader Storage | r ) (IdentityCaptchaResponse |+| IdentityTokenResponse) @@ -100,6 +106,7 @@ getLogInRequestToken prelogin email password captchaResponse = do liftPromise $ runFn3 crypto.hashPassword password key (nullify hashPurposeLocalAuthorization) StringHash hashedPassword <- liftPromise $ runFn3 crypto.hashPassword password key jnull + deviceId <- getDeviceID liftPromise $ api.postIdentityToken { email: email @@ -112,8 +119,8 @@ getLogInRequestToken prelogin email password captchaResponse = do } , device: { type: deviceTypeUnknownBrowser - , name: "Temporary device name" - , identifier: "42" + , name: "Ubuntu Touch" + , identifier: deviceId , pushToken: jnull } } @@ -430,3 +437,21 @@ encryptNullable :: encryptNullable x = case unwrap x of Nothing -> pure jnull Just y -> nullify <$> encrypt y + +getDeviceID :: + forall r. + Run + ( effect :: Effect + , storage :: Reader Storage + | r + ) + String +getDeviceID = do + storage <- askAt (Proxy :: _ "storage") + maybeId <- liftEffect $ Storage.get storage DeviceIDKey + case maybeId of + Just id -> pure id + Nothing -> do + id <- map Number.toString $ liftEffect $ random + liftEffect $ Storage.store storage DeviceIDKey id + pure id diff --git a/backend/Storage.purs b/backend/Storage.purs index 6c5f131..a0ca4bf 100644 --- a/backend/Storage.purs +++ b/backend/Storage.purs @@ -2,7 +2,7 @@ module Storage where import Prelude -import BW (Hash(..)) +import BW (Hash) import BW.Types (IdentityTokenResponse, SyncResponse, PreloginResponse) import Localstorage (class StorageKey) @@ -35,3 +35,9 @@ data MasterPasswordHashKey instance StorageKey MasterPasswordHashKey Hash where storageKey MasterPasswordHashKey = "master-password-hash" + +data DeviceIDKey + = DeviceIDKey + +instance StorageKey DeviceIDKey String where + storageKey DeviceIDKey = "device-uuid" diff --git a/packages.dhall b/packages.dhall index b386eae..4b0b7da 100644 --- a/packages.dhall +++ b/packages.dhall @@ -99,8 +99,8 @@ in upstream ------------------------------- -} let upstream = - https://github.com/purescript/package-sets/releases/download/psc-0.15.2-20220706/packages.dhall - sha256:7a24ebdbacb2bfa27b2fc6ce3da96f048093d64e54369965a2a7b5d9892b6031 + https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20220808/packages.dhall + sha256:60eee64b04ca0013fae3e02a69fc3b176105c6baa2f31865c67cd5f881a412fd in upstream with argonaut-aeson-generic = diff --git a/spago.dhall b/spago.dhall index 880e93f..926d7d4 100644 --- a/spago.dhall +++ b/spago.dhall @@ -32,8 +32,10 @@ to generate this file without the comments in this block. , "maybe" , "newtype" , "nullable" + , "numbers" , "orders" , "prelude" + , "random" , "refs" , "run" , "strings"