-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.hs
209 lines (182 loc) · 6.92 KB
/
app.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Main where
import ClassyPrelude hiding (Handler)
import Control.Lens
import Control.Monad.Except
import Control.Monad.Logger
import Data.Aeson (Value(Number))
import Data.Aeson.Lens
import Data.ByteString.Builder
import Database.Persist.Sqlite
import Database.Persist.TH
import Network.HTTP.Types.Status
import Network.Wai
import Network.Wai.Handler.Warp
import Servant
import Servant.HTML.Blaze
import Servant.Server.Experimental.Auth
import Text.Pretty.Simple
import Web.Cookie
import Web.JWT
--------
-- DB --
--------
$(share
[ mkPersist sqlSettings {mpsGenerateLenses = False}
, mkMigrate "migrateAll"
]
[persistLowerCase|
User
name Text
deriving Eq
deriving Show
deriving Typeable
TwitterUser
username Text
user UserId
UniqueTwitterUserUsername username
UniqueTwitterUserUser user
deriving Eq
deriving Show
deriving Typeable
|]
)
----------------------
-- Servant Handlers --
----------------------
jwtSecret :: Secret
jwtSecret = secret "foobarbaz"
instance FromHttpApiData Cookies where
parseHeader = Right . parseCookies
instance MonadUnliftIO Handler where
askUnliftIO :: Handler (UnliftIO Handler)
-- XXX: Throw away errors:
askUnliftIO =
pure $
UnliftIO
(\(Handler (ExceptT ioEither)) ->
ioEither >>= either (const (error "throw away servant err")) pure
)
type AfterLoginApi = "after-login" :> Header "Cookie" Cookies :> Get '[HTML] Text
type HomePageApi = "index.html" :> AuthProtect "jwt" :> Get '[HTML] Text
type Api = AfterLoginApi :<|> HomePageApi
serverRoot :: SqlBackend -> ServerT Api Handler
serverRoot backend = afterLogin backend :<|> homePage backend
afterLogin :: SqlBackend -> Maybe Cookies -> Handler Text
afterLogin backend maybeCookies = do
case maybeCookies of
Nothing -> error "no cookies"
Just cookies ->
case lookup "jwt" cookies of
Nothing -> error "no jwt cookie"
Just jwt -> do
pPrint jwt
let textJWT = decodeUtf8 jwt
let maybeVerifiedJWT = decodeAndVerifySignature jwtSecret textJWT
putStrLn "maybeVerifiedJWT:"
pPrint maybeVerifiedJWT
case maybeVerifiedJWT of
Nothing -> error "could not verify jwt"
Just verifiedJWT -> do
let jwtClaims = unregisteredClaims $ claims verifiedJWT
case lookup "profile" jwtClaims of
Nothing -> error "could not find the twitter profile in jwt claims"
Just profileJSON ->
case profileJSON ^? key "username" . _String of
Nothing -> error "could not find the username key in the profile"
Just username -> do
userKey <- flip runSqlConn backend $ do
maybeTwitterUser <- getBy $ UniqueTwitterUserUsername username
case maybeTwitterUser of
Nothing -> do
-- create new user
userKey <- insert $ User "fred"
insert_ $ TwitterUser username userKey
pure userKey
Just (Entity twitterUserKey (TwitterUser _ userKey)) ->
-- reuse existing user
pure userKey
let newClaims = insertMap "userid" (Number $ fromIntegral $ fromSqlKey userKey) jwtClaims
newClaimsSet = (claims verifiedJWT) { unregisteredClaims = newClaims }
newJWT = encodeUtf8 $ encodeSigned HS256 jwtSecret newClaimsSet
newSetCookie =
defaultSetCookie
{ setCookieName = "jwt"
, setCookieValue = newJWT
, setCookiePath = Just "/"
, setCookieDomain = Just ".servant-and-login-with.com"
, setCookieHttpOnly = True
, setCookieSecure = True
}
let headers =
[ ("Set-Cookie", toStrict . toLazyByteString $ renderSetCookie newSetCookie)
, ("Location", "https://servant-and-login-with.com:8443/index.html")
]
throwError $ err302 { errHeaders = headers }
homePage :: SqlBackend -> Key User -> Handler Text
homePage backend userKey = do
(twitterUsers, users) <- flip runSqlConn backend $ do
(twitterUsers :: [Entity TwitterUser]) <- selectList [] []
(users :: [Entity User]) <- selectList [] []
pure (twitterUsers, users)
putStrLn "twitter users:"
pPrint twitterUsers
putStrLn "users:"
pPrint users
pure $ "<p>authenticated as: " <> tshow userKey <> "</p>"
type instance AuthServerData (AuthProtect "jwt") = Key User
auther :: AuthHandler Request (AuthServerData (AuthProtect "jwt"))
auther = mkAuthHandler $ \req -> do
let headers = requestHeaders req
case lookup "Cookie" headers of
Nothing -> error "no cookies"
Just rawCookies -> do
let cookies = parseCookies rawCookies
case lookup "jwt" cookies of
Nothing -> error "no jwt cookie"
Just jwt -> do
pPrint jwt
let textJWT = decodeUtf8 jwt
let maybeVerifiedJWT = decodeAndVerifySignature jwtSecret textJWT
putStrLn "maybeVerifiedJWT:"
pPrint maybeVerifiedJWT
case maybeVerifiedJWT of
Nothing -> error "could not verify jwt"
Just verifiedJWT -> do
let jwtClaims = unregisteredClaims $ claims verifiedJWT
case lookup "userid" jwtClaims of
Nothing -> error "could not find the user id in the jwt claims"
Just claims ->
case claims ^? _Integral of
Nothing -> error "could not decode user id as number"
Just userId -> pure $ toSqlKey userId
----------
-- Main --
----------
instance MonadLogger IO where
monadLoggerLog a b c d = runStdoutLoggingT $ monadLoggerLog a b c d
app :: SqlBackend -> Application
app = serveWithContext (Proxy @Api) (auther :. EmptyContext) . serverRoot
-- app :: Application
-- app req respFunc = do
-- pPrint req
-- respFunc $ responseLBS status200 [] "<p>hello</p>"
main :: IO ()
main =
withSqliteConn ":memory:" $ \backend -> do
runSqlConn (runMigration migrateAll) backend
run 8000 $ app backend