use of com.webauthn4j.converter.exception.DataConversionException in project webauthn4j by webauthn4j.
the class AttestationObjectConverter method convert.
// ~ Methods
// ================================================================================================
/**
* Converts from a base64url {@link String} to {@link AttestationObject}.
*
* @param source the source byte array to convert
* @return the converted object
*/
@Nullable
public AttestationObject convert(@NonNull String source) {
try {
AssertUtil.notNull(source, SOURCE_NULL_CHECK_MESSAGE);
byte[] value = Base64UrlUtil.decode(source);
return convert(value);
} catch (IllegalArgumentException e) {
throw new DataConversionException(e);
}
}
use of com.webauthn4j.converter.exception.DataConversionException in project webauthn4j by webauthn4j.
the class AuthenticatorDataConverter method convert.
// ~ Methods
// ================================================================================================
/**
* Converts from a {@link AuthenticatorData} to byte[].
*
* @param source the source object to convert
* @param <T> extension type
* @return the converted byte array
*/
@NonNull
public <T extends ExtensionAuthenticatorOutput> byte[] convert(@NonNull 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 (IllegalArgumentException e) {
throw new DataConversionException(e);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of com.webauthn4j.converter.exception.DataConversionException in project webauthn4j by webauthn4j.
the class CollectedClientDataConverter method convert.
// ~ Methods
// ================================================================================================
/**
* Converts from a base64url {@link String} to {@link CollectedClientData}.
*
* @param base64UrlString the source byte array to convert
* @return the converted object
*/
@Nullable
public CollectedClientData convert(@NonNull String base64UrlString) {
try {
AssertUtil.notNull(base64UrlString, "base64UrlString must not be null");
byte[] bytes = Base64UrlUtil.decode(base64UrlString);
return convert(bytes);
} catch (IllegalArgumentException e) {
throw new DataConversionException(e);
}
}
use of com.webauthn4j.converter.exception.DataConversionException in project webauthn4j by webauthn4j.
the class CollectedClientDataConverter method convert.
/**
* Converts from a byte array to {@link CollectedClientData}.
*
* @param source the source byte array to convert
* @return the converted object
*/
@Nullable
public CollectedClientData convert(@NonNull byte[] source) {
try {
AssertUtil.notNull(source, "source must not be null");
String jsonString = new String(source, StandardCharsets.UTF_8);
return jsonConverter.readValue(jsonString, CollectedClientData.class);
} catch (IllegalArgumentException e) {
throw new DataConversionException(e);
}
}
use of com.webauthn4j.converter.exception.DataConversionException in project webauthn4j-spring-security by webauthn4j.
the class FidoServerAttestationOptionsEndpointFilter method processRequest.
@Override
protected ServerResponse processRequest(HttpServletRequest request) {
InputStream inputStream;
try {
inputStream = request.getInputStream();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
try {
ServerPublicKeyCredentialCreationOptionsRequest serverRequest = objectConverter.getJsonConverter().readValue(inputStream, ServerPublicKeyCredentialCreationOptionsRequest.class);
String username = serverRequest.getUsername();
String displayName = serverRequest.getDisplayName();
Challenge challenge = serverEndpointFilterUtil.encodeUsername(new DefaultChallenge(), username);
challengeRepository.saveChallenge(challenge, request);
// TODO: UsernamePasswordAuthenticationToken should not be used here in this way
AttestationOptions attestationOptions = optionsProvider.getAttestationOptions(request, new UsernamePasswordAuthenticationToken(username, null, Collections.emptyList()));
String userHandle;
if (attestationOptions.getUser() == null) {
userHandle = Base64UrlUtil.encodeToString(generateUserHandle());
} else {
userHandle = Base64UrlUtil.encodeToString(attestationOptions.getUser().getId());
}
ServerPublicKeyCredentialUserEntity user = new ServerPublicKeyCredentialUserEntity(userHandle, username, displayName);
List<ServerPublicKeyCredentialDescriptor> credentials = attestationOptions.getExcludeCredentials().stream().map(credential -> new ServerPublicKeyCredentialDescriptor(credential.getType(), Base64UrlUtil.encodeToString(credential.getId()), credential.getTransports())).collect(Collectors.toList());
AuthenticationExtensionsClientInputs<RegistrationExtensionClientInput> authenticationExtensionsClientInputs;
if (serverRequest.getExtensions() != null) {
authenticationExtensionsClientInputs = serverRequest.getExtensions();
} else {
authenticationExtensionsClientInputs = attestationOptions.getExtensions();
}
return new ServerPublicKeyCredentialCreationOptionsResponse(attestationOptions.getRp(), user, Base64UrlUtil.encodeToString(attestationOptions.getChallenge().getValue()), attestationOptions.getPubKeyCredParams(), attestationOptions.getTimeout(), credentials, serverRequest.getAuthenticatorSelection(), serverRequest.getAttestation(), authenticationExtensionsClientInputs);
} catch (DataConversionException e) {
throw new com.webauthn4j.springframework.security.exception.DataConversionException("Failed to convert data", e);
}
}
Aggregations