Commit 3fcd27d3 authored by Alexandre Delanoë's avatar Alexandre Delanoë
parents 28e68956 655584c0
...@@ -37,7 +37,7 @@ import Gargantext.Core.Utils.Prefix (unPrefix) ...@@ -37,7 +37,7 @@ import Gargantext.Core.Utils.Prefix (unPrefix)
import Gargantext.Database.Node (getRootUsername) import Gargantext.Database.Node (getRootUsername)
import Gargantext.Database.Types.Node (NodePoly(_node_id)) import Gargantext.Database.Types.Node (NodePoly(_node_id))
import Gargantext.Prelude hiding (reverse) import Gargantext.Prelude hiding (reverse)
import Test.QuickCheck (elements) import Test.QuickCheck (elements, oneof)
import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary) import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
--------------------------------------------------- ---------------------------------------------------
...@@ -51,6 +51,7 @@ data AuthRequest = AuthRequest { _authReq_username :: Username ...@@ -51,6 +51,7 @@ data AuthRequest = AuthRequest { _authReq_username :: Username
} }
deriving (Generic) deriving (Generic)
-- TODO: Use an HTTP error to wrap AuthInvalid
data AuthResponse = AuthResponse { _authRes_valid :: Maybe AuthValid data AuthResponse = AuthResponse { _authRes_valid :: Maybe AuthValid
, _authRes_inval :: Maybe AuthInvalid , _authRes_inval :: Maybe AuthInvalid
} }
...@@ -75,21 +76,18 @@ data CheckAuth = InvalidUser | InvalidPassword | Valid Token TreeId ...@@ -75,21 +76,18 @@ data CheckAuth = InvalidUser | InvalidPassword | Valid Token TreeId
deriving (Eq) deriving (Eq)
arbitraryUsername :: [Username] arbitraryUsername :: [Username]
arbitraryUsername = ["user1", "user2"] arbitraryUsername = ["gargantua", "user1", "user2"]
arbitraryPassword :: [Password] arbitraryPassword :: [Password]
arbitraryPassword = map reverse arbitraryUsername arbitraryPassword = map reverse arbitraryUsername
checkAuthRequest :: Username -> Password -> Connection -> IO CheckAuth checkAuthRequest :: Username -> Password -> Connection -> IO CheckAuth
checkAuthRequest u p c = case elem u arbitraryUsername of checkAuthRequest u p c
False -> pure InvalidUser | not (u `elem` arbitraryUsername) = pure InvalidUser
True -> case u == (reverse p) of | u /= reverse p = pure InvalidPassword
False -> pure InvalidPassword | otherwise = do
True -> do muId <- getRootUsername u c
muId <- getRootUsername u c pure $ maybe InvalidUser (Valid "token" . _node_id) $ head muId
let uId = maybe (panic "API.AUTH: no user node") _node_id $ head muId
pure $ Valid "token" uId
auth' :: Connection -> AuthRequest -> IO AuthResponse auth' :: Connection -> AuthRequest -> IO AuthResponse
auth' c (AuthRequest u p) = do auth' c (AuthRequest u p) = do
...@@ -112,7 +110,8 @@ instance Arbitrary AuthRequest where ...@@ -112,7 +110,8 @@ instance Arbitrary AuthRequest where
$(deriveJSON (unPrefix "_authRes_") ''AuthResponse) $(deriveJSON (unPrefix "_authRes_") ''AuthResponse)
instance ToSchema AuthResponse instance ToSchema AuthResponse
instance Arbitrary AuthResponse where instance Arbitrary AuthResponse where
arbitrary = AuthResponse <$> arbitrary <*> arbitrary arbitrary = oneof [ AuthResponse Nothing . Just <$> arbitrary
, flip AuthResponse Nothing . Just <$> arbitrary ]
$(deriveJSON (unPrefix "_authInv_") ''AuthInvalid) $(deriveJSON (unPrefix "_authInv_") ''AuthInvalid)
instance ToSchema AuthInvalid instance ToSchema AuthInvalid
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment