{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RecordWildCards #-}

-- | Stability: experimental
-- This module contains functions to further decode
-- [FIDO Metadata Statement](https://fidoalliance.org/specs/mds/fido-metadata-statement-v3.0-ps-20210518.html)
-- IDL types defined in 'Crypto.WebAuthn.Metadata.Statement.IDL' into the Haskell-specific types defined in 'Crypto.WebAuthn.Metadata.Statement.Types'
module Crypto.WebAuthn.Metadata.Statement.Decode
  ( decodeMetadataStatement,
    decodeAAGUID,
    decodeSubjectKeyIdentifier,
    decodeCertificate,
  )
where

import Control.Monad (unless)
import Crypto.Hash (SHA1, digestFromByteString)
import qualified Crypto.WebAuthn.Metadata.FidoRegistry as Registry
import Crypto.WebAuthn.Metadata.Statement.Types (WebauthnAttestationType (WebauthnAttestationAttCA, WebauthnAttestationBasic))
import qualified Crypto.WebAuthn.Metadata.Statement.Types as StatementTypes
import qualified Crypto.WebAuthn.Metadata.Statement.WebIDL as StatementIDL
import qualified Crypto.WebAuthn.Metadata.WebIDL as IDL
import qualified Crypto.WebAuthn.Model as M
import Crypto.WebAuthn.Model.Identifier (AAGUID (AAGUID), AuthenticatorIdentifier (AuthenticatorIdentifierFido2, AuthenticatorIdentifierFidoU2F), SubjectKeyIdentifier (SubjectKeyIdentifier))
import Data.Bifunctor (first)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Base64 as Base64
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NE
import Data.Maybe (mapMaybe)
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Text.Encoding (encodeUtf8)
import qualified Data.UUID as UUID
import qualified Data.X509 as X509

-- | Decodes an 'M.AAGUID' from an [aaguid](https://fidoalliance.org/specs/mds/fido-metadata-statement-v3.0-ps-20210518.html#dom-metadatastatement-aaguid) field of a metadata statement or an [aaguid](https://fidoalliance.org/specs/mds/fido-metadata-service-v3.0-ps-20210518.html#dom-metadatablobpayloadentry-aaguid) field of a metadata service payload entry field
decodeAAGUID :: StatementIDL.AAGUID -> Either Text (AuthenticatorIdentifier 'M.Fido2)
decodeAAGUID :: AAGUID -> Either Text (AuthenticatorIdentifier 'Fido2)
decodeAAGUID (StatementIDL.AAGUID Text
aaguidText) = case Text -> Maybe UUID
UUID.fromText Text
aaguidText of
  Maybe UUID
Nothing -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"Could not decode metadata aaguid: " forall a. Semigroup a => a -> a -> a
<> Text
aaguidText
  Just UUID
aaguid -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ AAGUID -> AuthenticatorIdentifier 'Fido2
AuthenticatorIdentifierFido2 forall a b. (a -> b) -> a -> b
$ UUID -> AAGUID
AAGUID UUID
aaguid

-- | Decodes a 'M.SubjectKeyIdentifier' from an [attestationCertificateKeyIdentifiers](https://fidoalliance.org/specs/mds/fido-metadata-statement-v3.0-ps-20210518.html#dom-metadatastatement-attestationcertificatekeyidentifiers) field of a metadata statement or an [attestationCertificateKeyIdentifiers](https://fidoalliance.org/specs/mds/fido-metadata-service-v3.0-ps-20210518.html#dom-metadatablobpayloadentry-attestationcertificatekeyidentifiers) field of a metadata service payload entry
decodeSubjectKeyIdentifier :: IDL.DOMString -> Either Text (AuthenticatorIdentifier 'M.FidoU2F)
decodeSubjectKeyIdentifier :: Text -> Either Text (AuthenticatorIdentifier 'FidoU2F)
decodeSubjectKeyIdentifier Text
subjectKeyIdentifierText = case ByteString -> Either String ByteString
Base16.decode (Text -> ByteString
encodeUtf8 Text
subjectKeyIdentifierText) of
  Left String
err -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"A attestationCertificateKeyIdentifier failed to parse because it's not a valid base-16 encoding: " forall a. Semigroup a => a -> a -> a
<> Text
subjectKeyIdentifierText forall a. Semigroup a => a -> a -> a
<> Text
", error: " forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack String
err
  Right ByteString
bytes -> case forall a ba.
(HashAlgorithm a, ByteArrayAccess ba) =>
ba -> Maybe (Digest a)
digestFromByteString @SHA1 ByteString
bytes of
    Maybe (Digest SHA1)
Nothing -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"A attestationCertificateKeyIdentifier failed to parse because it has the wrong length for a SHA1 hash: " forall a. Semigroup a => a -> a -> a
<> Text
subjectKeyIdentifierText
    Just Digest SHA1
hash -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ SubjectKeyIdentifier -> AuthenticatorIdentifier 'FidoU2F
AuthenticatorIdentifierFidoU2F forall a b. (a -> b) -> a -> b
$ Digest SHA1 -> SubjectKeyIdentifier
SubjectKeyIdentifier Digest SHA1
hash

-- | Decodes a 'X509.SignedCertificate' from an [attestationRootCertificates](https://fidoalliance.org/specs/mds/fido-metadata-statement-v3.0-ps-20210518.html#dom-metadatastatement-attestationrootcertificates) field of a metadata statement or the [certificate](https://fidoalliance.org/specs/mds/fido-metadata-service-v3.0-ps-20210518.html#dom-statusreport-certificate) field of a metadata service status report
decodeCertificate :: IDL.DOMString -> Either Text X509.SignedCertificate
decodeCertificate :: Text -> Either Text SignedCertificate
decodeCertificate Text
text =
  -- TODO: Remove Text.strip, it's only needed because of a spec violation, see
  -- <https://github.com/tweag/haskell-fido2/issues/68>
  -- TODO: Don't use decodeLenient, it's only needed because of a spec
  -- violation, see TODO above
  let bytes :: ByteString
bytes = ByteString -> ByteString
Base64.decodeLenient (Text -> ByteString
encodeUtf8 forall a b. (a -> b) -> a -> b
$ Text -> Text
Text.strip Text
text)
   in case ByteString -> Either String SignedCertificate
X509.decodeSignedCertificate ByteString
bytes of
        Left String
err -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"A certificate failed to parse because it's not a valid encoding: " forall a. Semigroup a => a -> a -> a
<> Text
text forall a. Semigroup a => a -> a -> a
<> Text
", error: " forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack String
err
        Right SignedCertificate
certificate -> forall a b. b -> Either a b
Right SignedCertificate
certificate

-- | Fully decodes a [MetadataStatement](https://fidoalliance.org/specs/mds/fido-metadata-statement-v3.0-ps-20210518.html#metadata-keys).
-- The @p@ type parameter is the 'StatementIDL.ProtocolFamily' that this metadata statement is for.
decodeMetadataStatement ::
  -- | The raw metadata statement, directly parsed from JSON
  StatementIDL.MetadataStatement ->
  -- | Either an early exit with 'Left', where @Left Nothing@ signals that
  -- this entry can be skipped because it's not relevant for Webauthn, and
  -- @Left . Just@ signals that an error happened during decoding
  -- Otherwise a successful result with 'Right'
  Either (Maybe Text) StatementTypes.MetadataStatement
decodeMetadataStatement :: MetadataStatement -> Either (Maybe Text) MetadataStatement
decodeMetadataStatement StatementIDL.MetadataStatement {[Text]
[TransactionConfirmationDisplayType]
Maybe Boolean
Maybe UnsignedShort
Maybe (NonEmpty KeyIdentifier)
Maybe (NonEmpty ExtensionDescriptor)
Maybe (NonEmpty EcdaaTrustAnchor)
Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
Maybe Text
Maybe AAID
Maybe AuthenticatorGetInfo
Maybe AlternativeDescriptions
Maybe AAGUID
UnsignedShort
UnsignedLong
NonEmpty Version
NonEmpty AuthenticatorAttestationType
NonEmpty PublicKeyRepresentationFormat
NonEmpty AuthenticationAlgorithm
NonEmpty AuthenticatorAttachmentHint
NonEmpty MatcherProtectionType
NonEmpty KeyProtectionType
NonEmpty VerificationMethodANDCombinations
Text
ProtocolFamily
$sel:authenticatorGetInfo:MetadataStatement :: MetadataStatement -> Maybe AuthenticatorGetInfo
$sel:supportedExtensions:MetadataStatement :: MetadataStatement -> Maybe (NonEmpty ExtensionDescriptor)
$sel:icon:MetadataStatement :: MetadataStatement -> Maybe Text
$sel:ecdaaTrustAnchors:MetadataStatement :: MetadataStatement -> Maybe (NonEmpty EcdaaTrustAnchor)
$sel:attestationRootCertificates:MetadataStatement :: MetadataStatement -> [Text]
$sel:tcDisplayPNGCharacteristics:MetadataStatement :: MetadataStatement
-> Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
$sel:tcDisplayContentType:MetadataStatement :: MetadataStatement -> Maybe Text
$sel:tcDisplay:MetadataStatement :: MetadataStatement -> [TransactionConfirmationDisplayType]
$sel:attachmentHint:MetadataStatement :: MetadataStatement -> NonEmpty AuthenticatorAttachmentHint
$sel:cryptoStrength:MetadataStatement :: MetadataStatement -> Maybe UnsignedShort
$sel:matcherProtection:MetadataStatement :: MetadataStatement -> NonEmpty MatcherProtectionType
$sel:isFreshUserVerificationRequired:MetadataStatement :: MetadataStatement -> Maybe Boolean
$sel:isKeyRestricted:MetadataStatement :: MetadataStatement -> Maybe Boolean
$sel:keyProtection:MetadataStatement :: MetadataStatement -> NonEmpty KeyProtectionType
$sel:userVerificationDetails:MetadataStatement :: MetadataStatement -> NonEmpty VerificationMethodANDCombinations
$sel:attestationTypes:MetadataStatement :: MetadataStatement -> NonEmpty AuthenticatorAttestationType
$sel:publicKeyAlgAndEncodings:MetadataStatement :: MetadataStatement -> NonEmpty PublicKeyRepresentationFormat
$sel:authenticationAlgorithms:MetadataStatement :: MetadataStatement -> NonEmpty AuthenticationAlgorithm
$sel:upv:MetadataStatement :: MetadataStatement -> NonEmpty Version
$sel:schema:MetadataStatement :: MetadataStatement -> UnsignedShort
$sel:protocolFamily:MetadataStatement :: MetadataStatement -> ProtocolFamily
$sel:authenticatorVersion:MetadataStatement :: MetadataStatement -> UnsignedLong
$sel:alternativeDescriptions:MetadataStatement :: MetadataStatement -> Maybe AlternativeDescriptions
$sel:description:MetadataStatement :: MetadataStatement -> Text
$sel:attestationCertificateKeyIdentifiers:MetadataStatement :: MetadataStatement -> Maybe (NonEmpty KeyIdentifier)
$sel:aaguid:MetadataStatement :: MetadataStatement -> Maybe AAGUID
$sel:aaid:MetadataStatement :: MetadataStatement -> Maybe AAID
$sel:legalHeader:MetadataStatement :: MetadataStatement -> Text
authenticatorGetInfo :: Maybe AuthenticatorGetInfo
supportedExtensions :: Maybe (NonEmpty ExtensionDescriptor)
icon :: Maybe Text
ecdaaTrustAnchors :: Maybe (NonEmpty EcdaaTrustAnchor)
attestationRootCertificates :: [Text]
tcDisplayPNGCharacteristics :: Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
tcDisplayContentType :: Maybe Text
tcDisplay :: [TransactionConfirmationDisplayType]
attachmentHint :: NonEmpty AuthenticatorAttachmentHint
cryptoStrength :: Maybe UnsignedShort
matcherProtection :: NonEmpty MatcherProtectionType
isFreshUserVerificationRequired :: Maybe Boolean
isKeyRestricted :: Maybe Boolean
keyProtection :: NonEmpty KeyProtectionType
userVerificationDetails :: NonEmpty VerificationMethodANDCombinations
attestationTypes :: NonEmpty AuthenticatorAttestationType
publicKeyAlgAndEncodings :: NonEmpty PublicKeyRepresentationFormat
authenticationAlgorithms :: NonEmpty AuthenticationAlgorithm
upv :: NonEmpty Version
schema :: UnsignedShort
protocolFamily :: ProtocolFamily
authenticatorVersion :: UnsignedLong
alternativeDescriptions :: Maybe AlternativeDescriptions
description :: Text
attestationCertificateKeyIdentifiers :: Maybe (NonEmpty KeyIdentifier)
aaguid :: Maybe AAGUID
aaid :: Maybe AAID
legalHeader :: Text
..} = do
  let msLegalHeader :: Text
msLegalHeader = Text
legalHeader
      msDescription :: Text
msDescription = Text
description
      msAlternativeDescriptions :: Maybe AlternativeDescriptions
msAlternativeDescriptions = Maybe AlternativeDescriptions
alternativeDescriptions
      msAuthenticatorVersion :: UnsignedLong
msAuthenticatorVersion = UnsignedLong
authenticatorVersion
  forall (f :: * -> *). Applicative f => Boolean -> f () -> f ()
unless (UnsignedShort
schema forall a. Eq a => a -> a -> Boolean
== UnsignedShort
3) forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text
"Schema version is not 3 but " forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (forall a. Show a => a -> String
show UnsignedShort
schema)
  let msUpv :: NonEmpty Version
msUpv = NonEmpty Version
upv
      msAuthenticationAlgorithms :: NonEmpty AuthenticationAlgorithm
msAuthenticationAlgorithms = NonEmpty AuthenticationAlgorithm
authenticationAlgorithms
      msPublicKeyAlgAndEncodings :: NonEmpty PublicKeyRepresentationFormat
msPublicKeyAlgAndEncodings = NonEmpty PublicKeyRepresentationFormat
publicKeyAlgAndEncodings
  NonEmpty WebauthnAttestationType
msAttestationTypes <- NonEmpty AuthenticatorAttestationType
-> Either (Maybe Text) (NonEmpty WebauthnAttestationType)
decodeAttestationTypes NonEmpty AuthenticatorAttestationType
attestationTypes
  let msUserVerificationDetails :: NonEmpty VerificationMethodANDCombinations
msUserVerificationDetails = NonEmpty VerificationMethodANDCombinations
userVerificationDetails
      msKeyProtection :: NonEmpty KeyProtectionType
msKeyProtection = NonEmpty KeyProtectionType
keyProtection
      msIsKeyRestricted :: Maybe Boolean
msIsKeyRestricted = Maybe Boolean
isKeyRestricted
      msIsFreshUserVerificationRequired :: Maybe Boolean
msIsFreshUserVerificationRequired = Maybe Boolean
isFreshUserVerificationRequired
      msMatcherProtection :: NonEmpty MatcherProtectionType
msMatcherProtection = NonEmpty MatcherProtectionType
matcherProtection
      msCryptoStrength :: Maybe UnsignedShort
msCryptoStrength = Maybe UnsignedShort
cryptoStrength
      msAttachmentHint :: NonEmpty AuthenticatorAttachmentHint
msAttachmentHint = NonEmpty AuthenticatorAttachmentHint
attachmentHint
      msTcDisplay :: [TransactionConfirmationDisplayType]
msTcDisplay = [TransactionConfirmationDisplayType]
tcDisplay
      msTcDisplayContentType :: Maybe Text
msTcDisplayContentType = Maybe Text
tcDisplayContentType
      msTcDisplayPNGCharacteristics :: Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
msTcDisplayPNGCharacteristics = Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
tcDisplayPNGCharacteristics
  NonEmpty SignedCertificate
msAttestationRootCertificates <- case forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty [Text]
attestationRootCertificates of
    -- > When supporting surrogate basic attestation only, no attestation trust anchor is required/used. So this array MUST be empty in that case.
    -- This will never be the case, because if only surrogate basic attestation is used, then decodeAttestationTypes above will have returned (Left Nothing) already
    Maybe (NonEmpty Text)
Nothing -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just Text
"attestationRootCertificates should not be empty"
    Just NonEmpty Text
certs -> forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Text -> Either Text SignedCertificate
decodeCertificate NonEmpty Text
certs
  Maybe ByteString
msIcon <- forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Text -> Either Text ByteString
decodeIcon Maybe Text
icon
  let msSupportedExtensions :: Maybe (NonEmpty ExtensionDescriptor)
msSupportedExtensions = Maybe (NonEmpty ExtensionDescriptor)
supportedExtensions
      msAuthenticatorGetInfo :: Maybe AuthenticatorGetInfo
msAuthenticatorGetInfo = Maybe AuthenticatorGetInfo
authenticatorGetInfo
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ StatementTypes.MetadataStatement {[TransactionConfirmationDisplayType]
Maybe Boolean
Maybe UnsignedShort
Maybe (NonEmpty ExtensionDescriptor)
Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
Maybe ByteString
Maybe Text
Maybe AuthenticatorGetInfo
Maybe AlternativeDescriptions
UnsignedLong
NonEmpty SignedCertificate
NonEmpty Version
NonEmpty PublicKeyRepresentationFormat
NonEmpty AuthenticationAlgorithm
NonEmpty AuthenticatorAttachmentHint
NonEmpty MatcherProtectionType
NonEmpty KeyProtectionType
NonEmpty VerificationMethodANDCombinations
NonEmpty WebauthnAttestationType
Text
msAuthenticatorGetInfo :: Maybe AuthenticatorGetInfo
msSupportedExtensions :: Maybe (NonEmpty ExtensionDescriptor)
msIcon :: Maybe ByteString
msAttestationRootCertificates :: NonEmpty SignedCertificate
msTcDisplayPNGCharacteristics :: Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
msTcDisplayContentType :: Maybe Text
msTcDisplay :: [TransactionConfirmationDisplayType]
msAttachmentHint :: NonEmpty AuthenticatorAttachmentHint
msCryptoStrength :: Maybe UnsignedShort
msMatcherProtection :: NonEmpty MatcherProtectionType
msIsFreshUserVerificationRequired :: Maybe Boolean
msIsKeyRestricted :: Maybe Boolean
msKeyProtection :: NonEmpty KeyProtectionType
msUserVerificationDetails :: NonEmpty VerificationMethodANDCombinations
msAttestationTypes :: NonEmpty WebauthnAttestationType
msPublicKeyAlgAndEncodings :: NonEmpty PublicKeyRepresentationFormat
msAuthenticationAlgorithms :: NonEmpty AuthenticationAlgorithm
msUpv :: NonEmpty Version
msAuthenticatorVersion :: UnsignedLong
msAlternativeDescriptions :: Maybe AlternativeDescriptions
msDescription :: Text
msLegalHeader :: Text
msAuthenticatorGetInfo :: Maybe AuthenticatorGetInfo
msSupportedExtensions :: Maybe (NonEmpty ExtensionDescriptor)
msIcon :: Maybe ByteString
msAttestationRootCertificates :: NonEmpty SignedCertificate
msTcDisplayPNGCharacteristics :: Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
msTcDisplayContentType :: Maybe Text
msTcDisplay :: [TransactionConfirmationDisplayType]
msAttachmentHint :: NonEmpty AuthenticatorAttachmentHint
msCryptoStrength :: Maybe UnsignedShort
msMatcherProtection :: NonEmpty MatcherProtectionType
msIsFreshUserVerificationRequired :: Maybe Boolean
msIsKeyRestricted :: Maybe Boolean
msKeyProtection :: NonEmpty KeyProtectionType
msUserVerificationDetails :: NonEmpty VerificationMethodANDCombinations
msAttestationTypes :: NonEmpty WebauthnAttestationType
msPublicKeyAlgAndEncodings :: NonEmpty PublicKeyRepresentationFormat
msAuthenticationAlgorithms :: NonEmpty AuthenticationAlgorithm
msUpv :: NonEmpty Version
msAuthenticatorVersion :: UnsignedLong
msAlternativeDescriptions :: Maybe AlternativeDescriptions
msDescription :: Text
msLegalHeader :: Text
..}
  where
    -- Turns a non-empty list of 'Registry.AuthenticatorAttestationType' into a non-empty list of 'WebauthnAttestationType'.
    -- If the authenticator doesn't support any webauthn attestation types,
    -- `Left Nothing` is returned, indicating that this authenticator should be ignored
    decodeAttestationTypes ::
      NonEmpty Registry.AuthenticatorAttestationType ->
      Either (Maybe Text) (NonEmpty WebauthnAttestationType)
    decodeAttestationTypes :: NonEmpty AuthenticatorAttestationType
-> Either (Maybe Text) (NonEmpty WebauthnAttestationType)
decodeAttestationTypes NonEmpty AuthenticatorAttestationType
types = case forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe AuthenticatorAttestationType -> Maybe WebauthnAttestationType
transform forall a b. (a -> b) -> a -> b
$ forall a. NonEmpty a -> [a]
NE.toList NonEmpty AuthenticatorAttestationType
types of
      Maybe (NonEmpty WebauthnAttestationType)
Nothing -> forall a b. a -> Either a b
Left forall a. Maybe a
Nothing
      Just NonEmpty WebauthnAttestationType
result -> forall a b. b -> Either a b
Right NonEmpty WebauthnAttestationType
result
      where
        transform :: Registry.AuthenticatorAttestationType -> Maybe WebauthnAttestationType
        transform :: AuthenticatorAttestationType -> Maybe WebauthnAttestationType
transform AuthenticatorAttestationType
Registry.ATTESTATION_BASIC_FULL = forall a. a -> Maybe a
Just WebauthnAttestationType
WebauthnAttestationBasic
        transform AuthenticatorAttestationType
Registry.ATTESTATION_ATTCA = forall a. a -> Maybe a
Just WebauthnAttestationType
WebauthnAttestationAttCA
        transform AuthenticatorAttestationType
_ = forall a. Maybe a
Nothing

    -- Decodes the PNG bytes of an [icon](https://fidoalliance.org/specs/mds/fido-metadata-statement-v3.0-ps-20210518.html#dom-metadatastatement-icon) field of a metadata statement
    decodeIcon :: IDL.DOMString -> Either Text BS.ByteString
    decodeIcon :: Text -> Either Text ByteString
decodeIcon Text
dataUrl = case Text -> Text -> Maybe Text
Text.stripPrefix Text
"data:image/png;base64," Text
dataUrl of
      Maybe Text
Nothing -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"Icon decoding failed because there is no \"data:image/png;base64,\" prefix: " forall a. Semigroup a => a -> a -> a
<> Text
dataUrl
      Just Text
suffix ->
        -- TODO: Use non-lenient decoding, it's only needed because of a spec violation,
        -- see <https://github.com/tweag/haskell-fido2/issues/68>
        forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
Base64.decodeLenient (Text -> ByteString
encodeUtf8 Text
suffix)