mateamt/src/Types/Auth.hs

225 lines
4.8 KiB
Haskell
Raw Normal View History

2019-04-17 05:51:15 +00:00
{-# LANGUAGE DeriveGeneric #-}
2019-04-17 08:45:48 +00:00
{-# LANGUAGE TypeSynonymInstances #-}
{-# 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)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base64 as B64
import qualified Data.Text as T
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
import Classes
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
-- instance ToDatabase AuthInfo where
--
-- type InTuple AuthInfo = (Maybe T.Text, T.Text)
--
-- toDatabase (AuthInfo mChallenge (AuthTicket ticket)) =
-- (mChallenge, ticket)
instance FromDatabase AuthInfo where
type OutTuple AuthInfo = (Maybe T.Text, T.Text)
fromDatabase (mChallenge, ticket) =
AuthInfo mChallenge (AuthTicket ticket)
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
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
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)
-- instance ToDatabase Token where
--
-- type InTuple Token = (T.Text, Int, UTCTime, Int)
--
-- toDatabase (Token string usr exp method) =
-- (string, usr, exp, fromEnum method)
instance FromDatabase Token where
type OutTuple Token = (T.Text, Int, UTCTime, Int)
fromDatabase (string, usr, exp, method) =
Token string usr exp (toEnum method)
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-16 06:59:57 +00:00
, authDataComment :: T.Text
2019-09-15 09:01:47 +00:00
, authDataPayload :: T.Text
}
deriving (Show)
2019-09-16 06:59:57 +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))
instance FromDatabase AuthData where
type OutTuple AuthData = (Int, Int, Int, T.Text, ByteString)
fromDatabase (id_, usr, method, comm, payload) =
AuthData id_ usr (toEnum method) comm (decodeUtf8 $ B64.encode payload)
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
-- instance ToDatabase AuthOverview where
--
-- type InTuple AuthOverview = (Int, T.Text, Int)
--
-- toDatabase (AuthOverview id_ comm method) =
-- (id_, comm, fromEnum method)
instance FromDatabase AuthOverview where
type OutTuple AuthOverview = (Int, T.Text, Int)
fromDatabase (id_, comm, method) =
AuthOverview id_ comm (toEnum method)