Skip to content

Commit

Permalink
Make mkPasswordResetKey pure
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaymankar committed Jun 17, 2024
1 parent 24587f3 commit e648388
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 24 deletions.
13 changes: 13 additions & 0 deletions libs/wire-api/src/Wire/API/User/Password.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Wire.API.User.Password
CompletePasswordReset (..),
PasswordResetIdentity (..),
PasswordResetKey (..),
mkPasswordResetKey,
PasswordResetCode (..),

-- * deprecated
Expand All @@ -33,9 +34,13 @@ where

import Cassandra qualified as C
import Control.Lens ((?~))
import Crypto.Hash
import Data.Aeson qualified as A
import Data.Aeson.Types (Parser)
import Data.ByteArray qualified as ByteArray
import Data.ByteString qualified as BS
import Data.ByteString.Conversion
import Data.Id
import Data.Misc (PlainTextPassword8)
import Data.OpenApi qualified as S
import Data.OpenApi.ParamSchema
Expand Down Expand Up @@ -175,6 +180,14 @@ newtype PasswordResetKey = PasswordResetKey
deriving stock (Eq, Show, Ord)
deriving newtype (ToSchema, FromByteString, ToByteString, A.FromJSON, A.ToJSON, Arbitrary)

mkPasswordResetKey :: UserId -> PasswordResetKey
mkPasswordResetKey userId =
PasswordResetKey
. encodeBase64Url
. BS.pack
. ByteArray.unpack
$ hashWith SHA256 (toByteString' userId)

instance ToParamSchema PasswordResetKey where
toParamSchema _ = toParamSchema (Proxy @Text)

Expand Down
1 change: 0 additions & 1 deletion libs/wire-subsystems/src/Wire/CodeStore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ data PRQueryData f = PRQueryData
}

data CodeStore m a where
MkPasswordResetKey :: UserId -> CodeStore m PasswordResetKey
GenerateEmailCode :: CodeStore m PasswordResetCode
GeneratePhoneCode :: CodeStore m PasswordResetCode
CodeSelect :: PasswordResetKey -> CodeStore m (Maybe (PRQueryData Maybe))
Expand Down
8 changes: 0 additions & 8 deletions libs/wire-subsystems/src/Wire/CodeStore/Cassandra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ module Wire.CodeStore.Cassandra
where

import Cassandra
import Data.ByteString.Conversion (toByteString')
import Data.Id
import Data.Text (pack)
import Data.Text.Ascii
import Data.Time.Clock
import Imports
import OpenSSL.BN (randIntegerZeroToNMinusOne)
import OpenSSL.EVP.Digest (digestBS, getDigestByName)
import OpenSSL.Random (randBytes)
import Polysemy
import Text.Printf
Expand All @@ -46,7 +44,6 @@ codeStoreToCassandra =
interpret $
embed @m
. \case
MkPasswordResetKey uid -> mkPwdResetKey uid
GenerateEmailCode -> genEmailCode
GeneratePhoneCode -> genPhoneCode
CodeSelect prk ->
Expand Down Expand Up @@ -82,11 +79,6 @@ genPhoneCode =
PasswordResetCode . unsafeFromText . pack . printf "%06d"
<$> liftIO (randIntegerZeroToNMinusOne 1000000)

mkPwdResetKey :: MonadIO m => UserId -> m PasswordResetKey
mkPwdResetKey u = do
d <- liftIO $ getDigestByName "SHA256" >>= maybe (error "SHA256 not found") pure
pure . PasswordResetKey . encodeBase64Url . digestBS d $ toByteString' u

interpretClientToIO ::
Member (Final IO) r =>
ClientState ->
Expand Down
7 changes: 2 additions & 5 deletions libs/wire-subsystems/src/Wire/MiniBackend.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ module Wire.MiniBackend
)
where

import Data.ByteString.Conversion
import Data.Default (Default (def))
import Data.Domain
import Data.Handle (Handle)
Expand Down Expand Up @@ -338,7 +337,7 @@ runNoFederationStackState localBackend teamMember cfg =
runAllErrorsUnsafe . interpretNoFederationStackState localBackend teamMember def cfg

interpretNoFederationStack ::
(HasCallStack, Members AllErrors r) =>
Members AllErrors r =>
MiniBackend ->
Maybe TeamMember ->
AllFeatureConfigs ->
Expand All @@ -349,7 +348,7 @@ interpretNoFederationStack localBackend teamMember galleyConfigs cfg =
snd <$$> interpretNoFederationStackState localBackend teamMember galleyConfigs cfg

interpretNoFederationStackState ::
(HasCallStack, Members AllErrors r) =>
Members AllErrors r =>
MiniBackend ->
Maybe TeamMember ->
AllFeatureConfigs ->
Expand Down Expand Up @@ -425,8 +424,6 @@ staticCodeStore ::
Member (State MiniBackend) r =>
InterpreterFor CodeStore r
staticCodeStore = interpret \case
MkPasswordResetKey uid ->
pure . PasswordResetKey . encodeBase64Url $ toByteString' uid
GenerateEmailCode ->
pure . PasswordResetCode . encodeBase64Url $ "email-code"
GeneratePhoneCode -> (error "deprecated")
Expand Down
4 changes: 2 additions & 2 deletions libs/wire-subsystems/src/Wire/PasswordStore/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ create ::
Either Email Phone ->
Sem r PasswordResetPair
create u target = do
key <- mkPasswordResetKey u
let key = mkPasswordResetKey u
now <- Now.get
code <- either (const generateEmailCode) (const generatePhoneCode) target
codeInsert
Expand All @@ -80,7 +80,7 @@ lookup ::
UserId ->
Sem r (Maybe PasswordResetCode)
lookup u = do
key <- mkPasswordResetKey u
let key = mkPasswordResetKey u
now <- Now.get
validate now =<< codeSelect key
where
Expand Down
16 changes: 8 additions & 8 deletions libs/wire-subsystems/src/Wire/UserSubsystem/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -537,16 +537,16 @@ checkNewIsDifferent uid pw = do
_ -> pure ()

mkPasswordResetKey' ::
(Member (Error UserSubsystemError) r, Member UserKeyStore r, Member CodeStore r) =>
(Member (Error UserSubsystemError) r, Member UserKeyStore r) =>
PasswordResetIdentity ->
Sem r PasswordResetKey
mkPasswordResetKey' ident = case ident of
PasswordResetIdentityKey k -> pure k
PasswordResetEmailIdentity e -> do
lookupKey (userEmailKey e)
>>= traverse mkPasswordResetKey
>>= maybe (throw UserSubsystemInvalidPasswordResetKey) pure
PasswordResetPhoneIdentity p ->
lookupKey (userPhoneKey p)
>>= traverse mkPasswordResetKey
>>= maybe (throw UserSubsystemInvalidPasswordResetKey) pure
mUserId <- lookupKey (userEmailKey e)
let mResetKey = mkPasswordResetKey <$> mUserId
maybe (throw UserSubsystemInvalidPasswordResetKey) pure mResetKey
PasswordResetPhoneIdentity p -> do
mUserId <- lookupKey (userPhoneKey p)
let mResetKey = mkPasswordResetKey <$> mUserId
maybe (throw UserSubsystemInvalidPasswordResetKey) pure mResetKey

0 comments on commit e648388

Please sign in to comment.