const signChallenge = async (challenge: UserActionSignatureChallenge) : Promise<SignedChallenge> => {
// The data being signed includes information that is important for validating the request originated from a valid location.
const clientData: Buffer = Buffer.from(
JSON.stringify({
type: 'key.get',
challenge: challenge.challenge,
origin: origin,
crossOrigin: false,
} as ClientData)
)
// Signing can be done locally or by calling an external signer (like AWS KMS).
const signature = crypto.sign(
undefined,
clientData,
apiKeyPrivateKey
)
// Pass back the signature, and the data that was signed so both can be parsed and validated properly.
return {
clientData: clientData.toString('base64url'),
credId: challenge.allowCredentials.key[0].id,
signature: signature.toString('base64url'),
}
}