0 | module HTTP.API.Header
 1 |
 2 | import Data.Buffer
 3 | import Data.ByteString
 4 | import Derive.Prelude
 5 | import HTTP.API.Decode
 6 | import HTTP.Status
 7 |
 8 | %default total
 9 | %language ElabReflection
10 |
11 | public export
12 | record HeaderPart where
13 |   constructor H
14 |   name        : String
15 |   0 type      : Type
16 |   errorStatus : Status
17 |
18 | ||| Computes the list of types captured by a list of header descriptions.
19 | public export
20 | 0 HeaderTypes : List HeaderPart -> List Type
21 | HeaderTypes []              = []
22 | HeaderTypes (H _ t _ :: xs) = t :: HeaderTypes xs
23 |
24 | ||| Wraps a list of `HeaderPart`s to account for a full description
25 | ||| of values extracted from a request's headers.
26 | public export
27 | record ReqHeaders where
28 |   constructor Headers
29 |   headers : List HeaderPart
30 |
31 | --------------------------------------------------------------------------------
32 | -- OAuth Tokens
33 | --------------------------------------------------------------------------------
34 |
35 | public export
36 | record OAuthToken where
37 |   constructor OAT
38 |   token : ByteString
39 |
40 | %runElab derive "OAuthToken" [Show,Eq,Ord]
41 |
42 | export
43 | Decode OAuthToken where
44 |   decode bs =
45 |     if "Bearer" `isPrefixOf` bs
46 |        then Right $ OAT $ trimLeft (drop 6 bs)
47 |        else Left (Msg "Invalid bearer token")
48 |