create method

  1. @override
Future<Map<String, Object?>?> create(
  1. Map<String, Object?> options
)
override

Creates a credential from the server's PublicKeyCredentialCreationOptionsJSON.

Implementation

@override
Future<Map<String, Object?>?> create(Map<String, Object?> options) async {
  final rpId = jsonMap(options['rp'])['id'] as String;
  final userHandle = jsonMap(options['user'])['id'] as String;
  final challenge = options['challenge'] as String;

  final keyPair = _generateKeyPair();
  final credentialId = _bytes(16);
  final credential = _Credential(
    idB64url: toB64url(credentialId),
    rpId: rpId,
    userHandleB64url: userHandle,
    privateKey: keyPair.privateKey as ECPrivateKey,
    x: _fixed32((keyPair.publicKey as ECPublicKey).Q!.x!.toBigInteger()!),
    y: _fixed32((keyPair.publicKey as ECPublicKey).Q!.y!.toBigInteger()!),
  );
  _credentials.add(credential);

  final clientData = _clientData('webauthn.create', challenge);
  final coseKey = _Cbor.encode({
    1: 2, // kty: EC2
    3: -7, // alg: ES256
    -1: 1, // crv: P-256
    -2: credential.x,
    -3: credential.y,
  });
  final authData = BytesBuilder()
    ..add(_sha256(utf8.encode(rpId)))
    // UP | UV | BE | BS | AT: a synced, user-verified platform passkey.
    ..addByte(0x01 | 0x04 | 0x08 | 0x10 | 0x40)
    ..add(_uint32(0))
    ..add(Uint8List(16)) // AAGUID
    ..add(_uint16(credentialId.length))
    ..add(credentialId)
    ..add(coseKey);
  final attestationObject = _Cbor.encode({
    'fmt': 'none',
    'attStmt': <String, Object?>{},
    'authData': authData.toBytes(),
  });

  return {
    'id': credential.idB64url,
    'rawId': credential.idB64url,
    'type': 'public-key',
    'response': {
      'clientDataJSON': toB64url(clientData),
      'attestationObject': toB64url(attestationObject),
      'transports': ['internal', 'hybrid'],
    },
    'clientExtensionResults': <String, Object?>{},
    'authenticatorAttachment': 'platform',
  };
}