5 {-# OPTIONS_HADDOCK prune #-}
7 -- |Manipulation of WWW authorization.
8 module Network.HTTP.Lucu.Authorization
16 , authCredentialP -- private
19 import Data.Ascii (Ascii)
20 import qualified Data.Ascii as A
21 import Data.Attoparsec.Char8
22 import qualified Data.ByteString.Base64 as B64
23 import qualified Data.ByteString.Char8 as C8
25 import Data.Monoid.Unicode
26 import Network.HTTP.Lucu.Parser.Http
27 import Network.HTTP.Lucu.Utils
28 import Prelude.Unicode
30 -- |Authorization challenge to be sent to client with
31 -- \"WWW-Authenticate\" header. See
32 -- 'Network.HTTP.Lucu.Resource.setWWWAuthenticate'.
34 = BasicAuthChallenge Realm
37 -- |'Realm' is just a string which must not contain any non-ASCII letters.
40 -- |Authorization credential to be sent by client with
41 -- \"Authorization\" header. See
42 -- 'Network.HTTP.Lucu.Resource.getAuthorization'.
44 = BasicAuthCredential UserID Password
47 -- |'UserID' is just a string which must not contain colon and any
51 -- |'Password' is just a string which must not contain any non-ASCII
55 -- |Convert an 'AuthChallenge' to 'Ascii'.
56 printAuthChallenge ∷ AuthChallenge → Ascii
57 printAuthChallenge (BasicAuthChallenge realm)
58 = A.fromAsciiBuilder $
59 A.toAsciiBuilder "Basic realm=" ⊕ quoteStr realm
61 authCredentialP ∷ Parser AuthCredential
66 b64 ← takeWhile1 base64
67 case C8.break (≡ ':') (B64.decodeLenient b64) of
70 → fail "no colons in the basic auth credential"
72 → let u = fromJust $ A.fromByteString user
73 p = fromJust $ A.fromByteString (C8.tail cPassword)
75 return (BasicAuthCredential u p)
78 base64 = inClass "a-zA-Z0-9+/="