-
Notifications
You must be signed in to change notification settings - Fork 13
/
Main.hs
64 lines (54 loc) · 1.76 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
56
57
58
59
60
61
62
63
64
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Main where
import Control.Monad.Catch (throwM)
import qualified Data.Set as Set
import qualified Data.UUID as UUID
import qualified Data.UUID.V4 as UUID
import Import
import Network.Wai
import Network.Wai.Handler.Warp
import OwlCloud
import Servant
server :: Server UsersAPI
server = owlIn :<|> owlOut :<|> tokenValidity
usersAPI :: Proxy UsersAPI
usersAPI = Proxy
app :: Application
app = serve usersAPI server
owlIn :: LoginReq -> Handler SigninToken
owlIn LoginReq {..} =
case (whoo, passwoord) of
("great horned owl", "tiger") -> do
uuid <- liftIO UUID.nextRandom
let token = SigninToken (UUID.toText uuid)
liftIO $
atomically $
modifyTVar db $ \s -> s {validTokens = Set.insert token (validTokens s)}
return token
_ -> throwM (ServantErr 400 "Username/password pair did not match" "" [])
owlOut :: Maybe SigninToken -> Handler ()
owlOut mt = do
checkAuth mt
maybe (return ()) out mt
where
out token =
liftIO $
atomically $
modifyTVar db $ \s -> s {validTokens = Set.delete token (validTokens s)}
tokenValidity :: SigninToken -> Handler TokenValidity
tokenValidity token = do
state <- liftIO $ atomically $ readTVar db
return (TokenValidity (Set.member token (validTokens state)))
-- Business-logic and utils
checkAuth :: Maybe SigninToken -> Handler ()
checkAuth = maybe unauthorized runCheck
where
runCheck (SigninToken token) = do
state <- liftIO $ atomically $ readTVar db
let isMember = Set.member (SigninToken token) (validTokens state)
unless isMember unauthorized
unauthorized =
throwM (ServantErr 401 "You are not authenticated. Please sign-in" "" [])
main :: IO ()
main = run 8082 app