66{-# LANGUAGE NamedFieldPuns #-}
77{-# LANGUAGE OverloadedStrings #-}
88{-# LANGUAGE QuasiQuotes #-}
9+ {-# LANGUAGE RankNTypes #-}
910{-# LANGUAGE RecordWildCards #-}
1011{-# LANGUAGE TupleSections #-}
1112{-|
1718-}
1819module Auth.Biscuit.Datalog.ScopedExecutor
1920 ( BlockWithRevocationId
20- , runAuthorizer
21- , runAuthorizerWithLimits
21+ , runAuthorizerWithTimer
2222 , runAuthorizerNoTimeout
2323 , runFactGeneration
2424 , PureExecError (.. )
@@ -44,6 +44,7 @@ import qualified Data.List.NonEmpty as NE
4444import Data.Map (Map )
4545import qualified Data.Map as Map
4646import Data.Map.Strict ((!?) )
47+ import Data.Maybe (fromMaybe )
4748import Data.Set (Set )
4849import qualified Data.Set as Set
4950import Data.Text (Text )
@@ -57,13 +58,11 @@ import Auth.Biscuit.Datalog.Executor (Bindings, ExecutionError (..),
5758 MatchedQuery (.. ),
5859 ResultError (.. ), Scoped ,
5960 checkCheck , checkPolicy ,
60- countFacts , defaultLimits ,
61- fromScopedFacts ,
61+ countFacts , fromScopedFacts ,
6262 getBindingsForRuleBody ,
6363 getFactsForRule ,
6464 keepAuthorized' , toScopedFacts )
6565import Auth.Biscuit.Datalog.Parser (fact )
66- import Auth.Biscuit.Timer (timer )
6766import Auth.Biscuit.Utils (foldMapM , mapMaybeM )
6867import Data.Bitraversable (bisequence )
6968
@@ -94,35 +93,22 @@ data AuthorizationSuccess
9493getBindings :: AuthorizationSuccess -> Set Bindings
9594getBindings AuthorizationSuccess {matchedAllowQuery= MatchedQuery {bindings}} = bindings
9695
97- -- | Given a series of blocks and an authorizer, ensure that all
98- -- the checks and policies match
99- runAuthorizer :: BlockWithRevocationId
100- -- ^ The authority block
101- -> [BlockWithRevocationId ]
102- -- ^ The extra blocks
103- -> Authorizer
104- -- ^ A authorizer
105- -> IO (Either ExecutionError AuthorizationSuccess )
106- runAuthorizer = runAuthorizerWithLimits defaultLimits
10796
108- -- | Given a series of blocks and an authorizer, ensure that all
109- -- the checks and policies match, with provided execution
110- -- constraints
111- runAuthorizerWithLimits :: Limits
112- -- ^ custom limits
113- -> BlockWithRevocationId
114- -- ^ The authority block
115- -> [BlockWithRevocationId ]
116- -- ^ The extra blocks
117- -> Authorizer
118- -- ^ A authorizer
119- -> IO (Either ExecutionError AuthorizationSuccess )
120- runAuthorizerWithLimits l@ Limits {.. } authority blocks v = do
121- resultOrTimeout <- timer maxTime $ pure $ runAuthorizerNoTimeout l authority blocks v
122- pure $ case resultOrTimeout of
123- Nothing -> Left Timeout
124- Just r -> r
12597
98+ runAuthorizerWithTimer :: Functor f
99+ => (forall a . Int -> a -> f (Maybe a ))
100+ -- ^ time making sure evaluation does not last longer than the provided amount of microseconds
101+ -> Limits
102+ -- ^ custom limits
103+ -> BlockWithRevocationId
104+ -- ^ The authority block
105+ -> [BlockWithRevocationId ]
106+ -- ^ The extra blocks
107+ -> Authorizer
108+ -- ^ An authorizer
109+ -> f (Either ExecutionError AuthorizationSuccess )
110+ runAuthorizerWithTimer timer l@ Limits {maxTime} authority blocks v =
111+ fromMaybe (Left Timeout ) <$> timer maxTime (runAuthorizerNoTimeout l authority blocks v)
126112
127113mkRevocationIdFacts :: BlockWithRevocationId -> [BlockWithRevocationId ]
128114 -> Set Fact
0 commit comments