X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FAuthorization.hs;h=d085234b5e1b4cf8c7c71cae9a10bb059c167a7c;hb=9bb89434103e9a22f100d6ecb7e65a5d461e0454;hp=5c3b9eec7229638f0ca5a1dcecc1336a5c415b3e;hpb=1196f43ecedbb123515065f0440844864af906fb;p=Lucu.git diff --git a/Network/HTTP/Lucu/Authorization.hs b/Network/HTTP/Lucu/Authorization.hs index 5c3b9ee..d085234 100644 --- a/Network/HTTP/Lucu/Authorization.hs +++ b/Network/HTTP/Lucu/Authorization.hs @@ -1,3 +1,6 @@ +{-# LANGUAGE + UnicodeSyntax + #-} {-# OPTIONS_HADDOCK prune #-} -- |Manipulation of WWW authorization. @@ -11,12 +14,13 @@ module Network.HTTP.Lucu.Authorization , authCredentialP -- private ) where - -import qualified Codec.Binary.Base64 as B64 -import Data.Maybe -import Network.HTTP.Lucu.Parser -import Network.HTTP.Lucu.Parser.Http -import Network.HTTP.Lucu.Utils +import Data.Ascii (Ascii) +import qualified Data.Ascii as A +import qualified Data.ByteString.Base64 as B64 +import qualified Data.ByteString.Char8 as C8 +import Network.HTTP.Lucu.Parser.Http +import Network.HTTP.Lucu.Utils +import Prelude.Unicode -- |Authorization challenge to be sent to client with -- \"WWW-Authenticate\" header. See @@ -26,7 +30,7 @@ data AuthChallenge deriving (Eq) -- |'Realm' is just a string which must not contain any non-ASCII letters. -type Realm = String +type Realm = Ascii -- |Authorization credential to be sent by client with -- \"Authorization\" header. See @@ -37,31 +41,33 @@ data AuthCredential -- |'UserID' is just a string which must not contain colon and any -- non-ASCII letters. -type UserID = String +type UserID = Ascii -- |'Password' is just a string which must not contain any non-ASCII -- letters. -type Password = String - +type Password = Ascii +-- FIXME: Don't use String for network output. instance Show AuthChallenge where show (BasicAuthChallenge realm) - = "Basic realm=" ++ quoteStr realm + = "Basic realm=" ⧺ quoteStr realm - -authCredentialP :: Parser AuthCredential -authCredentialP = allowEOF $! - do string "Basic" - many1 lws - b64 <- many1 - $ satisfy (\ c -> (c >= 'a' && c <= 'z') || - (c >= 'A' && c <= 'Z') || - (c >= '0' && c <= '9') || - c == '+' || - c == '/' || - c == '=') - let decoded = map (toEnum . fromEnum) (fromJust $ B64.decode b64) - case break (== ':') decoded of - (uid, ':' : password) - -> return (BasicAuthCredential uid password) - _ -> failP +authCredentialP ∷ Parser AuthCredential +authCredentialP + = allowEOF $! + do _ ← string "Basic" + _ ← many1 lws + b64 ← many1 + $ satisfy (\c → (c ≥ 'a' ∧ c ≤ 'z') ∨ + (c ≥ 'A' ∧ c ≤ 'Z') ∨ + (c ≥ '0' ∧ c ≤ '9') ∨ + c ≡ '+' ∨ + c ≡ '/' ∨ + c ≡ '=') + case break (≡ ':') (decode b64) of + (uid, ':' : password) + → return (BasicAuthCredential uid password) + _ → failP + where + decode ∷ String → String + decode = C8.unpack ∘ B64.decodeLenient ∘ C8.pack