2019-04-17 05:51:15 +00:00
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2019-04-17 08:45:48 +00:00
|
|
|
{-# LANGUAGE TypeSynonymInstances #-}
|
2020-08-26 19:29:39 +00:00
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
2019-04-17 05:51:15 +00:00
|
|
|
module Types.Auth where
|
|
|
|
|
|
|
|
import GHC.Generics
|
|
|
|
|
|
|
|
import Data.Aeson
|
|
|
|
|
2019-04-21 15:27:15 +00:00
|
|
|
import qualified Data.Set as S
|
|
|
|
|
|
|
|
import Data.Time.Clock (UTCTime)
|
|
|
|
|
2020-08-26 19:29:39 +00:00
|
|
|
import Data.ByteString (ByteString)
|
|
|
|
|
2022-07-17 19:28:22 +00:00
|
|
|
import Data.OpenApi
|
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
import qualified Data.Text as T
|
2020-08-26 19:29:39 +00:00
|
|
|
import Data.Text.Encoding
|
2019-04-17 05:51:15 +00:00
|
|
|
|
2019-05-06 21:41:05 +00:00
|
|
|
import Control.Concurrent.STM.TVar (TVar)
|
|
|
|
|
2019-04-21 15:27:15 +00:00
|
|
|
-- internal imports
|
|
|
|
|
2020-08-26 19:29:39 +00:00
|
|
|
import Classes
|
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
data TicketRequest = TicketRequest
|
|
|
|
{ ticketRequestUser :: Int
|
|
|
|
, ticketRequestMethod :: AuthMethod
|
|
|
|
}
|
|
|
|
deriving (Show, Generic)
|
|
|
|
|
|
|
|
instance ToJSON TicketRequest where
|
|
|
|
toEncoding = genericToEncoding defaultOptions
|
|
|
|
|
|
|
|
instance FromJSON TicketRequest
|
2022-07-17 19:28:22 +00:00
|
|
|
instance ToSchema TicketRequest
|
2019-09-14 21:03:04 +00:00
|
|
|
|
2019-04-17 05:51:15 +00:00
|
|
|
data AuthInfo = AuthInfo
|
2019-09-14 21:03:04 +00:00
|
|
|
{ authChallenge :: Maybe T.Text
|
2019-04-21 15:27:15 +00:00
|
|
|
, authTicket :: AuthTicket
|
2019-04-17 05:51:15 +00:00
|
|
|
}
|
|
|
|
deriving (Show, Generic)
|
|
|
|
|
|
|
|
instance ToJSON AuthInfo where
|
2019-07-27 14:34:28 +00:00
|
|
|
toEncoding = genericToEncoding defaultOptions
|
2019-04-17 05:51:15 +00:00
|
|
|
|
|
|
|
instance FromJSON AuthInfo
|
2022-07-17 19:28:22 +00:00
|
|
|
instance ToSchema AuthInfo
|
2019-04-17 05:51:15 +00:00
|
|
|
|
2020-08-26 19:29:39 +00:00
|
|
|
-- instance ToDatabase AuthInfo where
|
|
|
|
--
|
|
|
|
-- type InTuple AuthInfo = (Maybe T.Text, T.Text)
|
|
|
|
--
|
|
|
|
-- toDatabase (AuthInfo mChallenge (AuthTicket ticket)) =
|
|
|
|
-- (mChallenge, ticket)
|
|
|
|
|
2022-07-24 13:26:55 +00:00
|
|
|
instance DatabaseRepresentation AuthInfo where
|
|
|
|
|
|
|
|
type Representation AuthInfo = (Maybe ByteString, ByteString)
|
2020-08-26 19:29:39 +00:00
|
|
|
|
2022-07-24 13:26:55 +00:00
|
|
|
instance FromDatabase AuthInfo where
|
2020-08-26 19:29:39 +00:00
|
|
|
|
|
|
|
fromDatabase (mChallenge, ticket) =
|
2022-04-16 14:09:50 +00:00
|
|
|
AuthInfo (decodeUtf8 <$> mChallenge) (AuthTicket $ decodeUtf8 ticket)
|
2020-08-26 19:29:39 +00:00
|
|
|
|
2019-05-13 20:50:24 +00:00
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
data AuthMethod
|
|
|
|
= PrimaryPass
|
|
|
|
| SecondaryPass
|
|
|
|
| ChallengeResponse
|
|
|
|
deriving (Show, Generic, Enum, Eq, Ord)
|
2019-04-17 05:51:15 +00:00
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
instance ToJSON AuthMethod where
|
|
|
|
toEncoding = genericToEncoding defaultOptions
|
2019-04-17 05:51:15 +00:00
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
instance FromJSON AuthMethod
|
2019-04-17 05:51:15 +00:00
|
|
|
|
2022-07-17 19:28:22 +00:00
|
|
|
instance ToSchema AuthMethod
|
|
|
|
instance ToParamSchema AuthMethod
|
2019-05-13 20:50:24 +00:00
|
|
|
|
2019-09-16 06:59:57 +00:00
|
|
|
data AuthSubmit = AuthSubmit
|
|
|
|
{ authSubmitMethod :: AuthMethod
|
|
|
|
, authSubmitComment :: T.Text
|
|
|
|
, authSubmitPayload :: T.Text
|
|
|
|
}
|
|
|
|
deriving (Show, Generic)
|
|
|
|
|
|
|
|
instance ToJSON AuthSubmit where
|
|
|
|
toEncoding = genericToEncoding defaultOptions
|
|
|
|
|
|
|
|
instance FromJSON AuthSubmit
|
2022-07-17 19:28:22 +00:00
|
|
|
instance ToSchema AuthSubmit
|
2019-09-16 06:59:57 +00:00
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
newtype AuthTicket = AuthTicket T.Text deriving (Show, Generic, Eq, Ord)
|
2019-04-21 15:27:15 +00:00
|
|
|
|
|
|
|
instance ToJSON AuthTicket where
|
2019-09-14 21:03:04 +00:00
|
|
|
toEncoding = genericToEncoding defaultOptions
|
2019-04-17 08:45:48 +00:00
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
instance FromJSON AuthTicket
|
2022-07-17 19:28:22 +00:00
|
|
|
instance ToSchema AuthTicket
|
2019-05-13 20:50:24 +00:00
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
newtype AuthResponse = AuthResponse T.Text deriving (Show, Generic)
|
2019-04-17 05:51:15 +00:00
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
instance ToJSON AuthResponse where
|
|
|
|
toEncoding = genericToEncoding defaultOptions
|
2019-04-17 08:45:48 +00:00
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
instance FromJSON AuthResponse
|
2022-07-17 19:28:22 +00:00
|
|
|
instance ToSchema AuthResponse
|
2019-05-13 20:50:24 +00:00
|
|
|
|
2019-04-17 05:51:15 +00:00
|
|
|
data AuthRequest = AuthRequest
|
2022-04-17 14:38:48 +00:00
|
|
|
{ authRequestTicket :: AuthTicket
|
|
|
|
, authRequestPassword :: AuthResponse
|
2019-04-17 05:51:15 +00:00
|
|
|
}
|
|
|
|
deriving (Show, Generic)
|
|
|
|
|
|
|
|
instance ToJSON AuthRequest where
|
2019-04-17 08:45:48 +00:00
|
|
|
toEncoding = genericToEncoding defaultOptions
|
2019-04-17 05:51:15 +00:00
|
|
|
|
|
|
|
instance FromJSON AuthRequest
|
2022-07-17 19:28:22 +00:00
|
|
|
instance ToSchema AuthRequest
|
2019-05-13 20:50:24 +00:00
|
|
|
|
2019-04-17 05:51:15 +00:00
|
|
|
data AuthResult
|
|
|
|
= Granted
|
2019-05-09 14:53:19 +00:00
|
|
|
{ authToken :: AuthToken
|
2019-04-17 05:51:15 +00:00
|
|
|
}
|
|
|
|
| Denied
|
|
|
|
deriving (Show, Generic)
|
|
|
|
|
|
|
|
instance ToJSON AuthResult where
|
2019-04-17 08:45:48 +00:00
|
|
|
toEncoding = genericToEncoding defaultOptions
|
2019-04-17 05:51:15 +00:00
|
|
|
|
2019-09-09 10:57:36 +00:00
|
|
|
instance FromJSON AuthResult
|
2022-07-17 19:28:22 +00:00
|
|
|
instance ToSchema AuthResult
|
2019-05-13 20:50:24 +00:00
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
newtype AuthToken = AuthToken T.Text deriving (Show, Generic)
|
2019-04-17 08:45:48 +00:00
|
|
|
|
|
|
|
instance ToJSON AuthToken where
|
2019-09-14 21:03:04 +00:00
|
|
|
toEncoding = genericToEncoding defaultOptions
|
2019-04-17 08:45:48 +00:00
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
instance FromJSON AuthToken
|
2022-07-17 19:28:22 +00:00
|
|
|
instance ToSchema AuthToken
|
2019-05-13 20:50:24 +00:00
|
|
|
|
2019-04-21 15:27:15 +00:00
|
|
|
data Token = Token
|
2019-09-14 21:03:04 +00:00
|
|
|
{ tokenString :: T.Text
|
2019-04-21 15:27:15 +00:00
|
|
|
, tokenUser :: Int
|
|
|
|
, tokenExpiry :: UTCTime
|
2019-09-14 21:03:04 +00:00
|
|
|
, tokenMethod :: AuthMethod
|
2019-04-21 15:27:15 +00:00
|
|
|
}
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
2020-08-26 19:29:39 +00:00
|
|
|
-- instance ToDatabase Token where
|
|
|
|
--
|
|
|
|
-- type InTuple Token = (T.Text, Int, UTCTime, Int)
|
|
|
|
--
|
|
|
|
-- toDatabase (Token string usr exp method) =
|
|
|
|
-- (string, usr, exp, fromEnum method)
|
|
|
|
|
2022-07-24 13:26:55 +00:00
|
|
|
instance DatabaseRepresentation Token where
|
|
|
|
|
|
|
|
type Representation Token = (ByteString, Int, UTCTime, Int)
|
2020-08-26 19:29:39 +00:00
|
|
|
|
2022-07-24 13:26:55 +00:00
|
|
|
instance FromDatabase Token where
|
2020-08-26 19:29:39 +00:00
|
|
|
|
2021-07-12 11:29:40 +00:00
|
|
|
fromDatabase (string, usr, expiry, method) =
|
2022-04-16 14:09:50 +00:00
|
|
|
Token (decodeUtf8 string) usr expiry (toEnum method)
|
2020-08-26 19:29:39 +00:00
|
|
|
|
2019-05-13 20:50:24 +00:00
|
|
|
|
2019-05-06 21:41:05 +00:00
|
|
|
type TicketStore = TVar (S.Set Ticket)
|
2019-04-21 15:27:15 +00:00
|
|
|
|
2019-05-13 20:50:24 +00:00
|
|
|
|
2019-04-21 15:27:15 +00:00
|
|
|
data Ticket = Ticket
|
|
|
|
{ ticketId :: AuthTicket
|
|
|
|
, ticketUser :: Int
|
|
|
|
, ticketExpiry :: UTCTime
|
2019-09-15 09:01:47 +00:00
|
|
|
, ticketMethod :: (AuthMethod, Maybe T.Text)
|
2019-04-21 15:27:15 +00:00
|
|
|
}
|
2019-09-14 21:03:04 +00:00
|
|
|
deriving (Show, Ord)
|
2019-04-21 15:27:15 +00:00
|
|
|
|
|
|
|
instance Eq Ticket where
|
2019-09-14 21:03:04 +00:00
|
|
|
(Ticket i1 _ _ _) == (Ticket i2 _ _ _) = i1 == i2
|
|
|
|
|
|
|
|
|
|
|
|
data AuthData = AuthData
|
|
|
|
{ authDataId :: Int
|
|
|
|
, authDataUser :: Int
|
|
|
|
, authDataMethod :: AuthMethod
|
2019-09-16 06:59:57 +00:00
|
|
|
, authDataComment :: T.Text
|
2019-09-15 09:01:47 +00:00
|
|
|
, authDataPayload :: T.Text
|
2022-04-17 14:38:48 +00:00
|
|
|
, authDataSalt :: T.Text
|
2019-09-14 21:03:04 +00:00
|
|
|
}
|
|
|
|
deriving (Show)
|
2019-09-16 06:59:57 +00:00
|
|
|
|
2020-08-26 19:29:39 +00:00
|
|
|
-- instance ToDatabase AuthData where
|
|
|
|
--
|
|
|
|
-- type InTuple AuthData = (Int, Int, Int, T.Text, ByteString)
|
|
|
|
--
|
|
|
|
-- toDatabase (AuthData id_ usr method comm payload) =
|
|
|
|
-- (id_, usr, fromEnum method, comm, (B64.decode $ encodeUtf8 payload))
|
|
|
|
|
2022-07-24 13:26:55 +00:00
|
|
|
instance DatabaseRepresentation AuthData where
|
|
|
|
|
|
|
|
type Representation AuthData = (Int, Int, Int, T.Text, ByteString, ByteString)
|
2020-08-26 19:29:39 +00:00
|
|
|
|
2022-07-24 13:26:55 +00:00
|
|
|
instance FromDatabase AuthData where
|
2020-08-26 19:29:39 +00:00
|
|
|
|
2022-04-17 14:38:48 +00:00
|
|
|
fromDatabase (id_, usr, method, comm, payload, salt) =
|
|
|
|
AuthData id_ usr (toEnum method) comm (decodeUtf8 payload) (decodeUtf8 salt)
|
2020-08-26 19:29:39 +00:00
|
|
|
|
2019-09-16 06:59:57 +00:00
|
|
|
|
|
|
|
data AuthOverview = AuthOverview
|
|
|
|
{ authOverviewId :: Int
|
2021-06-16 16:09:08 +00:00
|
|
|
, authOverviewUser :: Int
|
2019-09-16 06:59:57 +00:00
|
|
|
, authOverviewComment :: T.Text
|
|
|
|
, authOverviewMethod :: AuthMethod
|
|
|
|
}
|
|
|
|
deriving (Show, Generic)
|
|
|
|
|
|
|
|
instance ToJSON AuthOverview where
|
|
|
|
toEncoding = genericToEncoding defaultOptions
|
|
|
|
|
|
|
|
instance FromJSON AuthOverview
|
2022-07-17 19:28:22 +00:00
|
|
|
instance ToSchema AuthOverview
|
2020-08-26 19:29:39 +00:00
|
|
|
|
|
|
|
-- instance ToDatabase AuthOverview where
|
|
|
|
--
|
|
|
|
-- type InTuple AuthOverview = (Int, T.Text, Int)
|
|
|
|
--
|
|
|
|
-- toDatabase (AuthOverview id_ comm method) =
|
|
|
|
-- (id_, comm, fromEnum method)
|
|
|
|
|
2022-07-24 13:26:55 +00:00
|
|
|
instance DatabaseRepresentation AuthOverview where
|
|
|
|
|
|
|
|
type Representation AuthOverview = (Int, Int, T.Text, Int)
|
2020-08-26 19:29:39 +00:00
|
|
|
|
2022-07-24 13:26:55 +00:00
|
|
|
instance FromDatabase AuthOverview where
|
2020-08-26 19:29:39 +00:00
|
|
|
|
2021-06-16 16:09:08 +00:00
|
|
|
fromDatabase (id_, uid, comm, method) =
|
|
|
|
AuthOverview id_ uid comm (toEnum method)
|