module Hasql.Core.UnauthenticatedSession where
import Hasql.Prelude
import Hasql.Core.Model
import qualified Hasql.Core.Request as A
newtype Session result =
Session (F A.Request result)
deriving (Functor, Applicative, Monad)
request :: A.Request result -> Session result
request = Session . liftF
startUp :: ByteString -> Maybe ByteString -> [(ByteString, ByteString)] -> Session AuthenticationResult
startUp username databaseMaybe runtimeParameters =
request (A.startUp username databaseMaybe runtimeParameters)
clearTextPassword :: ByteString -> Session AuthenticationResult
clearTextPassword password =
request (A.clearTextPassword password)
md5Password :: ByteString -> ByteString -> ByteString -> Session AuthenticationResult
md5Password username password salt =
request (A.md5Password username password salt)
handshake :: ByteString -> ByteString -> Maybe ByteString -> [(ByteString, ByteString)] -> Session (Either Text Bool)
handshake username password databaseMaybe runtimeParameters =
startUp username databaseMaybe runtimeParameters >>= handleFirstAuthenticationResult
where
handleFirstAuthenticationResult =
\case
OkAuthenticationResult idt -> return (Right idt)
NeedClearTextPasswordAuthenticationResult -> clearTextPassword password >>= handleSecondAuthenticationResult
NeedMD5PasswordAuthenticationResult salt -> md5Password username password salt >>= handleSecondAuthenticationResult
handleSecondAuthenticationResult =
\case
OkAuthenticationResult idt -> return (Right idt)
_ -> return (Left "Can't authenticate")