]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/Authentication.hs
Destroy Data.Attoparsec.Parsable; use Data.Default instead
[Lucu.git] / Network / HTTP / Lucu / Authentication.hs
index 69223f2e1bb82c878c2f1174cc007a44a850f90e..a63419cea4b6b03e814120f82afeede81869d8cc 100644 (file)
@@ -3,6 +3,7 @@
   , MultiParamTypeClasses
   , OverloadedStrings
   , TemplateHaskell
+  , TypeSynonymInstances
   , UnicodeSyntax
   #-}
 -- |An internal module for HTTP authentication.
@@ -12,7 +13,6 @@ module Network.HTTP.Lucu.Authentication
     , Realm
     , UserID
     , Password
-    , authCredential
     )
     where
 import Control.Monad
@@ -24,6 +24,7 @@ import qualified Data.ByteString.Char8 as C8
 import Data.Convertible.Base
 import Data.Convertible.Instances.Ascii ()
 import Data.Convertible.Utils
+import Data.Default
 import Data.Monoid.Unicode
 import Network.HTTP.Lucu.Parser.Http
 import Network.HTTP.Lucu.Utils
@@ -64,25 +65,24 @@ deriveAttempts [ ([t| AuthChallenge |], [t| Ascii        |])
                , ([t| AuthChallenge |], [t| AsciiBuilder |])
                ]
 
--- |'Parser' for an 'AuthCredential'.
-authCredential ∷ Parser AuthCredential
-authCredential
-    = do void $ string "Basic"
-         skipMany1 lws
-         b64 ← takeWhile1 base64
-         case C8.break (≡ ':') (B64.decodeLenient b64) of
-           (user, cPassword)
-               | C8.null cPassword
-                   → fail "no colons in the basic auth credential"
-               | otherwise
-                   → do u ← asc user
-                        p ← asc (C8.tail cPassword)
-                        return (BasicAuthCredential u p)
-    where
-      base64 ∷ Char → Bool
-      base64 = inClass "a-zA-Z0-9+/="
+instance Default (Parser AuthCredential) where
+    def = do void $ string "Basic"
+             skipMany1 lws
+             b64 ← takeWhile1 base64
+             case C8.break (≡ ':') (B64.decodeLenient b64) of
+               (user, cPassword)
+                   | C8.null cPassword
+                       → fail "no colons in the basic auth credential"
+                   | otherwise
+                       → do u ← asc user
+                            p ← asc (C8.tail cPassword)
+                            return (BasicAuthCredential u p)
+        where
+          base64 ∷ Char → Bool
+          base64 = inClass "a-zA-Z0-9+/="
 
-      asc ∷ C8.ByteString → Parser Ascii
-      asc bs = case ca bs of
-                 Success as → return as
-                 Failure _  → fail "Non-ascii character in auth credential"
+          asc ∷ C8.ByteString → Parser Ascii
+          asc bs
+              = case ca bs of
+                  Success as → return as
+                  Failure _  → fail "Non-ascii character in auth credential"