use of com.yubico.util.Either in project java-webauthn-server by Yubico.
the class WebAuthnServer method startRegistration.
public Either<String, RegistrationRequest> startRegistration(@NonNull String username, @NonNull String displayName, Optional<String> credentialNickname, ResidentKeyRequirement residentKeyRequirement, Optional<ByteArray> sessionToken) throws ExecutionException {
logger.trace("startRegistration username: {}, credentialNickname: {}", username, credentialNickname);
final Collection<CredentialRegistration> registrations = userStorage.getRegistrationsByUsername(username);
final Optional<UserIdentity> existingUser = registrations.stream().findAny().map(CredentialRegistration::getUserIdentity);
final boolean permissionGranted = existingUser.map(userIdentity -> sessions.isSessionForUser(userIdentity.getId(), sessionToken)).orElse(true);
if (permissionGranted) {
final UserIdentity registrationUserId = existingUser.orElseGet(() -> UserIdentity.builder().name(username).displayName(displayName).id(generateRandom(32)).build());
RegistrationRequest request = new RegistrationRequest(username, credentialNickname, generateRandom(32), rp.startRegistration(StartRegistrationOptions.builder().user(registrationUserId).authenticatorSelection(AuthenticatorSelectionCriteria.builder().residentKey(residentKeyRequirement).build()).build()), Optional.of(sessions.createSession(registrationUserId.getId())));
registerRequestStorage.put(request.getRequestId(), request);
return Either.right(request);
} else {
return Either.left("The username \"" + username + "\" is already registered.");
}
}
use of com.yubico.util.Either 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.util.Either 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()));
}
}
}
use of com.yubico.util.Either in project cas by apereo.
the class WebAuthnServer method startRegistration.
public Either<String, RegistrationRequest> startRegistration(@NonNull final String username, final Optional<String> displayName, final Optional<String> credentialNickname, final ResidentKeyRequirement residentKeyRequirement, final Optional<ByteArray> sessionToken) throws ExecutionException {
LOGGER.trace("startRegistration username: {}, credentialNickname: {}", username, credentialNickname);
var registrations = userStorage.getRegistrationsByUsername(username);
var existingUser = registrations.stream().findAny().map(CredentialRegistration::getUserIdentity);
val permissionGranted = existingUser.map(userIdentity -> sessions.isSessionForUser(userIdentity.getId(), sessionToken)).orElse(true);
if (permissionGranted) {
var registrationUserId = existingUser.orElseGet(() -> UserIdentity.builder().name(username).displayName(displayName.get()).id(generateRandom(32)).build());
val request = new RegistrationRequest(username, credentialNickname, generateRandom(32), rp.startRegistration(StartRegistrationOptions.builder().user(registrationUserId).authenticatorSelection(AuthenticatorSelectionCriteria.builder().residentKey(residentKeyRequirement).build()).build()), Optional.of(sessions.createSession(registrationUserId.getId())));
registerRequestStorage.put(request.getRequestId(), request);
return Either.right(request);
} else {
return Either.left("The username \"" + username + "\" is already registered.");
}
}
Aggregations