|
|
@ -3,6 +3,7 @@ import { Buffer } from "buffer/"; |
|
|
|
import { decode as cborDecode } from "cbor-x"; |
|
|
|
import { createJWS, JWTPayload, verifyJWT } from "did-jwt"; |
|
|
|
import { DIDResolutionResult, Resolver } from "did-resolver"; |
|
|
|
import { sha256 } from "ethereum-cryptography/sha256.js"; |
|
|
|
import { bytesToMultibase } from "@veramo/utils"; |
|
|
|
import { |
|
|
|
startAuthentication, |
|
|
@ -22,6 +23,8 @@ import { |
|
|
|
PublicKeyCredentialRequestOptionsJSON, |
|
|
|
} from "@simplewebauthn/types"; |
|
|
|
|
|
|
|
import { generateRandomBytes } from "@/libs/crypto"; |
|
|
|
|
|
|
|
export interface JWK { |
|
|
|
kty: string; |
|
|
|
crv: string; |
|
|
@ -33,15 +36,15 @@ export interface PublicKeyCredential { |
|
|
|
jwt: JWK; |
|
|
|
} |
|
|
|
|
|
|
|
function toBase64Url(anything: Uint8Array) { |
|
|
|
return Buffer.from(anything) |
|
|
|
.toString("base64") |
|
|
|
.replace(/\+/g, "-") |
|
|
|
.replace(/\//g, "_") |
|
|
|
.replace(/=+$/, ""); |
|
|
|
function toBase64Url(anythingB64: string) { |
|
|
|
return anythingB64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); |
|
|
|
} |
|
|
|
|
|
|
|
export async function registerCredential() { |
|
|
|
function arrayToBase64Url(anything: Uint8Array) { |
|
|
|
return toBase64Url(Buffer.from(anything).toString("base64")); |
|
|
|
} |
|
|
|
|
|
|
|
export async function registerCredential(userId: Uint8Array) { |
|
|
|
const options: PublicKeyCredentialCreationOptionsJSON = |
|
|
|
await generateRegistrationOptions({ |
|
|
|
rpName: "Time Safari", |
|
|
@ -85,10 +88,8 @@ export async function registerCredential() { |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
export async function registerCredential2( |
|
|
|
userId: Uint8Array, |
|
|
|
challenge: Uint8Array, |
|
|
|
) { |
|
|
|
export async function registerCredential2(userId: Uint8Array) { |
|
|
|
const challenge = generateRandomBytes(32); |
|
|
|
const publicKeyOptions: PublicKeyCredentialCreationOptions = { |
|
|
|
challenge: challenge, |
|
|
|
rp: { |
|
|
@ -120,20 +121,20 @@ export async function registerCredential2( |
|
|
|
|
|
|
|
console.log("credential", credential); |
|
|
|
console.log(credential?.id, " is the new ID base64-url-encoded"); |
|
|
|
console.log(toBase64Url(credential?.rawId), " is the base64 rawId"); |
|
|
|
console.log(arrayToBase64Url(credential?.rawId), " is the base64 rawId"); |
|
|
|
const attestationResponse = credential?.response; |
|
|
|
const verfInput: VerifyRegistrationResponseOpts = { |
|
|
|
response: { |
|
|
|
id: credential?.id as string, |
|
|
|
rawId: credential?.id as string, //Buffer.from(credential?.rawId).toString("base64"),
|
|
|
|
response: { |
|
|
|
attestationObject: toBase64Url(attestationResponse?.attestationObject), |
|
|
|
clientDataJSON: toBase64Url(attestationResponse?.clientDataJSON), |
|
|
|
attestationObject: arrayToBase64Url(attestationResponse?.attestationObject), |
|
|
|
clientDataJSON: arrayToBase64Url(attestationResponse?.clientDataJSON), |
|
|
|
}, |
|
|
|
clientExtensionResults: {}, |
|
|
|
type: "public-key", |
|
|
|
}, |
|
|
|
expectedChallenge: toBase64Url(challenge), |
|
|
|
expectedChallenge: arrayToBase64Url(challenge), |
|
|
|
expectedOrigin: window.location.origin, |
|
|
|
}; |
|
|
|
console.log("verfInput", verfInput); |
|
|
@ -169,8 +170,8 @@ export async function registerCredential2( |
|
|
|
alg: "ES256", |
|
|
|
crv: "P-256", |
|
|
|
kty: "EC", |
|
|
|
x: toBase64Url(jwkObj[-2]), |
|
|
|
y: toBase64Url(jwkObj[-3]), |
|
|
|
x: arrayToBase64Url(jwkObj[-2]), |
|
|
|
y: arrayToBase64Url(jwkObj[-3]), |
|
|
|
}; |
|
|
|
const publicKeyBytes = Buffer.concat([ |
|
|
|
Buffer.from(jwkObj[-2]), |
|
|
@ -207,12 +208,65 @@ export function createPeerDid(publicKeyBytes: Uint8Array) { |
|
|
|
|
|
|
|
export class PeerSetup { |
|
|
|
public authenticatorData?: ArrayBuffer; |
|
|
|
public challenge?: Uint8Array; |
|
|
|
public clientDataJsonDecoded?: object; |
|
|
|
public signature?: Base64URLString; |
|
|
|
|
|
|
|
public async createJwt( |
|
|
|
payload: object, |
|
|
|
issuerDid: string, |
|
|
|
credentialId: string, |
|
|
|
) { |
|
|
|
const header: JWTPayload = { typ: "JWT", alg: "ES256" }; |
|
|
|
|
|
|
|
// from createJWT in did-jwt/src/JWT.ts
|
|
|
|
const timestamps: Partial<JWTPayload> = { |
|
|
|
iat: Math.floor(Date.now() / 1000), |
|
|
|
exp: undefined, |
|
|
|
}; |
|
|
|
const fullPayload = { ...timestamps, ...payload, iss: issuerDid }; |
|
|
|
|
|
|
|
const payloadHash: Uint8Array = sha256( |
|
|
|
Buffer.from(JSON.stringify(fullPayload)), |
|
|
|
); |
|
|
|
const options: PublicKeyCredentialRequestOptionsJSON = |
|
|
|
await generateAuthenticationOptions({ |
|
|
|
challenge: payloadHash, |
|
|
|
rpID: window.location.hostname, |
|
|
|
// Require users to use a previously-registered authenticator
|
|
|
|
// allowCredentials: userPasskeys.map(passkey => ({
|
|
|
|
// id: passkey.id,
|
|
|
|
// transports: passkey.transports,
|
|
|
|
// })),
|
|
|
|
}); |
|
|
|
console.log("custom authentication options", options); |
|
|
|
|
|
|
|
const clientAuth = await startAuthentication(options); |
|
|
|
console.log("custom clientAuth", clientAuth); |
|
|
|
|
|
|
|
this.authenticatorData = Buffer.from( |
|
|
|
clientAuth.response.authenticatorData, |
|
|
|
"base64", |
|
|
|
).buffer; |
|
|
|
this.challenge = payloadHash; |
|
|
|
this.clientDataJsonDecoded = JSON.parse( |
|
|
|
Buffer.from(clientAuth.response.clientDataJSON, "base64").toString( |
|
|
|
"utf-8", |
|
|
|
), |
|
|
|
); |
|
|
|
this.signature = clientAuth.response.signature; |
|
|
|
|
|
|
|
const headerBase64 = Buffer.from(JSON.stringify(header)).toString("base64"); |
|
|
|
const payloadBase64 = clientAuth.response.clientDataJSON; |
|
|
|
const signature = clientAuth.response.signature; |
|
|
|
|
|
|
|
return headerBase64 + "." + payloadBase64 + "." + signature; |
|
|
|
} |
|
|
|
|
|
|
|
public async createJwt2( |
|
|
|
payload: object, |
|
|
|
issuerDid: string, |
|
|
|
credentialId: string, |
|
|
|
) { |
|
|
|
const signer = await this.webAuthnES256KSigner(credentialId); |
|
|
|
|
|
|
@ -319,58 +373,63 @@ export async function verifyJwt( |
|
|
|
credId: Base64URLString, |
|
|
|
rawId: Uint8Array, |
|
|
|
authenticatorData: ArrayBuffer, |
|
|
|
challenge: Uint8Array, |
|
|
|
clientDataJSON: object, |
|
|
|
publicKey: Uint8Array, |
|
|
|
signature: Base64URLString, |
|
|
|
) { |
|
|
|
const options: PublicKeyCredentialRequestOptionsJSON = |
|
|
|
await generateAuthenticationOptions({ |
|
|
|
rpID: window.location.hostname, |
|
|
|
// Require users to use a previously-registered authenticator
|
|
|
|
// allowCredentials: userPasskeys.map(passkey => ({
|
|
|
|
// id: passkey.id,
|
|
|
|
// transports: passkey.transports,
|
|
|
|
// })),
|
|
|
|
}); |
|
|
|
console.log("authentication options", options); |
|
|
|
|
|
|
|
const clientAuth = await startAuthentication(options); |
|
|
|
console.log("clientAuth", clientAuth); |
|
|
|
|
|
|
|
const verfOpts: VerifyAuthenticationResponseOpts = { |
|
|
|
authenticator: { |
|
|
|
credentialID: credId, |
|
|
|
credentialPublicKey: publicKey, |
|
|
|
counter: 0, |
|
|
|
}, |
|
|
|
expectedChallenge: options.challenge, |
|
|
|
expectedOrigin: window.location.origin, |
|
|
|
expectedRPID: window.location.hostname, |
|
|
|
response: clientAuth, |
|
|
|
}; |
|
|
|
console.log("verfOpts", verfOpts); |
|
|
|
const verificationFromClient = await verifyAuthenticationResponse(verfOpts); |
|
|
|
console.log("client auth verification", verificationFromClient); |
|
|
|
|
|
|
|
const authData = toBase64Url(Buffer.from(authenticatorData)); |
|
|
|
// Here's a combined auth & verify process, based on some of the inputs.
|
|
|
|
//
|
|
|
|
// const options: PublicKeyCredentialRequestOptionsJSON =
|
|
|
|
// await generateAuthenticationOptions({
|
|
|
|
// rpID: window.location.hostname,
|
|
|
|
// // Require users to use a previously-registered authenticator
|
|
|
|
// // allowCredentials: userPasskeys.map(passkey => ({
|
|
|
|
// // id: passkey.id,
|
|
|
|
// // transports: passkey.transports,
|
|
|
|
// // })),
|
|
|
|
// });
|
|
|
|
// console.log("authentication options", options);
|
|
|
|
//
|
|
|
|
// const clientAuth = await startAuthentication(options);
|
|
|
|
// console.log("clientAuth", clientAuth);
|
|
|
|
//
|
|
|
|
// const verfOpts: VerifyAuthenticationResponseOpts = {
|
|
|
|
// authenticator: {
|
|
|
|
// credentialID: credId,
|
|
|
|
// credentialPublicKey: publicKey,
|
|
|
|
// counter: 0,
|
|
|
|
// },
|
|
|
|
// expectedChallenge: options.challenge,
|
|
|
|
// expectedOrigin: window.location.origin,
|
|
|
|
// expectedRPID: window.location.hostname,
|
|
|
|
// response: clientAuth,
|
|
|
|
// };
|
|
|
|
// console.log("verfOpts", verfOpts);
|
|
|
|
// const verificationFromClient = await verifyAuthenticationResponse(verfOpts);
|
|
|
|
// console.log("client auth verification", verificationFromClient);
|
|
|
|
|
|
|
|
const authData = arrayToBase64Url(Buffer.from(authenticatorData)); |
|
|
|
const authOpts: VerifyAuthenticationResponseOpts = { |
|
|
|
authenticator: { |
|
|
|
credentialID: credId, |
|
|
|
credentialPublicKey: publicKey, |
|
|
|
counter: 0, |
|
|
|
}, |
|
|
|
expectedChallenge: options.challenge, |
|
|
|
expectedChallenge: arrayToBase64Url(challenge), |
|
|
|
expectedOrigin: window.location.origin, |
|
|
|
expectedRPID: window.location.hostname, |
|
|
|
response: { |
|
|
|
authenticatorAttachment: "platform", |
|
|
|
clientExtensionResults: {}, |
|
|
|
id: credId, |
|
|
|
rawId: toBase64Url(rawId), |
|
|
|
rawId: arrayToBase64Url(rawId), |
|
|
|
response: { |
|
|
|
authenticatorData: authData, |
|
|
|
clientDataJSON: clientAuth.response.clientDataJSON, |
|
|
|
signature: clientAuth.response.signature, |
|
|
|
clientDataJSON: arrayToBase64Url( |
|
|
|
Buffer.from(JSON.stringify(clientDataJSON)), |
|
|
|
), |
|
|
|
signature: signature, |
|
|
|
}, |
|
|
|
type: "public-key", |
|
|
|
}, |
|
|
|