Commit 7b41431a authored by Karen Konou's avatar Karen Konou

WIP: [GQL] Basic mutation authentication

parent 2b241420
...@@ -45,6 +45,7 @@ import Gargantext.Database.Schema.User (UserLight(..)) ...@@ -45,6 +45,7 @@ import Gargantext.Database.Schema.User (UserLight(..))
import Gargantext.Database.Schema.Node (node_id, node_hyperdata) import Gargantext.Database.Schema.Node (node_id, node_hyperdata)
import Gargantext.Prelude import Gargantext.Prelude
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Gargantext.API.GraphQL.Utils (AuthStatus(Invalid, Valid), authUser)
data UserInfo = UserInfo data UserInfo = UserInfo
{ ui_id :: Int { ui_id :: Int
...@@ -75,6 +76,7 @@ data UserInfoArgs ...@@ -75,6 +76,7 @@ data UserInfoArgs
data UserInfoMArgs data UserInfoMArgs
= UserInfoMArgs = UserInfoMArgs
{ ui_id :: Int { ui_id :: Int
, token :: Text
, ui_username :: Maybe Text , ui_username :: Maybe Text
, ui_email :: Maybe Text , ui_email :: Maybe Text
, ui_title :: Maybe Text , ui_title :: Maybe Text
...@@ -108,35 +110,38 @@ updateUserInfo (UserInfoMArgs { ui_id, .. }) = do ...@@ -108,35 +110,38 @@ updateUserInfo (UserInfoMArgs { ui_id, .. }) = do
users <- lift (getUsersWithNodeHyperdata ui_id) users <- lift (getUsersWithNodeHyperdata ui_id)
case users of case users of
[] -> panic $ "[updateUserInfo] User with id " <> (T.pack $ show ui_id) <> " doesn't exist." [] -> panic $ "[updateUserInfo] User with id " <> (T.pack $ show ui_id) <> " doesn't exist."
((UserLight { .. }, node_u):_) -> do ((UserLight { .. }, node_u):_) ->
let u_hyperdata = node_u ^. node_hyperdata case authUser ui_id token of
-- lift $ printDebug "[updateUserInfo] u" u Invalid -> panic "[updateUserInfo] failed to validate user"
let u_hyperdata' = uh ui_titleL ui_title $ Valid -> do
uh ui_sourceL ui_source $ let u_hyperdata = node_u ^. node_hyperdata
uh ui_cwFirstNameL ui_cwFirstName $ -- lift $ printDebug "[updateUserInfo] u" u
uh ui_cwLastNameL ui_cwLastName $ let u_hyperdata' = uh ui_titleL ui_title $
uh ui_cwCityL ui_cwCity $ uh ui_sourceL ui_source $
uh ui_cwCountryL ui_cwCountry $ uh ui_cwFirstNameL ui_cwFirstName $
uh' ui_cwLabTeamDeptsL ui_cwLabTeamDepts $ uh ui_cwLastNameL ui_cwLastName $
uh' ui_cwOrganizationL ui_cwOrganization $ uh ui_cwCityL ui_cwCity $
uh ui_cwOfficeL ui_cwOffice $ uh ui_cwCountryL ui_cwCountry $
uh ui_cwRoleL ui_cwRole $ uh' ui_cwLabTeamDeptsL ui_cwLabTeamDepts $
uh ui_cwTouchMailL ui_cwTouchMail $ uh' ui_cwOrganizationL ui_cwOrganization $
uh ui_cwTouchPhoneL ui_cwTouchPhone $ uh ui_cwOfficeL ui_cwOffice $
u_hyperdata uh ui_cwRoleL ui_cwRole $
-- NOTE: We have 1 username and 2 emails: userLight_email and ui_cwTouchMail uh ui_cwTouchMailL ui_cwTouchMail $
-- The userLight_email is more important: it is used for login and sending mail. uh ui_cwTouchPhoneL ui_cwTouchPhone $
-- Therefore we update ui_cwTouchMail and userLight_email. u_hyperdata
-- ui_cwTouchMail is to be removed in the future. -- NOTE: We have 1 username and 2 emails: userLight_email and ui_cwTouchMail
let u' = UserLight { userLight_id -- The userLight_email is more important: it is used for login and sending mail.
, userLight_username -- Therefore we update ui_cwTouchMail and userLight_email.
, userLight_email = fromMaybe userLight_email $ view ui_cwTouchMailL u_hyperdata -- ui_cwTouchMail is to be removed in the future.
, userLight_password } let u' = UserLight { userLight_id
-- lift $ printDebug "[updateUserInfo] with firstName" u_hyperdata' , userLight_username
_ <- lift $ updateHyperdata (node_u ^. node_id) u_hyperdata' , userLight_email = fromMaybe userLight_email $ view ui_cwTouchMailL u_hyperdata
_ <- lift $ updateUserEmail u' , userLight_password }
--let _newUser = toUser (u, u_hyperdata') -- lift $ printDebug "[updateUserInfo] with firstName" u_hyperdata'
pure 1 _ <- lift $ updateHyperdata (node_u ^. node_id) u_hyperdata'
_ <- lift $ updateUserEmail u'
--let _newUser = toUser (u, u_hyperdata')
pure 1
where where
uh _ Nothing u_hyperdata = u_hyperdata uh _ Nothing u_hyperdata = u_hyperdata
uh lens' (Just val) u_hyperdata = u_hyperdata & lens' .~ Just val uh lens' (Just val) u_hyperdata = u_hyperdata & lens' .~ Just val
......
...@@ -4,8 +4,37 @@ import Data.Morpheus.Types (GQLTypeOptions, fieldLabelModifier) ...@@ -4,8 +4,37 @@ import Data.Morpheus.Types (GQLTypeOptions, fieldLabelModifier)
import qualified Data.Text as T import qualified Data.Text as T
import Gargantext.Core.Utils.Prefix (unCapitalize, dropPrefix) import Gargantext.Core.Utils.Prefix (unCapitalize, dropPrefix)
import Gargantext.Prelude import Gargantext.Prelude
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8)
import Gargantext.API.Admin.Types (jwtSettings, HasSettings (settings))
import Servant.Auth.Server (verifyJWT, JWTSettings)
import Control.Lens.Getter (view)
import Gargantext.Database.Prelude (Cmd')
import Gargantext.API.Admin.Auth.Types (AuthenticatedUser (AuthenticatedUser, _authUser_id))
import Data.ByteString (ByteString)
import Gargantext.Database.Admin.Types.Node (unNodeId)
unPrefix :: T.Text -> GQLTypeOptions -> GQLTypeOptions unPrefix :: T.Text -> GQLTypeOptions -> GQLTypeOptions
unPrefix prefix options = options { fieldLabelModifier = nflm } unPrefix prefix options = options { fieldLabelModifier = nflm }
where where
nflm label = unCapitalize $ dropPrefix (T.unpack prefix) $ ( fieldLabelModifier options ) label nflm label = unCapitalize $ dropPrefix (T.unpack prefix) $ ( fieldLabelModifier options ) label
data AuthStatus = Valid | Invalid
authUser :: (HasSettings env) => Int -> Text -> Cmd' env err AuthStatus
authUser ui_id token = do
let token' = encodeUtf8 token
jwtS <- view $ settings . jwtSettings
u <- getUserFromToken jwtS token'
case u of
Nothing -> pure Invalid
Just au ->
if nId au == ui_id
then pure Valid
else pure Invalid
where
nId AuthenticatedUser {_authUser_id} = unNodeId _authUser_id
getUserFromToken :: JWTSettings -> ByteString -> IO (Maybe AuthenticatedUser)
getUserFromToken = verifyJWT
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