@ -26,9 +26,6 @@ import Network.Xmpp.Sasl.Types
import qualified System.Random as Random
import qualified System.Random as Random
data SaslElement = SaslSuccess ( Maybe Text . Text )
| SaslChallenge ( Maybe Text . Text )
--makeNonce :: SaslM BS.ByteString
--makeNonce :: SaslM BS.ByteString
makeNonce :: IO BS . ByteString
makeNonce :: IO BS . ByteString
makeNonce = do
makeNonce = do
@ -53,9 +50,10 @@ saslResponseE resp =
[]
[]
( maybeToList $ NodeContent . ContentText <$> resp )
( maybeToList $ NodeContent . ContentText <$> resp )
-- The <success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/> element.
xpSuccess :: PU [ Node ] ( Maybe Text . Text )
xpSuccess :: PU [ Node ] ( Maybe Text . Text )
xpSuccess = xpElemNodes " {urn:ietf:params:xml:ns:xmpp-sasl}success "
xpSuccess = xpElemNodes " {urn:ietf:params:xml:ns:xmpp-sasl}success "
( xpOption $ xpContent xpId )
( xpOption $ xpContent xpId )
-- Parses the incoming SASL data to a mapped list of pairs.
-- Parses the incoming SASL data to a mapped list of pairs.
pairs :: BS . ByteString -> Either String Pairs
pairs :: BS . ByteString -> Either String Pairs
@ -91,7 +89,7 @@ xpChallenge :: PU [Node] (Maybe Text.Text)
xpChallenge = xpElemNodes " {urn:ietf:params:xml:ns:xmpp-sasl}challenge "
xpChallenge = xpElemNodes " {urn:ietf:params:xml:ns:xmpp-sasl}challenge "
( xpOption $ xpContent xpId )
( xpOption $ xpContent xpId )
-- | pickler for SaslElement
-- | Pickler for SaslElement.
xpSaslElement :: PU [ Node ] SaslElement
xpSaslElement :: PU [ Node ] SaslElement
xpSaslElement = xpAlt saslSel
xpSaslElement = xpAlt saslSel
[ xpWrap SaslSuccess ( \ ( SaslSuccess x ) -> x ) xpSuccess
[ xpWrap SaslSuccess ( \ ( SaslSuccess x ) -> x ) xpSuccess
@ -101,7 +99,7 @@ xpSaslElement = xpAlt saslSel
saslSel ( SaslSuccess _ ) = 0
saslSel ( SaslSuccess _ ) = 0
saslSel ( SaslChallenge _ ) = 1
saslSel ( SaslChallenge _ ) = 1
-- | Add quotationmarks around a byte string
-- | Add quotationmarks around a byte string.
quote :: BS . ByteString -> BS . ByteString
quote :: BS . ByteString -> BS . ByteString
quote x = BS . concat [ " \ " " , x , " \ " " ]
quote x = BS . concat [ " \ " " , x , " \ " " ]
@ -109,7 +107,7 @@ saslInit :: Text.Text -> Maybe BS.ByteString -> SaslM Bool
saslInit mechanism payload = lift . pushElement . saslInitE mechanism $
saslInit mechanism payload = lift . pushElement . saslInitE mechanism $
Text . decodeUtf8 . B64 . encode <$> payload
Text . decodeUtf8 . B64 . encode <$> payload
-- | Pull the next element
-- | Pull the next element.
pullSaslElement :: SaslM SaslElement
pullSaslElement :: SaslM SaslElement
pullSaslElement = do
pullSaslElement = do
el <- lift $ pullPickle ( xpEither xpFailure xpSaslElement )
el <- lift $ pullPickle ( xpEither xpFailure xpSaslElement )
@ -117,7 +115,7 @@ pullSaslElement = do
Left e -> throwError $ AuthSaslFailure e
Left e -> throwError $ AuthSaslFailure e
Right r -> return r
Right r -> return r
-- | Pull the next element, checking that it is a challenge
-- | Pull the next element, checking that it is a challenge.
pullChallenge :: SaslM ( Maybe BS . ByteString )
pullChallenge :: SaslM ( Maybe BS . ByteString )
pullChallenge = do
pullChallenge = do
e <- pullSaslElement
e <- pullSaslElement
@ -128,12 +126,12 @@ pullChallenge = do
-> return $ Just sc
-> return $ Just sc
_ -> throwError AuthChallengeError
_ -> throwError AuthChallengeError
-- | Extract value from Just, failing with AuthChallengeError on Nothing
-- | Extract value from Just, failing with AuthChallengeError on Nothing.
saslFromJust :: Maybe a -> SaslM a
saslFromJust :: Maybe a -> SaslM a
saslFromJust Nothing = throwError $ AuthChallengeError
saslFromJust Nothing = throwError $ AuthChallengeError
saslFromJust ( Just d ) = return d
saslFromJust ( Just d ) = return d
-- | Pull the next element and check that it is success
-- | Pull the next element and check that it is success.
pullSuccess :: SaslM ( Maybe Text . Text )
pullSuccess :: SaslM ( Maybe Text . Text )
pullSuccess = do
pullSuccess = do
e <- pullSaslElement
e <- pullSaslElement
@ -142,7 +140,7 @@ pullSuccess = do
_ -> throwError $ AuthXmlError
_ -> throwError $ AuthXmlError
-- | Pull the next element. When it's success, return it's payload.
-- | Pull the next element. When it's success, return it's payload.
-- If it's a challenge, send an empty response and pull success
-- If it's a challenge, send an empty response and pull success.
pullFinalMessage :: SaslM ( Maybe BS . ByteString )
pullFinalMessage :: SaslM ( Maybe BS . ByteString )
pullFinalMessage = do
pullFinalMessage = do
challenge2 <- pullSaslElement
challenge2 <- pullSaslElement
@ -158,14 +156,13 @@ pullFinalMessage = do
Left _e -> throwError $ AuthChallengeError
Left _e -> throwError $ AuthChallengeError
Right x -> return $ Just x
Right x -> return $ Just x
-- | Extract p=q pairs from a challenge
-- | Extract p=q pairs from a challenge.
toPairs :: BS . ByteString -> SaslM Pairs
toPairs :: BS . ByteString -> SaslM Pairs
toPairs ctext = case pairs ctext of
toPairs ctext = case pairs ctext of
Left _e -> throwError AuthChallengeError
Left _e -> throwError AuthChallengeError
Right r -> return r
Right r -> return r
-- | Send a SASL response element. The content will be base64-encoded for you
-- | Send a SASL response element. The content will be base64-encoded.
respond :: Maybe BS . ByteString -> SaslM Bool
respond :: Maybe BS . ByteString -> SaslM Bool
respond = lift . pushElement . saslResponseE .
respond = lift . pushElement . saslResponseE .
fmap ( Text . decodeUtf8 . B64 . encode )
fmap ( Text . decodeUtf8 . B64 . encode )