Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't hard-code device ID #10

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions backend/BW/Logic.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ::
Expand Down Expand Up @@ -89,6 +94,7 @@ getLogInRequestToken ::
, crypto :: Reader CryptoService
, effect :: Effect
, aff :: Aff
, storage :: Reader Storage
| r
)
(IdentityCaptchaResponse |+| IdentityTokenResponse)
Expand All @@ -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
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
8 changes: 7 additions & 1 deletion backend/Storage.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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"
4 changes: 2 additions & 2 deletions packages.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 2 additions & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ to generate this file without the comments in this block.
, "maybe"
, "newtype"
, "nullable"
, "numbers"
, "orders"
, "prelude"
, "random"
, "refs"
, "run"
, "strings"
Expand Down