mateamt/src/Types/Auth.hs

136 lines
2.6 KiB
Haskell
Raw Normal View History

2019-04-17 05:51:15 +00:00
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
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)
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
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
{ 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
data AuthMethod
= PrimaryPass
| SecondaryPass
| ChallengeResponse
deriving (Show, Generic, Enum, Eq, Ord)
2019-04-17 05:51:15 +00:00
instance ToJSON AuthMethod where
toEncoding = genericToEncoding defaultOptions
2019-04-17 05:51:15 +00:00
instance FromJSON AuthMethod
2019-04-17 05:51:15 +00:00
2019-05-13 20:50:24 +00:00
newtype AuthTicket = AuthTicket T.Text deriving (Show, Generic, Eq, Ord)
2019-04-21 15:27:15 +00:00
instance ToJSON AuthTicket where
toEncoding = genericToEncoding defaultOptions
2019-04-17 08:45:48 +00:00
instance FromJSON AuthTicket
2019-04-17 08:45:48 +00:00
2019-05-13 20:50:24 +00:00
newtype AuthResponse = AuthResponse T.Text deriving (Show, Generic)
2019-04-17 05:51:15 +00:00
instance ToJSON AuthResponse where
toEncoding = genericToEncoding defaultOptions
2019-04-17 08:45:48 +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
, 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
instance FromJSON AuthResult
2019-05-13 20:50:24 +00:00
newtype AuthToken = AuthToken T.Text deriving (Show, Generic)
2019-04-17 08:45:48 +00:00
instance ToJSON AuthToken where
toEncoding = genericToEncoding defaultOptions
2019-04-17 08:45:48 +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
{ tokenString :: T.Text
2019-04-21 15:27:15 +00:00
, tokenUser :: Int
, tokenExpiry :: UTCTime
, 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
}
deriving (Show, Ord)
2019-04-21 15:27:15 +00:00
instance Eq Ticket where
(Ticket i1 _ _ _) == (Ticket i2 _ _ _) = i1 == i2
data AuthData = AuthData
{ authDataId :: Int
, authDataUser :: Int
, authDataMethod :: AuthMethod
2019-09-15 09:01:47 +00:00
, authDataPayload :: T.Text
}
deriving (Show)