Search in sources :

Example 6 with JWTNotRequired

use of io.hops.hopsworks.api.filter.JWTNotRequired in project hopsworks by logicalclocks.

the class AuthService method passwordRecovery.

@POST
@Path("/reset/password")
@Produces(MediaType.APPLICATION_JSON)
@JWTNotRequired
public Response passwordRecovery(@FormParam("key") String key, @FormParam("newPassword") String newPassword, @FormParam("confirmPassword") String confirmPassword, @Context HttpServletRequest req) throws UserException, MessagingException {
    RESTApiJsonResponse json = new RESTApiJsonResponse();
    userController.changePassword(key, newPassword, confirmPassword);
    json.setSuccessMessage(ResponseMessages.PASSWORD_CHANGED);
    return Response.ok(json).build();
}
Also used : RESTApiJsonResponse(io.hops.hopsworks.api.util.RESTApiJsonResponse) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) JWTNotRequired(io.hops.hopsworks.api.filter.JWTNotRequired)

Example 7 with JWTNotRequired

use of io.hops.hopsworks.api.filter.JWTNotRequired in project hopsworks by logicalclocks.

the class AuthService method register.

@POST
@Path("register")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@JWTNotRequired
public Response register(UserDTO newUser, @Context HttpServletRequest req) throws UserException {
    if (settings.isRegistrationDisabled()) {
        throw new UserException(RESTCodes.UserErrorCode.ACCOUNT_REGISTRATION_ERROR, Level.FINE, "Registration not " + "allowed.");
    }
    RESTApiJsonResponse json = new RESTApiJsonResponse();
    String linkUrl = FormatUtils.getUserURL(req) + settings.getEmailVerificationEndpoint();
    QrCode qrCode = userController.registerUser(newUser, linkUrl);
    if (authController.isTwoFactorEnabled(newUser.isTwoFactor())) {
        return Response.ok(qrCode).build();
    } else {
        json.setSuccessMessage("We registered your account request. Please validate you email and we will " + "review your account within 48 hours.");
    }
    return Response.ok(json).build();
}
Also used : QrCode(io.hops.hopsworks.common.user.QrCode) RESTApiJsonResponse(io.hops.hopsworks.api.util.RESTApiJsonResponse) UserException(io.hops.hopsworks.exceptions.UserException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) JWTNotRequired(io.hops.hopsworks.api.filter.JWTNotRequired)

Example 8 with JWTNotRequired

use of io.hops.hopsworks.api.filter.JWTNotRequired in project hopsworks by logicalclocks.

the class AuthService method serviceLogin.

@POST
@Path("/service")
@Produces(MediaType.APPLICATION_JSON)
@JWTNotRequired
public Response serviceLogin(@FormParam("email") String email, @FormParam("password") String password, @Context HttpServletRequest request) throws UserException, GeneralSecurityException, SigningKeyNotFoundException, DuplicateSigningKeyException, HopsSecurityException {
    if (Strings.isNullOrEmpty(email)) {
        throw new IllegalArgumentException("Email cannot be null or empty");
    }
    if (Strings.isNullOrEmpty(password)) {
        throw new IllegalArgumentException("Password cannot be null or empty");
    }
    Users user = userFacade.findByEmail(email);
    if (user == null) {
        throw new LoginException("Could not find registered user with email " + email);
    }
    if (!needLogin(request, user)) {
        return Response.ok().build();
    }
    if (!userController.isUserInRole(user, "AGENT")) {
        throw new HopsSecurityException(RESTCodes.SecurityErrorCode.REST_ACCESS_CONTROL, Level.FINE, "Users are not allowed to access this endpoint, use auth/login instead", "User " + user.getUsername() + " tried to login but they don't have AGENT role");
    }
    request.getSession();
    Collection roles = user.getBbcGroupCollection();
    if (roles == null || roles.isEmpty()) {
        throw new UserException(RESTCodes.UserErrorCode.NO_ROLE_FOUND, Level.FINE);
    }
    statusValidator.checkStatus(user.getStatus());
    String saltedPassword = authController.preCustomRealmLoginCheck(user, password, null);
    try {
        request.login(user.getEmail(), saltedPassword);
    } catch (ServletException ex) {
        authController.registerAuthenticationFailure(user);
        throw new UserException(RESTCodes.UserErrorCode.AUTHENTICATION_FAILURE, Level.FINE, null, ex.getMessage(), ex);
    }
    // First generate the one-time tokens for renewal of master token
    String renewalKeyName = jwtController.getServiceOneTimeJWTSigningKeyname(user.getUsername(), request.getRemoteHost());
    LocalDateTime masterExpiration = DateUtils.getNow().plus(settings.getServiceJWTLifetimeMS(), ChronoUnit.MILLIS);
    LocalDateTime notBefore = jwtController.computeNotBefore4ServiceRenewalTokens(masterExpiration);
    LocalDateTime expiresAt = notBefore.plus(settings.getServiceJWTLifetimeMS(), ChronoUnit.MILLIS);
    List<String> userRoles = userController.getUserRoles(user);
    JsonWebToken renewalJWTSpec = new JsonWebToken();
    renewalJWTSpec.setSubject(user.getUsername());
    renewalJWTSpec.setIssuer(settings.getJWTIssuer());
    renewalJWTSpec.setAudience(JWTHelper.SERVICE_RENEW_JWT_AUDIENCE);
    renewalJWTSpec.setKeyId(renewalKeyName);
    renewalJWTSpec.setNotBefore(DateUtils.localDateTime2Date(notBefore));
    renewalJWTSpec.setExpiresAt(DateUtils.localDateTime2Date(expiresAt));
    Map<String, Object> claims = new HashMap<>(4);
    claims.put(Constants.RENEWABLE, false);
    claims.put(Constants.EXPIRY_LEEWAY, 3600);
    claims.put(Constants.ROLES, userRoles.toArray(new String[1]));
    String[] oneTimeRenewalTokens = jwtController.generateOneTimeTokens4ServiceJWTRenewal(renewalJWTSpec, claims, settings.getJWTSigningKeyName());
    // Then generate the master service token
    try {
        String signingKeyID = jwtController.getSignKeyID(oneTimeRenewalTokens[0]);
        claims.clear();
        // The rest of JWT claims will be added by JWTHelper
        claims.put(Constants.RENEWABLE, false);
        claims.put(Constants.SERVICE_JWT_RENEWAL_KEY_ID, signingKeyID);
        String token = jWTHelper.createToken(user, settings.getJWTIssuer(), claims);
        ServiceJWTDTO renewTokensResponse = new ServiceJWTDTO();
        renewTokensResponse.setRenewTokens(oneTimeRenewalTokens);
        return Response.ok().header(AUTHORIZATION, Constants.BEARER + token).entity(renewTokensResponse).build();
    } catch (Exception ex) {
        jwtController.deleteSigningKey(renewalKeyName);
        throw ex;
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) Users(io.hops.hopsworks.persistence.entity.user.Users) JsonWebToken(io.hops.hopsworks.jwt.JsonWebToken) LoginException(javax.security.auth.login.LoginException) ServletException(javax.servlet.ServletException) MessagingException(javax.mail.MessagingException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HopsSecurityException(io.hops.hopsworks.exceptions.HopsSecurityException) DuplicateSigningKeyException(io.hops.hopsworks.jwt.exception.DuplicateSigningKeyException) SigningKeyNotFoundException(io.hops.hopsworks.jwt.exception.SigningKeyNotFoundException) UserException(io.hops.hopsworks.exceptions.UserException) InvalidationException(io.hops.hopsworks.jwt.exception.InvalidationException) HopsSecurityException(io.hops.hopsworks.exceptions.HopsSecurityException) ServletException(javax.servlet.ServletException) LoginException(javax.security.auth.login.LoginException) Collection(java.util.Collection) UserException(io.hops.hopsworks.exceptions.UserException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) JWTNotRequired(io.hops.hopsworks.api.filter.JWTNotRequired)

Example 9 with JWTNotRequired

use of io.hops.hopsworks.api.filter.JWTNotRequired in project hopsworks by logicalclocks.

the class VariablesService method getFileNameValidatorRegex.

@GET
@Path("filename-regex")
@Produces(MediaType.APPLICATION_JSON)
@JWTNotRequired
public Response getFileNameValidatorRegex(@QueryParam("type") String type) throws GenericException {
    FileNameRegexDTO fileNameRegexDTO = new FileNameRegexDTO();
    if (type == null || type.equals("project")) {
        fileNameRegexDTO.setRegex(FolderNameValidator.getProjectNameRegexStr(settings.getReservedProjectNames()));
        fileNameRegexDTO.setReservedWords(settings.getProjectNameReservedWords().toUpperCase());
    } else if (type.equals("dataset")) {
        fileNameRegexDTO.setRegex(FolderNameValidator.getDatasetNameRegexStr());
    } else {
        throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "Type QueryParam should be:" + "project|dataset|subdir");
    }
    return Response.ok(fileNameRegexDTO).build();
}
Also used : GenericException(io.hops.hopsworks.exceptions.GenericException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) JWTNotRequired(io.hops.hopsworks.api.filter.JWTNotRequired)

Example 10 with JWTNotRequired

use of io.hops.hopsworks.api.filter.JWTNotRequired in project hopsworks by logicalclocks.

the class VariablesService method getAuthenticationStatus.

@GET
@Path("authenticationStatus")
@Produces(MediaType.APPLICATION_JSON)
@JWTNotRequired
public Response getAuthenticationStatus() {
    boolean remoteAuthEnabled = remoteUserHelper.isRemoteUserAuthAvailable();
    List<OpenIdProvider> providers = new ArrayList<>();
    if (remoteAuthEnabled) {
        List<OauthClient> oauthClients = oauthClientFacade.findAll();
        for (OauthClient client : oauthClients) {
            providers.add(new OpenIdProvider(client.getProviderName(), client.getProviderDisplayName(), client.getProviderLogoURI()));
        }
    }
    AuthenticationStatus authenticationStatus = new AuthenticationStatus(OTPAuthStatus.fromTwoFactorMode(settings.getTwoFactorAuth()), settings.isLdapEnabled(), settings.isKrbEnabled(), settings.isOAuthEnabled(), providers, settings.isPasswordLoginDisabled(), settings.isRegistrationUIDisabled(), remoteAuthEnabled);
    return Response.ok(authenticationStatus).build();
}
Also used : OauthClient(io.hops.hopsworks.persistence.entity.remote.oauth.OauthClient) ArrayList(java.util.ArrayList) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) JWTNotRequired(io.hops.hopsworks.api.filter.JWTNotRequired)

Aggregations

JWTNotRequired (io.hops.hopsworks.api.filter.JWTNotRequired)12 Produces (javax.ws.rs.Produces)12 Path (javax.ws.rs.Path)10 POST (javax.ws.rs.POST)7 RESTApiJsonResponse (io.hops.hopsworks.api.util.RESTApiJsonResponse)5 GET (javax.ws.rs.GET)5 Users (io.hops.hopsworks.persistence.entity.user.Users)4 UserException (io.hops.hopsworks.exceptions.UserException)2 OauthClient (io.hops.hopsworks.persistence.entity.remote.oauth.OauthClient)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ArrayList (java.util.ArrayList)2 LoginException (javax.security.auth.login.LoginException)2 Response (javax.ws.rs.core.Response)2 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)1 NoCacheResponse (io.hops.hopsworks.api.filter.NoCacheResponse)1 DatasetPath (io.hops.hopsworks.common.dataset.util.DatasetPath)1 Maintenance (io.hops.hopsworks.common.maintenance.Maintenance)1 QrCode (io.hops.hopsworks.common.user.QrCode)1 DatasetException (io.hops.hopsworks.exceptions.DatasetException)1 GenericException (io.hops.hopsworks.exceptions.GenericException)1