@@ -15,6 +15,7 @@ import Data.JNullable as JNullable
1515import Data.JOpt (JOpt(..), fromJOpt)
1616import Data.Maybe (Maybe(..), fromMaybe, maybe)
1717import Data.Newtype (class Newtype, unwrap, wrap)
18+import Data.Number.Format as Number
1819import Data.String as String
1920import Data.SymmetricCryptoKey (SymmetricCryptoKey)
2021import Data.SymmetricCryptoKey as SymmetricCryptoKey
@@ -26,10 +27,14 @@ import Effect.Aff.Class (class MonadAff, liftAff)
2627import Effect.Class (liftEffect)
2728import Effect.Exception (error)
2829import Effect.Exception as Exc
30+import Effect.Random (random)
31+import Localstorage as Storage
2932import Run (Run)
3033import Run.Reader (Reader, askAt)
34+import Storage (DeviceIDKey(..))
3135import Type.Prelude (Proxy(..))
3236import Untagged.Union (type (|+|))
37+import Web.Storage.Storage (Storage)
3338
3439-- | Creates the master key
3540makePreloginKey
@@ -96,6 +101,7 @@ getLogInRequestToken
96101 , crypto :: Reader CryptoService
97102 , effect :: Effect
98103 , aff :: Aff
104+ , storage :: Reader Storage
99105 | r
100106 )
101107 (IdentityCaptchaResponse |+| IdentityTwoFactorResponse |+| IdentityTokenResponse)
@@ -106,6 +112,8 @@ getLogInRequestToken
106112 , captchaResponse
107113 , secondFactor: Bridge.Cmd_Login_secondFactor_Maybe secondFactor
108114 } = do
115+ key <- makePreloginKey prelogin email password
116+ crypto <- askAt (Proxy :: _ "crypto")
109117 let
110118 twoFactor =
111119 maybe
@@ -117,7 +125,11 @@ getLogInRequestToken
117125 x { provider = bridgeToSecondFactorType provider }
118126 )
119127 secondFactor
120− StringHash hashedPassword <- bwPasswordStringHash prelogin email password
128+ _localHashedPassword <-
129+ liftPromise
130+ $ runFn3 crypto.hashPassword password key (nullify hashPurposeLocalAuthorization)
131+ StringHash hashedPassword <- liftPromise $ runFn3 crypto.hashPassword password key jnull
132+ deviceId <- getDeviceID
121133 api :: ApiService <- askAt (Proxy :: _ "api")
122134 liftPromise
123135 $ api.postIdentityToken
@@ -128,7 +140,7 @@ getLogInRequestToken
128140 , device:
129141 { type: deviceTypeUnknownBrowser
130142 , name: "Ubuntu Touch"
131− , identifier: "42"
143+ , identifier: deviceId
132144 , pushToken: jnull
133145 }
134146 }
@@ -480,3 +492,21 @@ encryptNullable
480492encryptNullable x = case unwrap x of
481493 Nothing -> pure jnull
482494 Just y -> nullify <$> encrypt y
495+
496+getDeviceID ::
497+ forall r.
498+ Run
499+ ( effect :: Effect
500+ , storage :: Reader Storage
501+ | r
502+ )
503+ String
504+getDeviceID = do
505+ storage <- askAt (Proxy :: _ "storage")
506+ maybeId <- liftEffect $ Storage.get storage DeviceIDKey
507+ case maybeId of
508+ Just id -> pure id
509+ Nothing -> do
510+ id <- map Number.toString $ liftEffect $ random
511+ liftEffect $ Storage.store storage DeviceIDKey id
512+ pure id