module Conformance.Authenticate
(
AuthDecision (..)
, AuthReject (..)
, authenticate
, authenticateWeak
, Assets
) where
import Data.ByteString (ByteString)
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
type Assets = Map ByteString (Map ByteString Integer)
data AuthDecision
= AuthAccept
| AuthReject AuthReject
deriving stock (Int -> AuthDecision -> ShowS
[AuthDecision] -> ShowS
AuthDecision -> String
(Int -> AuthDecision -> ShowS)
-> (AuthDecision -> String)
-> ([AuthDecision] -> ShowS)
-> Show AuthDecision
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AuthDecision -> ShowS
showsPrec :: Int -> AuthDecision -> ShowS
$cshow :: AuthDecision -> String
show :: AuthDecision -> String
$cshowList :: [AuthDecision] -> ShowS
showList :: [AuthDecision] -> ShowS
Show, AuthDecision -> AuthDecision -> Bool
(AuthDecision -> AuthDecision -> Bool)
-> (AuthDecision -> AuthDecision -> Bool) -> Eq AuthDecision
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AuthDecision -> AuthDecision -> Bool
== :: AuthDecision -> AuthDecision -> Bool
$c/= :: AuthDecision -> AuthDecision -> Bool
/= :: AuthDecision -> AuthDecision -> Bool
Eq)
data AuthReject
=
PolicyAbsent
|
NameMismatch
|
QuantityNotOne Integer
deriving stock (Int -> AuthReject -> ShowS
[AuthReject] -> ShowS
AuthReject -> String
(Int -> AuthReject -> ShowS)
-> (AuthReject -> String)
-> ([AuthReject] -> ShowS)
-> Show AuthReject
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AuthReject -> ShowS
showsPrec :: Int -> AuthReject -> ShowS
$cshow :: AuthReject -> String
show :: AuthReject -> String
$cshowList :: [AuthReject] -> ShowS
showList :: [AuthReject] -> ShowS
Show, AuthReject -> AuthReject -> Bool
(AuthReject -> AuthReject -> Bool)
-> (AuthReject -> AuthReject -> Bool) -> Eq AuthReject
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AuthReject -> AuthReject -> Bool
== :: AuthReject -> AuthReject -> Bool
$c/= :: AuthReject -> AuthReject -> Bool
/= :: AuthReject -> AuthReject -> Bool
Eq)
authenticate :: ByteString -> ByteString -> Assets -> AuthDecision
authenticate :: ByteString -> ByteString -> Assets -> AuthDecision
authenticate ByteString
policy ByteString
name Assets
assets = case ByteString -> Assets -> Maybe (Map ByteString Integer)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ByteString
policy Assets
assets of
Maybe (Map ByteString Integer)
Nothing -> AuthReject -> AuthDecision
AuthReject AuthReject
PolicyAbsent
Just Map ByteString Integer
underPolicy -> case ByteString -> Map ByteString Integer -> Maybe Integer
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ByteString
name Map ByteString Integer
underPolicy of
Maybe Integer
Nothing -> AuthReject -> AuthDecision
AuthReject AuthReject
NameMismatch
Just Integer
1 -> AuthDecision
AuthAccept
Just Integer
q -> AuthReject -> AuthDecision
AuthReject (Integer -> AuthReject
QuantityNotOne Integer
q)
authenticateWeak :: ByteString -> Assets -> AuthDecision
authenticateWeak :: ByteString -> Assets -> AuthDecision
authenticateWeak ByteString
policy Assets
assets = case ByteString -> Assets -> Maybe (Map ByteString Integer)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ByteString
policy Assets
assets of
Maybe (Map ByteString Integer)
Nothing -> AuthReject -> AuthDecision
AuthReject AuthReject
PolicyAbsent
Just Map ByteString Integer
_ -> AuthDecision
AuthAccept