{-# LANGUAGE EmptyDataDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
module Auth.Biscuit
(
newKeypair
, fromPrivateKey
, PrivateKey
, PublicKey
, Keypair (..)
, serializePrivateKeyHex
, serializePublicKeyHex
, parsePrivateKeyHex
, parsePublicKeyHex
, serializePrivateKey
, serializePublicKey
, parsePrivateKey
, parsePublicKey
, block
, blockContext
, mkBiscuit
, addBlock
, Biscuit
, Block
, serializeB64
, parseB64
, parse
, serialize
, parseHex
, serializeHex
, verifier
, verifyBiscuit
, verifyBiscuitWithLimits
, checkBiscuitSignature
, defaultLimits
, Verifier
, ParseError (..)
, VerificationError (..)
, ExecutionError (..)
, Limits (..)
) where
import Control.Monad ((<=<))
import Data.Bifunctor (first)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base16 as Hex
import qualified Data.ByteString.Base64.URL as B64
import Data.Text (Text)
import Auth.Biscuit.Datalog.AST (Block, Verifier, bContext)
import Auth.Biscuit.Datalog.Executor (ExecutionError (..),
Limits (..), defaultLimits)
import Auth.Biscuit.Datalog.Parser (block, verifier)
import Auth.Biscuit.Sel (Keypair (..), PrivateKey,
PublicKey, fromPrivateKey,
newKeypair, parsePrivateKey,
parsePublicKey,
serializePrivateKey,
serializePublicKey)
import Auth.Biscuit.Token (Biscuit, ParseError (..),
VerificationError (..),
addBlock, checkBiscuitSignature,
mkBiscuit, parseBiscuit,
serializeBiscuit, verifyBiscuit,
verifyBiscuitWithLimits)
import Auth.Biscuit.Utils (maybeToRight)
blockContext :: Text -> Block
blockContext :: Text -> Block
blockContext Text
c = Block
forall a. Monoid a => a
mempty { bContext :: Maybe Text
bContext = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
c }
fromHex :: MonadFail m => ByteString -> m ByteString
fromHex :: ByteString -> m ByteString
fromHex ByteString
input = do
(ByteString
decoded, ByteString
"") <- (ByteString, ByteString) -> m (ByteString, ByteString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((ByteString, ByteString) -> m (ByteString, ByteString))
-> (ByteString, ByteString) -> m (ByteString, ByteString)
forall a b. (a -> b) -> a -> b
$ ByteString -> (ByteString, ByteString)
Hex.decode ByteString
input
ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
decoded
serializePrivateKeyHex :: PrivateKey -> ByteString
serializePrivateKeyHex :: PrivateKey -> ByteString
serializePrivateKeyHex = ByteString -> ByteString
Hex.encode (ByteString -> ByteString)
-> (PrivateKey -> ByteString) -> PrivateKey -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrivateKey -> ByteString
serializePrivateKey
serializePublicKeyHex :: PublicKey -> ByteString
serializePublicKeyHex :: PublicKey -> ByteString
serializePublicKeyHex = ByteString -> ByteString
Hex.encode (ByteString -> ByteString)
-> (PublicKey -> ByteString) -> PublicKey -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PublicKey -> ByteString
serializePublicKey
parsePrivateKeyHex :: ByteString -> Maybe PrivateKey
parsePrivateKeyHex :: ByteString -> Maybe PrivateKey
parsePrivateKeyHex = ByteString -> Maybe PrivateKey
parsePrivateKey (ByteString -> Maybe PrivateKey)
-> (ByteString -> Maybe ByteString)
-> ByteString
-> Maybe PrivateKey
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< ByteString -> Maybe ByteString
forall (m :: * -> *). MonadFail m => ByteString -> m ByteString
fromHex
parsePublicKeyHex :: ByteString -> Maybe PublicKey
parsePublicKeyHex :: ByteString -> Maybe PublicKey
parsePublicKeyHex = ByteString -> Maybe PublicKey
parsePublicKey (ByteString -> Maybe PublicKey)
-> (ByteString -> Maybe ByteString)
-> ByteString
-> Maybe PublicKey
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< ByteString -> Maybe ByteString
forall (m :: * -> *). MonadFail m => ByteString -> m ByteString
fromHex
parse :: ByteString -> Either ParseError Biscuit
parse :: ByteString -> Either ParseError Biscuit
parse = ByteString -> Either ParseError Biscuit
parseBiscuit
parseB64 :: ByteString -> Either ParseError Biscuit
parseB64 :: ByteString -> Either ParseError Biscuit
parseB64 = ByteString -> Either ParseError Biscuit
parse (ByteString -> Either ParseError Biscuit)
-> (ByteString -> Either ParseError ByteString)
-> ByteString
-> Either ParseError Biscuit
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Text -> ParseError)
-> Either Text ByteString -> Either ParseError ByteString
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> Text -> ParseError
forall a b. a -> b -> a
const ParseError
InvalidB64Encoding) (Either Text ByteString -> Either ParseError ByteString)
-> (ByteString -> Either Text ByteString)
-> ByteString
-> Either ParseError ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text ByteString
B64.decodeBase64
parseHex :: ByteString -> Either ParseError Biscuit
parseHex :: ByteString -> Either ParseError Biscuit
parseHex = ByteString -> Either ParseError Biscuit
parse (ByteString -> Either ParseError Biscuit)
-> (ByteString -> Either ParseError ByteString)
-> ByteString
-> Either ParseError Biscuit
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< ParseError -> Maybe ByteString -> Either ParseError ByteString
forall b a. b -> Maybe a -> Either b a
maybeToRight ParseError
InvalidHexEncoding (Maybe ByteString -> Either ParseError ByteString)
-> (ByteString -> Maybe ByteString)
-> ByteString
-> Either ParseError ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe ByteString
forall (m :: * -> *). MonadFail m => ByteString -> m ByteString
fromHex
serialize :: Biscuit -> ByteString
serialize :: Biscuit -> ByteString
serialize = Biscuit -> ByteString
serializeBiscuit
serializeB64 :: Biscuit -> ByteString
serializeB64 :: Biscuit -> ByteString
serializeB64 = ByteString -> ByteString
B64.encodeBase64' (ByteString -> ByteString)
-> (Biscuit -> ByteString) -> Biscuit -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Biscuit -> ByteString
serialize
serializeHex :: Biscuit -> ByteString
serializeHex :: Biscuit -> ByteString
serializeHex = ByteString -> ByteString
Hex.encode (ByteString -> ByteString)
-> (Biscuit -> ByteString) -> Biscuit -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Biscuit -> ByteString
serialize