2019-04-17 05:51:15 +00:00
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2019-04-17 08:45:48 +00:00
|
|
|
{-# LANGUAGE TypeSynonymInstances #-}
|
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)
|
|
|
|
|
2019-09-14 21:03:04 +00:00
|
|
|
import qualified Data.Text as T
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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
|
2019-04-17 08:45:48 +00:00
|
|
|
|
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
|
2019-04-17 05:51:15 +00:00
|
|
|
|
2019-05-13 20:50:24 +00:00
|
|
|
|
2019-04-17 05:51:15 +00:00
|
|
|
data AuthRequest = AuthRequest
|
2019-04-21 15:27:15 +00:00
|
|
|
{ authRequestTicket :: AuthTicket
|
2019-09-14 21:03:04 +00:00
|
|
|
, authRequestHash :: 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
|
|
|
|
|
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
|
|
|
|
|
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
|
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 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)
|
|
|
|
|
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
|
2019-09-14 21:03:04 +00:00
|
|
|
}
|
|
|
|
deriving (Show)
|
2019-09-16 06:59:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
data AuthOverview = AuthOverview
|
|
|
|
{ authOverviewId :: Int
|
|
|
|
, authOverviewComment :: T.Text
|
|
|
|
, authOverviewMethod :: AuthMethod
|
|
|
|
}
|
|
|
|
deriving (Show, Generic)
|
|
|
|
|
|
|
|
instance ToJSON AuthOverview where
|
|
|
|
toEncoding = genericToEncoding defaultOptions
|
|
|
|
|
|
|
|
instance FromJSON AuthOverview
|