Skip to content

Commit 13b58f5

Browse files
committed
fix(web): send correct status codes on auth failures
Failure to authenticate due to a malformed or missing auth token is now reported differently than failure to authenticate due to a blatantly incorrect auth token. Fixes #53, #54.
1 parent 9fc2fba commit 13b58f5

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

web/app/Auth.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-# LANGUAGE ImportQualifiedPost #-}
22
{-# LANGUAGE OverloadedStrings #-}
33

4-
module Auth (register, unregister, checkAuth, TokenStore, initTokenStore, verifyAdmin) where
4+
module Auth (register, unregister, checkAuth, TokenStore, initTokenStore, verifyAdmin, AuthStatus(..)) where
55

66
import Control.Concurrent.STM (STM, modifyTVar, newTVarIO)
77
import Control.Concurrent.STM.TVar (TVar, readTVar)
@@ -17,6 +17,8 @@ import Data.Text.Lazy qualified as L
1717
import Data.UUID (UUID, fromText)
1818
import Data.UUID.V4 (nextRandom)
1919

20+
data AuthStatus = BadToken | Allowed | Disallowed
21+
2022
data BadPassfileException = WrongSpec | CantDecode deriving (Show)
2123

2224
data RequestException = BadTokenException deriving (Show)
@@ -50,16 +52,15 @@ initTokenStore =
5052
parse [cred] = return $ pack cred
5153
parse _ = throwIO WrongSpec
5254

53-
checkAuth :: TokenStore -> Name -> L.Text -> IO Bool
55+
checkAuth :: TokenStore -> Name -> L.Text -> IO AuthStatus
5456
checkAuth ts name t = case fromText $ L.toStrict t of
5557
Just token -> atomically $ checkAuth' token
56-
Nothing -> throwIO BadTokenException
58+
Nothing -> return BadToken
5759
where
58-
checkAuth' :: UUID -> STM Bool
5960
checkAuth' uuid = check uuid . tokens <$> readTVar ts
6061
check uuid toks = case toks Map.!? name of
61-
Just x -> x == uuid
62-
Nothing -> False
62+
Just x -> if x == uuid then Allowed else Disallowed
63+
Nothing -> Disallowed
6364

6465
unregister :: TokenStore -> Name -> IO ()
6566
unregister ts name = atomically $ modifyTVar ts unregister'

web/app/Main.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
module Main where
66

7-
import Auth (TokenStore, checkAuth, initTokenStore, register, unregister, verifyAdmin)
7+
import Auth (TokenStore, checkAuth, initTokenStore, register, unregister, verifyAdmin, AuthStatus(..))
88
import Control.Monad.IO.Class (liftIO)
99
import qualified Data.Text as T
1010
import Data.Text.Lazy (pack, toStrict)
1111
import Data.UUID (toText)
1212
import GHC.Conc (atomically)
13-
import Network.HTTP.Types.Status (badRequest400, unauthorized401, conflict409, internalServerError500)
13+
import Network.HTTP.Types.Status (badRequest400, unauthorized401, forbidden403, conflict409, internalServerError500)
1414
import Proc (Proc, call, kill, launch, mkCommand, mkUnvalidatedCommand, mkComp)
1515
import System.Environment (getArgs)
1616
import System.Posix.Signals (Handler (..), installHandler, sigINT)
@@ -75,8 +75,9 @@ runWebServer proc ts =
7575
check (Just uuid) = liftIO $ checkAuth ts name uuid
7676
check Nothing = status unauthorized401 >> finish
7777

78-
serve True = return name
79-
serve False = status unauthorized401 >> finish
78+
serve Allowed = return name
79+
serve Disallowed = status forbidden403 >> finish
80+
serve BadToken = status unauthorized401 >> finish
8081

8182
callCreate name color = callCmd $ mkCommand (mkComp name) (mkComp "CREATE") [mkComp color]
8283

0 commit comments

Comments
 (0)