All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.graphscope.groot.sdk.BasicAuth Maven / Gradle / Ivy

There is a newer version: 0.28.1
Show newest version
package com.alibaba.graphscope.groot.sdk;

import io.grpc.CallCredentials;
import io.grpc.Metadata;
import io.grpc.Status;

import java.util.Base64;
import java.util.concurrent.Executor;

public class BasicAuth extends CallCredentials {
    public static final Metadata.Key AUTH_KEY =
            Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER);
    public static final String TOKEN_HEAD = "Basic ";

    private String secret;

    public BasicAuth(String username, String password) {
        this.secret = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
    }

    @Override
    public void applyRequestMetadata(
            RequestInfo requestInfo, Executor executor, MetadataApplier metadataApplier) {
        executor.execute(
                () -> {
                    try {
                        Metadata metadata = new Metadata();
                        metadata.put(AUTH_KEY, TOKEN_HEAD + secret);
                        metadataApplier.apply(metadata);
                    } catch (Throwable t) {
                        metadataApplier.fail(Status.UNAUTHENTICATED.withCause(t));
                    }
                });
    }

    @Override
    public void thisUsesUnstableApi() {}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy