Search in sources :

Example 1 with BasicPrincipal

use of io.prestosql.spi.security.BasicPrincipal in project hetu-core by openlookeng.

the class LdapAuthenticator method authenticate.

private Principal authenticate(String user, String password) {
    Map<String, String> environment = createEnvironment(user, password);
    DirContext context = null;
    try {
        context = createDirContext(environment);
        checkForGroupMembership(user, context);
        log.debug("Authentication successful for user [%s]", user);
        return new BasicPrincipal(user);
    } catch (AuthenticationException e) {
        log.debug("Authentication failed for user [%s]: %s", user, e.getMessage());
        throw new AccessDeniedException("Invalid credentials");
    } catch (NamingException e) {
        log.debug(e, "Authentication error for user [%s]", user);
        throw new RuntimeException("Authentication error");
    } finally {
        if (context != null) {
            closeContext(context);
        }
    }
}
Also used : AccessDeniedException(io.prestosql.spi.security.AccessDeniedException) BasicPrincipal(io.prestosql.spi.security.BasicPrincipal) AuthenticationException(javax.naming.AuthenticationException) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) JndiUtils.createDirContext(io.prestosql.plugin.password.jndi.JndiUtils.createDirContext)

Example 2 with BasicPrincipal

use of io.prestosql.spi.security.BasicPrincipal in project hetu-core by openlookeng.

the class AuthenticationFilter method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain nextFilter) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    if (internalAuthenticationManager.isInternalRequest(request)) {
        Principal principal = internalAuthenticationManager.authenticateInternalRequest(request);
        if (principal == null) {
            response.sendError(SC_UNAUTHORIZED);
            return;
        }
        nextFilter.doFilter(withPrincipal(request, principal), response);
        return;
    }
    if (isWebUi(request)) {
        // asset files, vendor files and disable page are always visible
        if (isSkipAuth(request)) {
            nextFilter.doFilter(request, response);
            return;
        }
        Optional<String> authenticatedUser = uiAuthenticator.getAuthenticatedUsername(request);
        if (authenticatedUser.isPresent()) {
            // if the authenticated user is requesting the login page, send them directly to the ui
            if (request.getPathInfo().equals(UiAuthenticator.LOGIN_FORM)) {
                response.sendRedirect(UiAuthenticator.UI_LOCATION);
                return;
            }
            // authentication succeeded
            request.setAttribute(PRESTO_USER, authenticatedUser.get());
            nextFilter.doFilter(withPrincipal(request, new BasicPrincipal(authenticatedUser.get())), response);
            return;
        }
        AccessType accessType = getAccessType(request, authenticators, config);
        if (accessType.equals(AccessType.DISABLE)) {
            // redirect to disable page
            response.sendRedirect(UiAuthenticator.DISABLED_LOCATION);
            return;
        }
        // skip authentication for login/logout page
        if (isLoginLogout(request)) {
            nextFilter.doFilter(request, response);
            return;
        }
        if (accessType.equals(AccessType.REDIRECT)) {
            // redirect to login page
            URI redirectUri = UiAuthenticator.buildLoginFormURI(URI.create(request.getRequestURI()));
            response.sendRedirect(redirectUri.toString());
            return;
        }
    }
    // skip authentication if non-secure or not configured
    if (!request.isSecure() || authenticators.isEmpty()) {
        nextFilter.doFilter(request, response);
        return;
    }
    // try to authenticate, collecting errors and authentication headers
    Set<String> messages = new LinkedHashSet<>();
    Set<String> authenticateHeaders = new LinkedHashSet<>();
    for (Authenticator authenticator : authenticators) {
        AuthenticatedPrincipal authenticatedPrincipal;
        try {
            authenticatedPrincipal = authenticator.authenticate(request);
        } catch (AuthenticationException e) {
            if (e.getMessage() != null) {
                messages.add(e.getMessage());
            }
            e.getAuthenticateHeader().ifPresent(authenticateHeaders::add);
            continue;
        }
        // authentication succeeded
        request.setAttribute(PRESTO_USER, authenticatedPrincipal.getUser());
        nextFilter.doFilter(withPrincipal(request, authenticatedPrincipal.getPrincipal()), response);
        return;
    }
    // authentication failed
    skipRequestBody(request);
    for (String value : authenticateHeaders) {
        response.addHeader(WWW_AUTHENTICATE, value);
    }
    if (messages.isEmpty()) {
        messages.add("Unauthorized");
    }
    response.sendError(SC_UNAUTHORIZED, Joiner.on(" | ").join(messages));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BasicPrincipal(io.prestosql.spi.security.BasicPrincipal) HttpServletResponse(javax.servlet.http.HttpServletResponse) URI(java.net.URI) AuthenticatedPrincipal(io.prestosql.server.security.Authenticator.AuthenticatedPrincipal) HttpServletRequest(javax.servlet.http.HttpServletRequest) BasicPrincipal(io.prestosql.spi.security.BasicPrincipal) Principal(java.security.Principal) AuthenticatedPrincipal(io.prestosql.server.security.Authenticator.AuthenticatedPrincipal) UiAuthenticator(io.prestosql.queryeditorui.security.UiAuthenticator)

Example 3 with BasicPrincipal

use of io.prestosql.spi.security.BasicPrincipal in project hetu-core by openlookeng.

the class JsonWebTokenAuthenticator method authenticate.

@Override
public AuthenticatedPrincipal authenticate(HttpServletRequest request) throws AuthenticationException {
    String header = nullToEmpty(request.getHeader(AUTHORIZATION));
    int space = header.indexOf(' ');
    if ((space < 0) || !header.substring(0, space).equalsIgnoreCase("bearer")) {
        throw needAuthentication(null);
    }
    String token = header.substring(space + 1).trim();
    if (token.isEmpty()) {
        throw needAuthentication(null);
    }
    try {
        Jws<Claims> claimsJws = jwtParser.parseClaimsJws(token);
        String subject = claimsJws.getBody().getSubject();
        String authenticatedUser = userMapping.mapUser(subject);
        return new AuthenticatedPrincipal(authenticatedUser, new BasicPrincipal(subject));
    } catch (JwtException | UserMappingException e) {
        throw needAuthentication(e.getMessage());
    } catch (RuntimeException e) {
        throw new RuntimeException("Authentication error", e);
    }
}
Also used : Claims(io.jsonwebtoken.Claims) BasicPrincipal(io.prestosql.spi.security.BasicPrincipal) UnsupportedJwtException(io.jsonwebtoken.UnsupportedJwtException) JwtException(io.jsonwebtoken.JwtException)

Aggregations

BasicPrincipal (io.prestosql.spi.security.BasicPrincipal)3 Claims (io.jsonwebtoken.Claims)1 JwtException (io.jsonwebtoken.JwtException)1 UnsupportedJwtException (io.jsonwebtoken.UnsupportedJwtException)1 JndiUtils.createDirContext (io.prestosql.plugin.password.jndi.JndiUtils.createDirContext)1 UiAuthenticator (io.prestosql.queryeditorui.security.UiAuthenticator)1 AuthenticatedPrincipal (io.prestosql.server.security.Authenticator.AuthenticatedPrincipal)1 AccessDeniedException (io.prestosql.spi.security.AccessDeniedException)1 URI (java.net.URI)1 Principal (java.security.Principal)1 LinkedHashSet (java.util.LinkedHashSet)1 AuthenticationException (javax.naming.AuthenticationException)1 NamingException (javax.naming.NamingException)1 DirContext (javax.naming.directory.DirContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1