Skip to content

Commit

Permalink
Fixed ToJSON instances
Browse files Browse the repository at this point in the history
  • Loading branch information
sumo committed Nov 15, 2020
1 parent 4b8f0a3 commit 21f1498
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/Web/WebAuthn/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Web.WebAuthn.Types (
, Attestation (..)
, Extensions (..)
, AuthenticatorSelection (..)
, UserVerification (..)
, PubKeyCredAlg (..)
) where

Expand Down Expand Up @@ -76,6 +77,7 @@ import Data.Char ( toLower, toUpper )
import Data.ByteArray (ByteArrayAccess)
import Data.Aeson (SumEncoding(UntaggedValue))
import Data.List.NonEmpty
import Data.Aeson (genericToJSON)

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

Expand Down Expand Up @@ -217,6 +219,7 @@ data CredentialData = CredentialData
} deriving (Show, Eq, Generic)

instance J.FromJSON CredentialData

instance J.ToJSON CredentialData

data User = User
Expand All @@ -226,6 +229,7 @@ data User = User
} deriving (Generic, Show, Eq)

instance ToJSON User where
toJSON = genericToJSON defaultOptions { omitNothingFields = True}
toEncoding = genericToEncoding defaultOptions { omitNothingFields = True}

instance CBOR.Serialise User where
Expand Down Expand Up @@ -305,6 +309,7 @@ data AuthenticatorTransport = USB -- usb

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
Expand All @@ -314,6 +319,7 @@ data PublicKeyCredentialDescriptor = PublicKeyCredentialDescriptor {

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
Expand All @@ -322,6 +328,7 @@ data UserVerification = Required | Preferred | Discouraged deriving (Show, Eq, G

instance ToJSON UserVerification where
toEncoding = genericToEncoding defaultOptions { sumEncoding = UntaggedValue, constructorTagModifier = fmap toLower }
toJSON = genericToJSON defaultOptions { sumEncoding = UntaggedValue, constructorTagModifier = fmap toLower }

data PublicKeyCredentialRequestOptions = PublicKeyCredentialRequestOptions {
challenge :: Base64ByteString
Expand All @@ -334,6 +341,7 @@ data PublicKeyCredentialRequestOptions = PublicKeyCredentialRequestOptions {

instance ToJSON PublicKeyCredentialRequestOptions where
toEncoding = genericToEncoding defaultOptions { omitNothingFields = True}
toJSON = genericToJSON defaultOptions { omitNothingFields = True}

data PubKeyCredAlg = ES256 -- -7
| RS256 -- (-257)
Expand All @@ -352,16 +360,19 @@ data PubKeyCredParam = PubKeyCredParam {

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

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

instance ToJSON Attestation where
toEncoding = genericToEncoding defaultOptions { sumEncoding = UntaggedValue, constructorTagModifier = fmap toLower }
toJSON = genericToJSON defaultOptions { sumEncoding = UntaggedValue, constructorTagModifier = fmap toLower }

newtype AuthnSel = AuthnSel [Base64ByteString] deriving (Show, Eq, Generic)

instance ToJSON AuthnSel where
toEncoding = genericToEncoding defaultOptions { unwrapUnaryRecords = True }
toJSON = genericToJSON defaultOptions { unwrapUnaryRecords = True }

data BiometricPerfBounds = BiometricPerfBounds {
far :: Double
Expand All @@ -370,6 +381,7 @@ data BiometricPerfBounds = BiometricPerfBounds {

instance ToJSON BiometricPerfBounds where
toEncoding = genericToEncoding defaultOptions { fieldLabelModifier = fmap toUpper }
toJSON = genericToJSON defaultOptions { fieldLabelModifier = fmap toUpper }

data Extensions = Extensions {
uvi :: Bool
Expand All @@ -382,6 +394,7 @@ data Extensions = Extensions {

instance ToJSON Extensions where
toEncoding = genericToEncoding defaultOptions { omitNothingFields = True }
toJSON = genericToJSON defaultOptions { omitNothingFields = True }

data AuthenticatorAttachment = Platform | CrossPlatform deriving (Eq, Show)

Expand All @@ -397,6 +410,7 @@ data AuthenticatorSelection = AuthenticatorSelection {

instance ToJSON AuthenticatorSelection where
toEncoding = genericToEncoding defaultOptions { omitNothingFields = True }
toJSON = genericToJSON defaultOptions { omitNothingFields = True }

data PublicKeyCredentialCreationOptions = PublicKeyCredentialCreationOptions {
rp :: RelyingParty
Expand All @@ -412,4 +426,5 @@ data PublicKeyCredentialCreationOptions = PublicKeyCredentialCreationOptions {

instance ToJSON PublicKeyCredentialCreationOptions where
toEncoding = genericToEncoding defaultOptions { omitNothingFields = True }
toJSON = genericToJSON defaultOptions { omitNothingFields = True }

8 changes: 5 additions & 3 deletions test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Test.Tasty ( defaultMain, testGroup, TestTree )
import Test.Tasty.HUnit (assertEqual, assertBool, testCaseSteps )
import Data.String.Interpolate ()
import Data.ByteString.Base64.URL as BS (decodeLenient)
import Data.Aeson as A (toJSON, eitherDecode, FromJSON)
import Data.Aeson as A (toEncoding, toJSON, eitherDecode, FromJSON)
import URI.ByteString ()
import Data.X509.CertificateStore ( readCertificateStore )
import Data.ByteString ( ByteString )
Expand All @@ -27,6 +27,7 @@ import Web.WebAuthn.Types
defaultRelyingParty )
import Data.Aeson.QQ.Simple ( aesonQQ )
import Data.List.NonEmpty ( NonEmpty((:|)) )
import Data.Aeson.Encoding (value)

main :: IO ()
main = defaultMain tests
Expand All @@ -38,6 +39,7 @@ androidTests :: TestTree
androidTests = testGroup "WebAuthn Tests"
[
androidCredentialTest
, registrationTest
]

androidCredentialTest :: TestTree
Expand All @@ -56,7 +58,7 @@ registrationTest = testCaseSteps "Credentials Test" $ \step -> do
step "Credential creation"
let pkcco = PublicKeyCredentialCreationOptions (defaultRelyingParty (Origin "https" "webauthn.biz" Nothing)) (Base64ByteString "12343434") (User (Base64ByteString "id") Nothing Nothing) (PubKeyCredParam PublicKey ES256 :| []) Nothing Nothing Nothing Nothing (Just (PublicKeyCredentialDescriptor PublicKey (Base64ByteString "1234") (Just (BLE :| [])) :| []))
let ref = [aesonQQ| {
"rp":{"id":"matrixpay.biz"},
"rp":{"id":"webauthn.biz"},
"challenge":"MTIzNDM0MzQ=",
"user":{"id":"aWQ="},
"pubKeyCredParams":[
Expand All @@ -65,7 +67,7 @@ registrationTest = testCaseSteps "Credentials Test" $ \step -> do
"alg":-7
}],
"excludeCredentials":[
{"type":"public-key", "id": "MTIzNA==", "transports ":["ble"]}
{"type":"public-key", "id": "MTIzNA==", "transports":["ble"]}
]
}
|]
Expand Down

0 comments on commit 21f1498

Please sign in to comment.