summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2015-10-12 08:13:12 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2015-10-12 08:13:12 -0400
commit9aa7f0001830bf6fa2fbb38b9946dc24815fd0e0 (patch)
tree17a05bdd307345c80f2fe1fd319ee738587d20d4
parent4fe1a71b481527b7145e59c27eae3d3fd4060c74 (diff)
Authentication module to check if tokens are valid.
Keeps a cache of valid tokens to avoid multiple lookups.
-rw-r--r--src/Authentication.hs23
1 files changed, 13 insertions, 10 deletions
diff --git a/src/Authentication.hs b/src/Authentication.hs
index 3b0e479..0a0a49f 100644
--- a/src/Authentication.hs
+++ b/src/Authentication.hs
@@ -36,15 +36,18 @@ queryUser' token = do
response <- simpleHttp (makeUrl token)
return (decode response)
-isAuthenticated :: [[String]] -> MVar [Token] -> Token -> IO Bool
-isAuthenticated allowedUsers tokenCache token = runEitherT f >>= return . fromEither
- where f :: EitherT Bool IO Bool
- f = do
+isAuthenticated :: [String] -> MVar [Token] -> Token -> IO Bool
+isAuthenticated allowedUsers tokenCache token = runEitherT runner >>= return . fromEither
+ where runner :: EitherT Bool IO Bool
+ runner = do
ts <- lift $ readMVar tokenCache
- _ <- test (token `elem` ts)
+ _ <- leftIf (token `elem` ts) True
user <- lift $ queryUser token
- return False
-
- test False = return ()
- test True = left True
-
+ email' <- return $ fromMaybe "" (user >>= return . email)
+ _ <- leftIf (not (email' `elem` allowedUsers)) False
+ tokens <- lift $ takeMVar tokenCache
+ lift $ putMVar tokenCache (token:tokens)
+ return True
+
+ leftIf True x = left x
+ leftIf False x = right x