]> gitweb @ CieloNegro.org - Lucu.git/blob - Network/HTTP/Lucu/Authorization.hs
Use base64-bytestring instead of dataenc
[Lucu.git] / Network / HTTP / Lucu / Authorization.hs
1 {-# LANGUAGE
2     UnicodeSyntax
3   #-}
4 {-# OPTIONS_HADDOCK prune #-}
5
6 -- |Manipulation of WWW authorization.
7 module Network.HTTP.Lucu.Authorization
8     ( AuthChallenge(..)
9     , AuthCredential(..)
10     , Realm
11     , UserID
12     , Password
13
14     , authCredentialP -- private
15     )
16     where
17 import qualified Data.ByteString.Base64 as B64
18 import qualified Data.ByteString.Char8 as C8
19 import Network.HTTP.Lucu.Parser
20 import Network.HTTP.Lucu.Parser.Http
21 import Network.HTTP.Lucu.Utils
22 import Prelude.Unicode
23
24 -- |Authorization challenge to be sent to client with
25 -- \"WWW-Authenticate\" header. See
26 -- 'Network.HTTP.Lucu.Resource.setWWWAuthenticate'.
27 data AuthChallenge
28     = BasicAuthChallenge Realm
29       deriving (Eq)
30
31 -- |'Realm' is just a string which must not contain any non-ASCII letters.
32 type Realm = String
33
34 -- |Authorization credential to be sent by client with
35 -- \"Authorization\" header. See
36 -- 'Network.HTTP.Lucu.Resource.getAuthorization'.
37 data AuthCredential
38     = BasicAuthCredential UserID Password
39       deriving (Show, Eq)
40
41 -- |'UserID' is just a string which must not contain colon and any
42 -- non-ASCII letters.
43 type UserID   = String
44
45 -- |'Password' is just a string which must not contain any non-ASCII
46 -- letters.
47 type Password = String
48
49 instance Show AuthChallenge where
50     show (BasicAuthChallenge realm)
51         = "Basic realm=" ⧺ quoteStr realm
52
53 authCredentialP ∷ Parser AuthCredential
54 authCredentialP
55     = allowEOF $!
56       do _   ← string "Basic"
57          _   ← many1 lws
58          b64 ← many1
59                $ satisfy (\c → (c ≥ 'a' ∧ c ≤ 'z') ∨
60                                (c ≥ 'A' ∧ c ≤ 'Z') ∨
61                                (c ≥ '0' ∧ c ≤ '9') ∨
62                                 c ≡ '+' ∨
63                                 c ≡ '/' ∨
64                                 c ≡ '=')
65          case break (≡ ':') (decode b64) of
66            (uid, ':' : password)
67                → return (BasicAuthCredential uid password)
68            _   → failP
69     where
70       decode ∷ String → String
71       decode = C8.unpack ∘ B64.decodeLenient ∘ C8.pack