summaryrefslogtreecommitdiff
path: root/src/Authentication.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Authentication.hs')
-rw-r--r--src/Authentication.hs21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/Authentication.hs b/src/Authentication.hs
index 2908309..3b0e479 100644
--- a/src/Authentication.hs
+++ b/src/Authentication.hs
@@ -7,12 +7,18 @@
module Authentication (User, queryUser) where
import Data.Aeson
+import Data.Maybe
+import Data.Either.Extra
import GHC.Generics
import Network.HTTP.Conduit
import Data.ByteString.Lazy.Char8 (unpack)
import Control.Exception
+import Control.Monad.Trans
+import Control.Monad.Trans.Either
+import Control.Concurrent.MVar
makeUrl = ("https://www.googleapis.com/oauth2/v3/tokeninfo?id_token="++)
+type Token = String
data User = User
{ email :: String
@@ -21,7 +27,7 @@ data User = User
instance FromJSON User
-queryUser :: String -> IO (Maybe User)
+queryUser :: Token -> IO (Maybe User)
queryUser token = (queryUser' token) `catch` \e -> do
print (e :: HttpException)
return Nothing
@@ -29,3 +35,16 @@ queryUser token = (queryUser' token) `catch` \e -> do
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
+ ts <- lift $ readMVar tokenCache
+ _ <- test (token `elem` ts)
+ user <- lift $ queryUser token
+ return False
+
+ test False = return ()
+ test True = left True
+