]> gitweb @ CieloNegro.org - Lucu.git/blob - Network/HTTP/Lucu/Authorization.hs
added facilities for basic authentication (not tested)
[Lucu.git] / Network / HTTP / Lucu / Authorization.hs
1 -- #prune
2
3 -- |Manipulation of WWW authorization.
4 module Network.HTTP.Lucu.Authorization
5     ( AuthChallenge(..)
6     , AuthCredential(..)
7     , Realm
8     , UserID
9     , Password
10
11     , authCredentialP -- private
12     )
13     where
14
15 import qualified Codec.Binary.Base64 as B64
16 import           Network.HTTP.Lucu.Parser
17 import           Network.HTTP.Lucu.Parser.Http
18 import           Network.HTTP.Lucu.Utils
19
20 -- |Authorization challenge to be sent to client with
21 -- \"WWW-Authenticate\" header. See
22 -- 'Network.HTTP.Lucu.Resource.setWWWAuthenticate'.
23 data AuthChallenge
24     = BasicAuthChallenge Realm
25       deriving (Eq)
26
27 -- |'Realm' is just a string which must not contain any non-ASCII letters.
28 type Realm = String    
29
30 -- |Authorization credential to be sent by client with
31 -- \"Authorization\" header. See
32 -- 'Network.HTTP.Lucu.Resource.getAuthorization'.
33 data AuthCredential
34     = BasicAuthCredential UserID Password
35       deriving (Show, Eq)
36
37 -- |'UserID' is just a string which must not contain colon and any
38 -- non-ASCII letters.
39 type UserID   = String
40
41 -- |'Password' is just a string which must not contain any non-ASCII
42 -- letters.
43 type Password = String
44
45
46 instance Show AuthChallenge where
47     show (BasicAuthChallenge realm)
48         = "Basic realm=" ++ quoteStr realm
49
50
51 authCredentialP :: Parser AuthCredential
52 authCredentialP = allowEOF $!
53                   do string "Basic"
54                      many1 lws
55                      b64 <- many1
56                             $ satisfy (\ c -> (c >= 'a' && c <= 'z') ||
57                                               (c >= 'A' && c <= 'Z') ||
58                                               (c >= '0' && c <= '9') ||
59                                               c == '+' ||
60                                               c == '/' ||
61                                               c == '=')
62                      let decoded = map (toEnum . fromEnum) (B64.decode b64)
63                      case break (== ':') decoded of
64                        (uid, ':' : password)
65                            -> return (BasicAuthCredential uid password)
66                        _   -> failP