Search in sources :

Example 1 with RegistrationNotAllowed

use of org.xdi.oxauth.model.fido.u2f.exception.RegistrationNotAllowed in project oxAuth by GluuFederation.

the class U2fRegistrationWS method startRegistration.

@GET
@Produces({ "application/json" })
public Response startRegistration(@QueryParam("username") String userName, @QueryParam("application") String appId, @QueryParam("session_state") String sessionState, @QueryParam("enrollment_code") String enrollmentCode) {
    // Parameter username is deprecated. We uses it only to determine is it's one or two step workflow
    try {
        log.debug("Startig registration with username '{}' for appId '{}'. session_state '{}', enrollment_code '{}'", userName, appId, sessionState, enrollmentCode);
        String userInum = null;
        boolean sessionBasedEnrollment = false;
        boolean twoStep = StringHelper.isNotEmpty(userName);
        if (twoStep) {
            boolean removeEnrollment = false;
            if (StringHelper.isNotEmpty(sessionState)) {
                boolean valid = u2fValidationService.isValidSessionState(userName, sessionState);
                if (!valid) {
                    throw new BadInputException(String.format("session_state '%s' is invalid", sessionState));
                }
                sessionBasedEnrollment = true;
            } else if (StringHelper.isNotEmpty(enrollmentCode)) {
                boolean valid = u2fValidationService.isValidEnrollmentCode(userName, enrollmentCode);
                if (!valid) {
                    throw new BadInputException(String.format("enrollment_code '%s' is invalid", enrollmentCode));
                }
                removeEnrollment = true;
            } else {
                throw new BadInputException(String.format("session_state or enrollment_code is mandatory"));
            }
            User user = userService.getUser(userName);
            userInum = userService.getUserInum(user);
            if (StringHelper.isEmpty(userInum)) {
                throw new BadInputException(String.format("Failed to find user '%s' in LDAP", userName));
            }
            if (removeEnrollment) {
                // We allow to use enrollment code only one time
                user.setAttribute(U2fConstants.U2F_ENROLLMENT_CODE_ATTRIBUTE, (String) null);
                userService.updateUser(user);
            }
        }
        if (sessionBasedEnrollment) {
            List<DeviceRegistration> deviceRegistrations = deviceRegistrationService.findUserDeviceRegistrations(userInum, appId);
            if (deviceRegistrations.size() > 0 && !isCurrentAuthenticationLevelCorrespondsToU2fLevel(sessionState)) {
                throw new RegistrationNotAllowed(String.format("It's not possible to start registration with user_name and session_state becuase user '%s' has already enrolled device", userName));
            }
        }
        RegisterRequestMessage registerRequestMessage = u2fRegistrationService.builRegisterRequestMessage(appId, userInum);
        u2fRegistrationService.storeRegisterRequestMessage(registerRequestMessage, userInum, sessionState);
        // Convert manually to avoid possible conflict between resteasy providers, e.g. jettison, jackson
        final String entity = ServerUtil.asJson(registerRequestMessage);
        return Response.status(Response.Status.OK).entity(entity).cacheControl(ServerUtil.cacheControl(true)).build();
    } catch (Exception ex) {
        log.error("Exception happened", ex);
        if (ex instanceof WebApplicationException) {
            throw (WebApplicationException) ex;
        }
        if (ex instanceof RegistrationNotAllowed) {
            throw new WebApplicationException(Response.status(Response.Status.NOT_ACCEPTABLE).entity(errorResponseFactory.getErrorResponse(U2fErrorResponseType.REGISTRATION_NOT_ALLOWED)).build());
        }
        throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorResponseFactory.getJsonErrorResponse(U2fErrorResponseType.SERVER_ERROR)).build());
    }
}
Also used : BadInputException(org.xdi.oxauth.model.fido.u2f.exception.BadInputException) User(org.xdi.oxauth.model.common.User) WebApplicationException(javax.ws.rs.WebApplicationException) RegistrationNotAllowed(org.xdi.oxauth.model.fido.u2f.exception.RegistrationNotAllowed) DeviceRegistration(org.xdi.oxauth.model.fido.u2f.DeviceRegistration) RegisterRequestMessage(org.xdi.oxauth.model.fido.u2f.protocol.RegisterRequestMessage) BadInputException(org.xdi.oxauth.model.fido.u2f.exception.BadInputException) WebApplicationException(javax.ws.rs.WebApplicationException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 User (org.xdi.oxauth.model.common.User)1 DeviceRegistration (org.xdi.oxauth.model.fido.u2f.DeviceRegistration)1 BadInputException (org.xdi.oxauth.model.fido.u2f.exception.BadInputException)1 RegistrationNotAllowed (org.xdi.oxauth.model.fido.u2f.exception.RegistrationNotAllowed)1 RegisterRequestMessage (org.xdi.oxauth.model.fido.u2f.protocol.RegisterRequestMessage)1