Skip to content

Commit

Permalink
prefix field names of User
Browse files Browse the repository at this point in the history
  • Loading branch information
fumieval committed Feb 23, 2021
1 parent c3609e5 commit abe6db5
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/WebAuthn/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import qualified Data.ByteString.Base64.URL as Base64
import Data.ByteString.Base16 as Base16 (decodeLenient, encode )
import qualified Data.Hashable as H
import qualified Data.Map as Map
import Data.List (stripPrefix)
import Data.Text (Text)
import Data.Text.Encoding ( decodeUtf8, encodeUtf8 )
import qualified Data.Text as T
Expand All @@ -80,6 +81,7 @@ import Data.ByteArray (ByteArrayAccess)
import Data.Aeson (SumEncoding(UntaggedValue))
import Data.List.NonEmpty
import Data.Aeson (genericToJSON)
import qualified Data.Aeson as Aeson

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

Expand Down Expand Up @@ -234,15 +236,22 @@ instance J.FromJSON AttestedCredentialData
instance J.ToJSON AttestedCredentialData

-- | 5.4.3. User Account Parameters for Credential Generation
data User = User {
id :: Base64ByteString
, name :: Maybe T.Text
, displayName :: Maybe T.Text
data User = User
{ userId :: Base64ByteString
, userName :: Maybe T.Text
, userDisplayName :: Maybe T.Text
} deriving (Generic, Show, Eq)

userJSONOptions :: Aeson.Options
userJSONOptions = defaultOptions
{ omitNothingFields = True
, fieldLabelModifier = \str -> case stripPrefix "user" str of
Just (c : cs) -> toLower c : cs
_ -> error "impossible" }

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

instance CBOR.Serialise User where
encode (User i n d) = CBOR.encode $ Map.fromList
Expand Down

0 comments on commit abe6db5

Please sign in to comment.