module Network.Xmpp.Sasl.Mechanisms.Plain
( plain
) where
import Control.Monad.Error
import Control.Monad.State.Strict
import qualified Data.ByteString as BS
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Network.Xmpp.Sasl.Common
import Network.Xmpp.Sasl.Types
import Network.Xmpp.Types
xmppPlain :: Text.Text
-> Maybe Text.Text
-> Text.Text
-> ErrorT AuthFailure (StateT StreamState IO) ()
xmppPlain authcid' authzid' password = do
(ac, az, pw) <- prepCredentials authcid' authzid' password
_ <- saslInit "PLAIN" ( Just $ plainMessage ac az pw)
_ <- pullSuccess
return ()
where
plainMessage :: Text.Text
-> Maybe Text.Text
-> Text.Text
-> BS.ByteString
plainMessage authcid _authzid passwd = BS.concat $
[ authzid''
, "\NUL"
, Text.encodeUtf8 $ authcid
, "\NUL"
, Text.encodeUtf8 $ passwd
]
where
authzid'' = maybe "" Text.encodeUtf8 authzid'
plain :: Username
-> Maybe AuthZID
-> Password
-> SaslHandler
plain authcid authzid passwd =
( "PLAIN"
, do
r <- runErrorT $ xmppPlain authcid authzid passwd
case r of
Left (AuthStreamFailure e) -> return $ Left e
Left e -> return $ Right $ Just e
Right () -> return $ Right Nothing
)