AuthenticatorDataConverter { public <T extends ExtensionAuthenticatorOutput> byte[] convert(AuthenticatorData<T> source) { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] rpIdHash = source.getRpIdHash(); byteArrayOutputStream.write(rpIdHash); byteArrayOutputStream.write(new byte[]{source.getFlags()}); byteArrayOutputStream.write(UnsignedNumberUtil.toBytes(source.getSignCount())); if (source.getAttestedCredentialData() != null) { byteArrayOutputStream.write(attestedCredentialDataConverter.convert(source.getAttestedCredentialData())); } byteArrayOutputStream.write(convert(source.getExtensions())); return byteArrayOutputStream.toByteArray(); } catch (IOException e) { throw new UncheckedIOException(e); } } AuthenticatorDataConverter(ObjectConverter objectConverter); byte[] convert(AuthenticatorData<T> source); AuthenticatorData<T> convert(byte[] source); byte[] extractAttestedCredentialData(byte[] authenticatorData); long extractSignCount(byte[] authenticatorData);  }
@Test void convert_test() { String input = "SZYN5YgOjGh0NBcPZHZgW4_krrmihjLHmVzzuoMdl2MBAAABRQ"; AuthenticatorData<RegistrationExtensionAuthenticatorOutput> result = new AuthenticatorDataConverter(objectConverter).convert(Base64UrlUtil.decode(input)); assertThat(result.getRpIdHash()).isNotNull(); assertThat(result.getRpIdHash()).hasSize(32); assertThat(result.getFlags()).isEqualTo(BIT_UP); assertThat(result.getSignCount()).isEqualTo(325); assertThat(result.getAttestedCredentialData()).isNull(); assertThat(result.getExtensions().getKeys()).isEmpty(); }
@Test void convert_too_short_data_test() { byte[] input = Base64UrlUtil.decode("SZYN5YgOjGh0NBcP"); AuthenticatorDataConverter authenticatorDataConverter = new AuthenticatorDataConverter(objectConverter); assertThrows(DataConversionException.class, () -> authenticatorDataConverter.convert(input) ); }
@Test void serialize_deserialize_test() { byte[] rpIdHash = new byte[32]; byte flags = BIT_ED; AuthenticationExtensionsAuthenticatorOutputs.BuilderForRegistration builder = new AuthenticationExtensionsAuthenticatorOutputs.BuilderForRegistration(); builder.setUvm(new UvmEntries()); AuthenticatorData<RegistrationExtensionAuthenticatorOutput> authenticatorData = new AuthenticatorData<>(rpIdHash, flags, 0, builder.build()); byte[] serialized = new AuthenticatorDataConverter(objectConverter).convert(authenticatorData); AuthenticatorData<RegistrationExtensionAuthenticatorOutput> result = new AuthenticatorDataConverter(objectConverter).convert(serialized); assertThat(result.getRpIdHash()).isNotNull(); assertThat(result.getRpIdHash()).hasSize(32); assertThat(result.getFlags()).isEqualTo(BIT_ED); assertThat(result.getSignCount()).isZero(); assertThat(result.getAttestedCredentialData()).isNull(); assertThat(result.getExtensions().getKeys()).contains(UserVerificationMethodExtensionAuthenticatorOutput.ID); }
@Test void deserialize_data_with_surplus_bytes_test() { String input = "SZYN5YgOjGh0NBcPZHZgW4_krrmihjLHmVzzuoMdl2MBAAABRQ"; byte[] data = Base64UrlUtil.decode(input); byte[] bytes = Arrays.copyOf(data, data.length + 1); AuthenticatorDataConverter authenticatorDataConverter = new AuthenticatorDataConverter(objectConverter); assertThrows(DataConversionException.class, () -> authenticatorDataConverter.convert(bytes) ); }