{-# LANGUAGE DataKinds #-}
module Crypto.WebAuthn.Encoding.WebAuthnJson
(
wjEncodeCredentialOptionsRegistration,
WJCredentialOptionsRegistration (..),
WJCredentialRegistration (..),
wjDecodeCredentialRegistration',
wjDecodeCredentialRegistration,
wjEncodeCredentialOptionsAuthentication,
WJCredentialOptionsAuthentication (..),
WJCredentialAuthentication (..),
wjDecodeCredentialAuthentication,
)
where
import Control.Monad.Except (runExceptT)
import Control.Monad.Identity (runIdentity)
import Control.Monad.Reader (runReaderT)
import Crypto.WebAuthn.AttestationStatementFormat (allSupportedFormats)
import qualified Crypto.WebAuthn.Encoding.Internal.WebAuthnJson as WJ
import qualified Crypto.WebAuthn.Model.Kinds as K
import qualified Crypto.WebAuthn.Model.Types as T
import Data.Aeson (FromJSON, ToJSON)
import Data.Text (Text)
wjEncodeCredentialOptionsRegistration ::
T.CredentialOptions 'K.Registration ->
WJCredentialOptionsRegistration
wjEncodeCredentialOptionsRegistration :: CredentialOptions 'Registration -> WJCredentialOptionsRegistration
wjEncodeCredentialOptionsRegistration = PublicKeyCredentialCreationOptions
-> WJCredentialOptionsRegistration
WJCredentialOptionsRegistration forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Encode a => a -> JSON a
WJ.encode
newtype WJCredentialOptionsRegistration = WJCredentialOptionsRegistration
{ WJCredentialOptionsRegistration
-> PublicKeyCredentialCreationOptions
_unWJCredentialOptionsRegistration :: WJ.PublicKeyCredentialCreationOptions
}
deriving newtype (Int -> WJCredentialOptionsRegistration -> ShowS
[WJCredentialOptionsRegistration] -> ShowS
WJCredentialOptionsRegistration -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WJCredentialOptionsRegistration] -> ShowS
$cshowList :: [WJCredentialOptionsRegistration] -> ShowS
show :: WJCredentialOptionsRegistration -> String
$cshow :: WJCredentialOptionsRegistration -> String
showsPrec :: Int -> WJCredentialOptionsRegistration -> ShowS
$cshowsPrec :: Int -> WJCredentialOptionsRegistration -> ShowS
Show, WJCredentialOptionsRegistration
-> WJCredentialOptionsRegistration -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WJCredentialOptionsRegistration
-> WJCredentialOptionsRegistration -> Bool
$c/= :: WJCredentialOptionsRegistration
-> WJCredentialOptionsRegistration -> Bool
== :: WJCredentialOptionsRegistration
-> WJCredentialOptionsRegistration -> Bool
$c== :: WJCredentialOptionsRegistration
-> WJCredentialOptionsRegistration -> Bool
Eq, Value -> Parser [WJCredentialOptionsRegistration]
Value -> Parser WJCredentialOptionsRegistration
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [WJCredentialOptionsRegistration]
$cparseJSONList :: Value -> Parser [WJCredentialOptionsRegistration]
parseJSON :: Value -> Parser WJCredentialOptionsRegistration
$cparseJSON :: Value -> Parser WJCredentialOptionsRegistration
FromJSON, [WJCredentialOptionsRegistration] -> Encoding
[WJCredentialOptionsRegistration] -> Value
WJCredentialOptionsRegistration -> Encoding
WJCredentialOptionsRegistration -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [WJCredentialOptionsRegistration] -> Encoding
$ctoEncodingList :: [WJCredentialOptionsRegistration] -> Encoding
toJSONList :: [WJCredentialOptionsRegistration] -> Value
$ctoJSONList :: [WJCredentialOptionsRegistration] -> Value
toEncoding :: WJCredentialOptionsRegistration -> Encoding
$ctoEncoding :: WJCredentialOptionsRegistration -> Encoding
toJSON :: WJCredentialOptionsRegistration -> Value
$ctoJSON :: WJCredentialOptionsRegistration -> Value
ToJSON)
newtype WJCredentialRegistration = WJCredentialRegistration
{ WJCredentialRegistration
-> PublicKeyCredential AuthenticatorAttestationResponse
unWJCredentialRegistration :: WJ.PublicKeyCredential WJ.AuthenticatorAttestationResponse
}
deriving newtype (Int -> WJCredentialRegistration -> ShowS
[WJCredentialRegistration] -> ShowS
WJCredentialRegistration -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WJCredentialRegistration] -> ShowS
$cshowList :: [WJCredentialRegistration] -> ShowS
show :: WJCredentialRegistration -> String
$cshow :: WJCredentialRegistration -> String
showsPrec :: Int -> WJCredentialRegistration -> ShowS
$cshowsPrec :: Int -> WJCredentialRegistration -> ShowS
Show, WJCredentialRegistration -> WJCredentialRegistration -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WJCredentialRegistration -> WJCredentialRegistration -> Bool
$c/= :: WJCredentialRegistration -> WJCredentialRegistration -> Bool
== :: WJCredentialRegistration -> WJCredentialRegistration -> Bool
$c== :: WJCredentialRegistration -> WJCredentialRegistration -> Bool
Eq, Value -> Parser [WJCredentialRegistration]
Value -> Parser WJCredentialRegistration
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [WJCredentialRegistration]
$cparseJSONList :: Value -> Parser [WJCredentialRegistration]
parseJSON :: Value -> Parser WJCredentialRegistration
$cparseJSON :: Value -> Parser WJCredentialRegistration
FromJSON, [WJCredentialRegistration] -> Encoding
[WJCredentialRegistration] -> Value
WJCredentialRegistration -> Encoding
WJCredentialRegistration -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [WJCredentialRegistration] -> Encoding
$ctoEncodingList :: [WJCredentialRegistration] -> Encoding
toJSONList :: [WJCredentialRegistration] -> Value
$ctoJSONList :: [WJCredentialRegistration] -> Value
toEncoding :: WJCredentialRegistration -> Encoding
$ctoEncoding :: WJCredentialRegistration -> Encoding
toJSON :: WJCredentialRegistration -> Value
$ctoJSON :: WJCredentialRegistration -> Value
ToJSON)
wjDecodeCredentialRegistration' ::
T.SupportedAttestationStatementFormats ->
WJCredentialRegistration ->
Either Text (T.Credential 'K.Registration 'True)
wjDecodeCredentialRegistration' :: SupportedAttestationStatementFormats
-> WJCredentialRegistration
-> Either Text (Credential 'Registration 'True)
wjDecodeCredentialRegistration' SupportedAttestationStatementFormats
supportedFormats =
forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
`runReaderT` SupportedAttestationStatementFormats
supportedFormats) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
(Decode m a, MonadError Text m) =>
JSON a -> m a
WJ.decode forall b c a. (b -> c) -> (a -> b) -> a -> c
. WJCredentialRegistration
-> PublicKeyCredential AuthenticatorAttestationResponse
unWJCredentialRegistration
wjDecodeCredentialRegistration ::
WJCredentialRegistration ->
Either Text (T.Credential 'K.Registration 'True)
wjDecodeCredentialRegistration :: WJCredentialRegistration
-> Either Text (Credential 'Registration 'True)
wjDecodeCredentialRegistration = SupportedAttestationStatementFormats
-> WJCredentialRegistration
-> Either Text (Credential 'Registration 'True)
wjDecodeCredentialRegistration' SupportedAttestationStatementFormats
allSupportedFormats
wjEncodeCredentialOptionsAuthentication ::
T.CredentialOptions 'K.Authentication ->
WJCredentialOptionsAuthentication
wjEncodeCredentialOptionsAuthentication :: CredentialOptions 'Authentication
-> WJCredentialOptionsAuthentication
wjEncodeCredentialOptionsAuthentication = PublicKeyCredentialRequestOptions
-> WJCredentialOptionsAuthentication
WJCredentialOptionsAuthentication forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Encode a => a -> JSON a
WJ.encode
newtype WJCredentialOptionsAuthentication = WJCredentialOptionsAuthentication
{ WJCredentialOptionsAuthentication
-> PublicKeyCredentialRequestOptions
_unWJCredentialOptionsAuthentication :: WJ.PublicKeyCredentialRequestOptions
}
deriving newtype (Int -> WJCredentialOptionsAuthentication -> ShowS
[WJCredentialOptionsAuthentication] -> ShowS
WJCredentialOptionsAuthentication -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WJCredentialOptionsAuthentication] -> ShowS
$cshowList :: [WJCredentialOptionsAuthentication] -> ShowS
show :: WJCredentialOptionsAuthentication -> String
$cshow :: WJCredentialOptionsAuthentication -> String
showsPrec :: Int -> WJCredentialOptionsAuthentication -> ShowS
$cshowsPrec :: Int -> WJCredentialOptionsAuthentication -> ShowS
Show, WJCredentialOptionsAuthentication
-> WJCredentialOptionsAuthentication -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WJCredentialOptionsAuthentication
-> WJCredentialOptionsAuthentication -> Bool
$c/= :: WJCredentialOptionsAuthentication
-> WJCredentialOptionsAuthentication -> Bool
== :: WJCredentialOptionsAuthentication
-> WJCredentialOptionsAuthentication -> Bool
$c== :: WJCredentialOptionsAuthentication
-> WJCredentialOptionsAuthentication -> Bool
Eq, Value -> Parser [WJCredentialOptionsAuthentication]
Value -> Parser WJCredentialOptionsAuthentication
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [WJCredentialOptionsAuthentication]
$cparseJSONList :: Value -> Parser [WJCredentialOptionsAuthentication]
parseJSON :: Value -> Parser WJCredentialOptionsAuthentication
$cparseJSON :: Value -> Parser WJCredentialOptionsAuthentication
FromJSON, [WJCredentialOptionsAuthentication] -> Encoding
[WJCredentialOptionsAuthentication] -> Value
WJCredentialOptionsAuthentication -> Encoding
WJCredentialOptionsAuthentication -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [WJCredentialOptionsAuthentication] -> Encoding
$ctoEncodingList :: [WJCredentialOptionsAuthentication] -> Encoding
toJSONList :: [WJCredentialOptionsAuthentication] -> Value
$ctoJSONList :: [WJCredentialOptionsAuthentication] -> Value
toEncoding :: WJCredentialOptionsAuthentication -> Encoding
$ctoEncoding :: WJCredentialOptionsAuthentication -> Encoding
toJSON :: WJCredentialOptionsAuthentication -> Value
$ctoJSON :: WJCredentialOptionsAuthentication -> Value
ToJSON)
newtype WJCredentialAuthentication = WJCredentialAuthentication
{ WJCredentialAuthentication
-> PublicKeyCredential AuthenticatorAssertionResponse
unWJCredentialAuthentication :: WJ.PublicKeyCredential WJ.AuthenticatorAssertionResponse
}
deriving newtype (Int -> WJCredentialAuthentication -> ShowS
[WJCredentialAuthentication] -> ShowS
WJCredentialAuthentication -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WJCredentialAuthentication] -> ShowS
$cshowList :: [WJCredentialAuthentication] -> ShowS
show :: WJCredentialAuthentication -> String
$cshow :: WJCredentialAuthentication -> String
showsPrec :: Int -> WJCredentialAuthentication -> ShowS
$cshowsPrec :: Int -> WJCredentialAuthentication -> ShowS
Show, WJCredentialAuthentication -> WJCredentialAuthentication -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WJCredentialAuthentication -> WJCredentialAuthentication -> Bool
$c/= :: WJCredentialAuthentication -> WJCredentialAuthentication -> Bool
== :: WJCredentialAuthentication -> WJCredentialAuthentication -> Bool
$c== :: WJCredentialAuthentication -> WJCredentialAuthentication -> Bool
Eq, Value -> Parser [WJCredentialAuthentication]
Value -> Parser WJCredentialAuthentication
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [WJCredentialAuthentication]
$cparseJSONList :: Value -> Parser [WJCredentialAuthentication]
parseJSON :: Value -> Parser WJCredentialAuthentication
$cparseJSON :: Value -> Parser WJCredentialAuthentication
FromJSON, [WJCredentialAuthentication] -> Encoding
[WJCredentialAuthentication] -> Value
WJCredentialAuthentication -> Encoding
WJCredentialAuthentication -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [WJCredentialAuthentication] -> Encoding
$ctoEncodingList :: [WJCredentialAuthentication] -> Encoding
toJSONList :: [WJCredentialAuthentication] -> Value
$ctoJSONList :: [WJCredentialAuthentication] -> Value
toEncoding :: WJCredentialAuthentication -> Encoding
$ctoEncoding :: WJCredentialAuthentication -> Encoding
toJSON :: WJCredentialAuthentication -> Value
$ctoJSON :: WJCredentialAuthentication -> Value
ToJSON)
wjDecodeCredentialAuthentication ::
WJCredentialAuthentication ->
Either Text (T.Credential 'K.Authentication 'True)
wjDecodeCredentialAuthentication :: WJCredentialAuthentication
-> Either Text (Credential 'Authentication 'True)
wjDecodeCredentialAuthentication =
forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
(Decode m a, MonadError Text m) =>
JSON a -> m a
WJ.decode forall b c a. (b -> c) -> (a -> b) -> a -> c
. WJCredentialAuthentication
-> PublicKeyCredential AuthenticatorAssertionResponse
unWJCredentialAuthentication