feat: uncancer login signup and pipelines params

This commit is contained in:
GitBluub
2022-02-21 12:10:55 +01:00
parent 244b488d45
commit 8ccc631bad
4 changed files with 40 additions and 29 deletions
+3 -1
View File
@@ -5,6 +5,8 @@
module Api where
import GHC.Generics (Generic)
import Servant (Get, Handler, HasServer (ServerT), JSON, NamedRoutes, RemoteHost, type (:>))
import Servant.API.Generic (ToServantApi, type (:-))
@@ -23,7 +25,7 @@ import Control.Monad.Trans.Reader (ReaderT (runReaderT))
data API mode = API
{ about :: mode :- "about.json" :> RemoteHost :> Get '[JSON] About
, auth :: mode :- "auth" :> NamedRoutes AuthAPI
, pipelines :: mode :- "workflow" :> NamedRoutes PipelineAPI
, pipelines :: mode :- NamedRoutes PipelineAPI
}
deriving stock (Generic)
+12 -12
View File
@@ -1,6 +1,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE TypeOperators #-}
module Api.Auth where
@@ -38,14 +39,14 @@ import Password (hashPassword'', toPassword, validatePassword')
import Repository (createUser, getUserByName')
data LoginUser = LoginUser
{ loginUsername :: String
, loginPassword :: String
{ username :: String
, password :: String
}
deriving (Eq, Show, Read, Generic)
data SignupUser = SignupUser
{ signupUsername :: String
, signupPassword :: String
{ username :: String
, password :: String
}
deriving (Eq, Show, Read, Generic)
@@ -63,12 +64,12 @@ protected (Servant.Auth.Server.Authenticated user) = return $ toUser user
protected _ = throwAll err401
type Unprotected =
"login"
"login"
:> ReqBody '[JSON] LoginUser
:> Post '[JSON] (Headers '[Header "Set-Cookie" SetCookie, Header "Set-Cookie" SetCookie] NoContent)
:<|> "signup"
:> ReqBody '[JSON] SignupUser
:> Post '[JSON] NoContent
:<|> "signup"
:> ReqBody '[JSON] SignupUser
:> Post '[JSON] NoContent
loginHandler ::
CookieSettings ->
@@ -78,7 +79,7 @@ loginHandler ::
loginHandler cs jwts (LoginUser username p) = do
users' <- getUserByName' $ pack username
let usr = head users'
if validatePassword' (toPassword $ pack p) (password usr)
if validatePassword' (toPassword $ pack p) (Db.User.password usr)
then do
mApplyCookies <- liftIO $ acceptLogin cs jwts usr
case mApplyCookies of
@@ -102,9 +103,8 @@ unprotected cs jwts =
data AuthAPI mode = AuthAPI
{ protectedApi ::
mode
:- ( Servant.Auth.Server.Auth '[JWT] User'
:> Protected
)
:- Servant.Auth.Server.Auth '[JWT] User'
:> Protected
, unprotectedApi ::
mode
:- Unprotected
+22 -13
View File
@@ -3,6 +3,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
@@ -25,21 +26,21 @@ import Hasql.Statement (Statement)
import Hasql.Transaction (Transaction, statement)
import Rel8 (asc, each, insert, limit, orderBy, select)
import Repository
import Servant (Capture, Get, JSON, err401, throwError, type (:>))
import Servant.API (Delete, Post, Put, ReqBody)
import Servant (Capture, Get, JSON, err401, throwError, type (:>), NoContent (NoContent))
import Servant.API (Delete, Post, Put, ReqBody, QueryParam)
import Servant.API.Generic ((:-))
import Servant.Server.Generic (AsServerT)
import Utils (mapInd)
data PipelineData = PipelineData
{ pipelineDataName :: Text
, pipelineDataType :: PipelineType
, pipelineDataParams :: PipelineParams
{ name :: Text
, pType :: PipelineType
, pParams :: PipelineParams
}
data ReactionData = ReactionData
{ reactionDataType :: ReactionType
, reactionDataParams :: ReactionParams
{ rType :: ReactionType
, rParams :: ReactionParams
}
data PostPipelineData = PostPipelineData
@@ -54,10 +55,11 @@ $(deriveJSON defaultOptions ''ReactionData)
$(deriveJSON defaultOptions ''PostPipelineData)
data PipelineAPI mode = PipelineAPI
{ get :: mode :- Capture "id" PipelineId :> Get '[JSON] GetPipelineResponse
, post :: mode :- ReqBody '[JSON] PostPipelineData :> Post '[JSON] [ReactionId]
, put :: mode :- Capture "id" PipelineId :> Put '[JSON] (Pipeline Identity)
, del :: mode :- Capture "id" PipelineId :> Delete '[JSON] (Pipeline Identity)
{ get :: mode :- "workflow" :> Capture "id" PipelineId :> Get '[JSON] GetPipelineResponse
, post :: mode :- "workflow" :> ReqBody '[JSON] PostPipelineData :> Post '[JSON] [ReactionId]
, put :: mode :- "workflow" :> Capture "id" PipelineId :> Put '[JSON] (Pipeline Identity)
, del :: mode :- "workflow" :> Capture "id" PipelineId :> Delete '[JSON] (Pipeline Identity)
, all :: mode :- "workflows" :> QueryParam "API_KEY" String :>Get '[JSON] NoContent
}
deriving stock (Generic)
@@ -71,14 +73,14 @@ getPipelineHandler pipelineId = do
postPipelineHandler :: PostPipelineData -> AppM [ReactionId]
postPipelineHandler x = do
actionId <- createPipeline $ Pipeline (PipelineId 1) (pipelineDataName p) (pipelineDataType p) (pipelineDataParams p)
actionId <- createPipeline $ Pipeline (PipelineId 1) (name p) (pType p) (pParams p)
sequence $ mapInd (reactionMap (head actionId)) r
where
p = action x
r = reactions x
reactionMap :: PipelineId -> ReactionData -> Int -> AppM ReactionId
reactionMap actionId s i = do
res <- createReaction $ Reaction (ReactionId 1) (reactionDataType s) (reactionDataParams s) actionId (fromIntegral i)
res <- createReaction $ Reaction (ReactionId 1) (rType s) (rParams s) actionId (fromIntegral i)
return $ head res
putPipelineHandler :: PipelineId -> AppM (Pipeline Identity)
@@ -87,6 +89,12 @@ putPipelineHandler pipelineId = throwError err401
delPipelineHandler :: PipelineId -> AppM (Pipeline Identity)
delPipelineHandler pipelineId = throwError err401
allPipelineHandler :: Maybe String -> AppM NoContent
allPipelineHandler Nothing = do
--pipelines <- getPipelineByUser
return NoContent
allPipelineHandler (Just key) = return NoContent
pipelineHandler :: PipelineAPI (AsServerT AppM)
pipelineHandler =
PipelineAPI
@@ -94,4 +102,5 @@ pipelineHandler =
, post = postPipelineHandler
, put = putPipelineHandler
, del = delPipelineHandler
, Api.Pipeline.all = allPipelineHandler
}
+3 -3
View File
@@ -1,4 +1,4 @@
- toPassword check security
- Separate User and UserDB
- Craft JWT ?
- Crud pipelines
- UPDATE DELETE pipelines
- pipeline actions using jwt
-