-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hs
55 lines (46 loc) · 1.4 KB
/
main.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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
import Control.Monad
import Data.Text
import Data.Text.Encoding
import Database.Persist
import Database.Persist.Sqlite
import Database.Persist.TH
import Network.Wai
import System.Directory
import Yesod hiding (runDB)
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
User
token Text
UniqueToken token
deriving Show
|]
data TokenApp = TokenApp
mkYesod "TokenApp" [parseRoutes|
/ HomeR GET
|]
instance Yesod TokenApp where
isAuthorized _ _ = do
mUser <- maybeCurrentUser
return $ case mUser of
Just _ -> Authorized
Nothing -> Unauthorized "Please provide a token"
maybeCurrentUser = do
req <- waiRequest
case lookup "token-header" (requestHeaders req) of
Nothing -> return Nothing
Just token -> runDB . getBy . UniqueToken $ decodeUtf8 token
dbFile = "poc.sqlite"
runDB = runSqlite dbFile
getHomeR :: Handler Html
getHomeR = defaultLayout [whamlet|Token authentication POC!|]
main :: IO ()
main = do
exists <- doesFileExist dbFile
when exists (removeFile dbFile)
runDB $ do
runMigration migrateAll
insert $ User "secure_token"
warp 3000 TokenApp