use of com.yubico.webauthn.exception.RegistrationFailedException in project java-webauthn-server by Yubico.
the class WebAuthnServer method finishRegistration.
public Either<List<String>, SuccessfulRegistrationResult> finishRegistration(String responseJson) {
logger.trace("finishRegistration responseJson: {}", responseJson);
RegistrationResponse response = null;
try {
response = jsonMapper.readValue(responseJson, RegistrationResponse.class);
} catch (IOException e) {
logger.error("JSON error in finishRegistration; responseJson: {}", responseJson, e);
return Either.left(Arrays.asList("Registration failed!", "Failed to decode response object.", e.getMessage()));
}
RegistrationRequest request = registerRequestStorage.getIfPresent(response.getRequestId());
registerRequestStorage.invalidate(response.getRequestId());
if (request == null) {
logger.debug("fail finishRegistration responseJson: {}", responseJson);
return Either.left(Arrays.asList("Registration failed!", "No such registration in progress."));
} else {
try {
com.yubico.webauthn.RegistrationResult registration = rp.finishRegistration(FinishRegistrationOptions.builder().request(request.getPublicKeyCredentialCreationOptions()).response(response.getCredential()).build());
if (userStorage.userExists(request.getUsername())) {
boolean permissionGranted = false;
final boolean isValidSession = request.getSessionToken().map(token -> sessions.isSessionForUser(request.getPublicKeyCredentialCreationOptions().getUser().getId(), token)).orElse(false);
logger.debug("Session token: {}", request.getSessionToken());
logger.debug("Valid session: {}", isValidSession);
if (isValidSession) {
permissionGranted = true;
logger.info("Session token accepted for user {}", request.getPublicKeyCredentialCreationOptions().getUser().getId());
}
logger.debug("permissionGranted: {}", permissionGranted);
if (!permissionGranted) {
throw new RegistrationFailedException(new IllegalArgumentException(String.format("User %s already exists", request.getUsername())));
}
}
return Either.right(new SuccessfulRegistrationResult(request, response, addRegistration(request.getPublicKeyCredentialCreationOptions().getUser(), request.getCredentialNickname(), registration), registration.isAttestationTrusted(), sessions.createSession(request.getPublicKeyCredentialCreationOptions().getUser().getId())));
} catch (RegistrationFailedException e) {
logger.debug("fail finishRegistration responseJson: {}", responseJson, e);
return Either.left(Arrays.asList("Registration failed!", e.getMessage()));
} catch (Exception e) {
logger.error("fail finishRegistration responseJson: {}", responseJson, e);
return Either.left(Arrays.asList("Registration failed unexpectedly; this is likely a bug.", e.getMessage()));
}
}
}
use of com.yubico.webauthn.exception.RegistrationFailedException in project cas by apereo.
the class WebAuthnServer method finishRegistration.
public Either<List<String>, SuccessfulRegistrationResult> finishRegistration(final String responseJson) {
LOGGER.trace("finishRegistration responseJson: {}", responseJson);
RegistrationResponse response = null;
try {
response = jsonMapper.readValue(responseJson, RegistrationResponse.class);
} catch (final IOException e) {
LOGGER.error("JSON error in finishRegistration; responseJson: {}", responseJson, e);
return Either.left(Arrays.asList("Registration failed!", "Failed to decode response object.", e.getMessage()));
}
val request = registerRequestStorage.getIfPresent(response.getRequestId());
registerRequestStorage.invalidate(response.getRequestId());
if (request == null) {
LOGGER.debug("fail finishRegistration responseJson: {}", responseJson);
return Either.left(Arrays.asList("Registration failed!", "No such registration in progress."));
} else {
try {
val registration = rp.finishRegistration(FinishRegistrationOptions.builder().request(request.getPublicKeyCredentialCreationOptions()).response(response.getCredential()).build());
if (userStorage.userExists(request.getUsername())) {
var permissionGranted = false;
val isValidSession = request.getSessionToken().map(token -> sessions.isSessionForUser(request.getPublicKeyCredentialCreationOptions().getUser().getId(), token)).orElse(false);
LOGGER.debug("Session token: {}", request.getSessionToken());
LOGGER.debug("Valid session: {}", isValidSession);
if (isValidSession) {
permissionGranted = true;
LOGGER.info("Session token accepted for user {}", request.getPublicKeyCredentialCreationOptions().getUser().getId());
}
LOGGER.debug("permissionGranted: {}", permissionGranted);
if (!permissionGranted) {
throw new RegistrationFailedException(new IllegalArgumentException(String.format("User %s already exists", request.getUsername())));
}
}
return Either.right(new SuccessfulRegistrationResult(request, response, addRegistration(request.getPublicKeyCredentialCreationOptions().getUser(), request.getCredentialNickname(), registration), registration.isAttestationTrusted(), sessions.createSession(request.getPublicKeyCredentialCreationOptions().getUser().getId())));
} catch (final RegistrationFailedException e) {
LOGGER.debug("fail finishRegistration responseJson: {}", responseJson, e);
return Either.left(Arrays.asList("Registration failed", e.getMessage()));
} catch (final Exception e) {
LOGGER.error("fail finishRegistration responseJson: {}", responseJson, e);
return Either.left(Arrays.asList("Registration failed unexpectedly; this is likely a bug.", e.getMessage()));
}
}
}
Aggregations