Skip to content

Commit

Permalink
webauthn: prefix some record field names
Browse files Browse the repository at this point in the history
It's better to avoid duplicate fields until NoFieldSelector
  • Loading branch information
fumieval committed Feb 23, 2021
1 parent abe6db5 commit 6937216
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/WebAuthn.hs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ registerCredential opts cs challenge (RelyingParty rpOrigin rpId _ _) tbi verifi
case pubKey of
Just k -> do
parsedPubKey <- either throwE return $ parsePublicKey k
let hasProperAlg pubKeyParam = hasMatchingAlg parsedPubKey $ alg (pubKeyParam :: PubKeyCredParam)
let hasProperAlg pubKeyParam = hasMatchingAlg parsedPubKey $ pkcpAlg (pubKeyParam :: PubKeyCredParam)
when (not . any hasProperAlg $ pubKeyCredParams opts) $ throwE MalformedAuthenticatorData
return $ Just parsedPubKey
-- non present public key will fail anyway or the fmt == 'none'
Expand Down
54 changes: 24 additions & 30 deletions src/WebAuthn/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE DerivingVia #-}
module WebAuthn.Types (
-- * Relying party
RelyingParty(..)
Expand Down Expand Up @@ -82,6 +84,7 @@ import Data.Aeson (SumEncoding(UntaggedValue))
import Data.List.NonEmpty
import Data.Aeson (genericToJSON)
import qualified Data.Aeson as Aeson
import Deriving.Aeson

newtype Base64ByteString = Base64ByteString { unBase64ByteString :: ByteString } deriving (Generic, Show, Eq, ByteArrayAccess)

Expand Down Expand Up @@ -240,7 +243,7 @@ data User = User
{ userId :: Base64ByteString
, userName :: Maybe T.Text
, userDisplayName :: Maybe T.Text
} deriving (Generic, Show, Eq)
} deriving (Generic, Show, Eq)

userJSONOptions :: Aeson.Options
userJSONOptions = defaultOptions
Expand Down Expand Up @@ -294,28 +297,28 @@ data VerificationFailure
| NonceCheckFailure
deriving Show

data AndroidSafetyNet = AndroidSafetyNet {
timestampMs :: Integer
data AndroidSafetyNet = AndroidSafetyNet
{ timestampMs :: Integer
, nonce :: [Char]
, apkPackageName :: Text
, apkCertificateDigestSha256 :: [Text]
, ctsProfileMatch :: Bool
, basicIntegrity :: Bool
} deriving (Show, Generic)
} deriving (Show, Generic)

instance FromJSON AndroidSafetyNet

data StmtSafetyNet = StmtSafetyNet {
header :: Base64ByteString
data StmtSafetyNet = StmtSafetyNet
{ header :: Base64ByteString
, payload :: Base64ByteString
, signature :: ByteString
, certificates :: X509.CertificateChain
} deriving Show
} deriving Show

data JWTHeader = JWTHeader {
alg :: Text
data JWTHeader = JWTHeader
{ alg :: Text
, x5c :: [Text]
} deriving (Show, Generic)
} deriving (Show, Generic)

instance FromJSON JWTHeader

Expand All @@ -334,18 +337,12 @@ instance ToJSON AuthenticatorTransport where
toEncoding = genericToEncoding defaultOptions { sumEncoding = UntaggedValue, constructorTagModifier = fmap toLower }
toJSON = genericToJSON defaultOptions { sumEncoding = UntaggedValue, constructorTagModifier = fmap toLower }

data PublicKeyCredentialDescriptor = PublicKeyCredentialDescriptor {
tipe :: PublicKeyCredentialType
, id :: Base64ByteString
, transports :: Maybe (NonEmpty AuthenticatorTransport)
} deriving (Eq, Show, Generic)

instance ToJSON PublicKeyCredentialDescriptor where
toEncoding = genericToEncoding defaultOptions { omitNothingFields = True, fieldLabelModifier = mapTipe}
toJSON = genericToJSON defaultOptions { omitNothingFields = True, fieldLabelModifier = mapTipe}

mapTipe :: String -> String
mapTipe str = if str == "tipe" then "type" else str
data PublicKeyCredentialDescriptor = PublicKeyCredentialDescriptor
{ pkcdType :: PublicKeyCredentialType
, pkcdId :: Base64ByteString
, pkcdTransports :: Maybe (NonEmpty AuthenticatorTransport)
} deriving (Eq, Show, Generic)
deriving ToJSON via CustomJSON '[FieldLabelModifier (StripPrefix "pkcd", CamelToSnake), OmitNothingFields] PublicKeyCredentialDescriptor

data UserVerification = Required | Preferred | Discouraged deriving (Show, Eq, Generic)

Expand Down Expand Up @@ -382,14 +379,11 @@ pubKeyCredAlgFromInt = \case -7 -> Just ES256
-37 -> Just PS256
_ -> Nothing

data PubKeyCredParam = PubKeyCredParam {
tipe :: PublicKeyCredentialType
, alg :: PubKeyCredAlg
} deriving (Show, Eq, Generic)

instance ToJSON PubKeyCredParam where
toEncoding = genericToEncoding defaultOptions { omitNothingFields = True, fieldLabelModifier = mapTipe}
toJSON = genericToJSON defaultOptions { omitNothingFields = True, fieldLabelModifier = mapTipe}
data PubKeyCredParam = PubKeyCredParam
{ pkcpType :: PublicKeyCredentialType
, pkcpAlg :: PubKeyCredAlg
} deriving (Show, Eq, Generic)
deriving ToJSON via CustomJSON '[FieldLabelModifier (StripPrefix "pkcp", CamelToSnake)] PubKeyCredParam

data Attestation = None | Direct | Indirect deriving (Eq, Show, Generic)

Expand Down
1 change: 1 addition & 0 deletions webauthn.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ library
, base16-bytestring >=1.0 && <1.1
, cborg >=0.2 && <0.3
, cereal >=0.5 && <0.6
, deriving-aeson ^>= 0.2
, containers >=0.6 && <0.7
, cryptonite >=0.28 && <0.29
, hashable >=1.3 && <1.4
Expand Down

0 comments on commit 6937216

Please sign in to comment.