Search in sources :

Example 1 with GraphManager

use of com.baidu.hugegraph.core.GraphManager in project incubator-hugegraph by apache.

the class AuthenticationFilter method authenticate.

protected User authenticate(ContainerRequestContext context) {
    GraphManager manager = this.managerProvider.get();
    E.checkState(manager != null, "Context GraphManager is absent");
    if (!manager.requireAuthentication()) {
        // Return anonymous user with admin role if disable authentication
        return User.ANONYMOUS;
    }
    // Get peer info
    Request request = this.requestProvider.get();
    String peer = null;
    String path = null;
    if (request != null) {
        peer = request.getRemoteAddr() + ":" + request.getRemotePort();
        path = request.getRequestURI();
    }
    Map<String, String> credentials = new HashMap<>();
    // Extract authentication credentials
    String auth = context.getHeaderString(HttpHeaders.AUTHORIZATION);
    if (auth == null) {
        throw new NotAuthorizedException("Authentication credentials are required", "Missing authentication credentials");
    }
    if (auth.startsWith(BASIC_AUTH_PREFIX)) {
        auth = auth.substring(BASIC_AUTH_PREFIX.length());
        auth = new String(DatatypeConverter.parseBase64Binary(auth), Charsets.ASCII_CHARSET);
        String[] values = auth.split(":");
        if (values.length != 2) {
            throw new BadRequestException("Invalid syntax for username and password");
        }
        final String username = values[0];
        final String password = values[1];
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new BadRequestException("Invalid syntax for username and password");
        }
        credentials.put(HugeAuthenticator.KEY_USERNAME, username);
        credentials.put(HugeAuthenticator.KEY_PASSWORD, password);
    } else if (auth.startsWith(BEARER_TOKEN_PREFIX)) {
        String token = auth.substring(BEARER_TOKEN_PREFIX.length());
        credentials.put(HugeAuthenticator.KEY_TOKEN, token);
    } else {
        throw new BadRequestException("Only HTTP Basic or Bearer authentication is supported");
    }
    credentials.put(HugeAuthenticator.KEY_ADDRESS, peer);
    credentials.put(HugeAuthenticator.KEY_PATH, path);
    // Validate the extracted credentials
    try {
        return manager.authenticate(credentials);
    } catch (AuthenticationException e) {
        throw new NotAuthorizedException("Authentication failed", e.getMessage());
    }
}
Also used : GraphManager(com.baidu.hugegraph.core.GraphManager) HashMap(java.util.HashMap) AuthenticationException(org.apache.tinkerpop.gremlin.server.auth.AuthenticationException) Request(org.glassfish.grizzly.http.server.Request) BadRequestException(jakarta.ws.rs.BadRequestException) NotAuthorizedException(jakarta.ws.rs.NotAuthorizedException)

Aggregations

GraphManager (com.baidu.hugegraph.core.GraphManager)1 BadRequestException (jakarta.ws.rs.BadRequestException)1 NotAuthorizedException (jakarta.ws.rs.NotAuthorizedException)1 HashMap (java.util.HashMap)1 AuthenticationException (org.apache.tinkerpop.gremlin.server.auth.AuthenticationException)1 Request (org.glassfish.grizzly.http.server.Request)1