Search in sources :

Example 1 with AuthenticateRequest

use of io.etcd.jetcd.api.AuthenticateRequest in project jetcd by coreos.

the class AuthCredential method authenticate.

private void authenticate(MetadataApplier applier) {
    checkArgument(!manager.builder().user().isEmpty(), "username can not be empty.");
    checkArgument(!manager.builder().password().isEmpty(), "password can not be empty.");
    VertxAuthGrpc.AuthVertxStub authFutureStub = VertxAuthGrpc.newVertxStub(this.manager.getChannel());
    List<ClientInterceptor> interceptorsChain = new ArrayList<>();
    if (manager.builder().authHeaders() != null) {
        Metadata metadata = new Metadata();
        manager.builder().authHeaders().forEach((BiConsumer<Metadata.Key, Object>) metadata::put);
        interceptorsChain.add(MetadataUtils.newAttachHeadersInterceptor(metadata));
    }
    if (manager.builder().authInterceptors() != null) {
        interceptorsChain.addAll(manager.builder().authInterceptors());
    }
    if (!interceptorsChain.isEmpty()) {
        authFutureStub = authFutureStub.withInterceptors(interceptorsChain.toArray(new ClientInterceptor[0]));
    }
    final ByteString user = ByteString.copyFrom(this.manager.builder().user().getBytes());
    final ByteString pass = ByteString.copyFrom(this.manager.builder().password().getBytes());
    AuthenticateRequest request = AuthenticateRequest.newBuilder().setNameBytes(user).setPasswordBytes(pass).build();
    try {
        authFutureStub.authenticate(request).onFailure(t -> {
            applier.fail(Status.UNAUTHENTICATED.withCause(t));
        }).onSuccess(h -> {
            Metadata meta = new Metadata();
            meta.put(TOKEN, h.getToken());
            this.meta = meta;
            applier.apply(this.meta);
        });
    } catch (Exception e) {
        applier.fail(Status.UNAUTHENTICATED.withCause(e));
    }
}
Also used : CallCredentials(io.grpc.CallCredentials) AuthenticateRequest(io.etcd.jetcd.api.AuthenticateRequest) Executor(java.util.concurrent.Executor) ClientInterceptor(io.grpc.ClientInterceptor) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) MetadataUtils(io.grpc.stub.MetadataUtils) BiConsumer(java.util.function.BiConsumer) VertxAuthGrpc(io.etcd.jetcd.api.VertxAuthGrpc) Metadata(io.grpc.Metadata) Status(io.grpc.Status) AuthenticateRequest(io.etcd.jetcd.api.AuthenticateRequest) ByteString(com.google.protobuf.ByteString) ClientInterceptor(io.grpc.ClientInterceptor) ArrayList(java.util.ArrayList) Metadata(io.grpc.Metadata) VertxAuthGrpc(io.etcd.jetcd.api.VertxAuthGrpc)

Aggregations

Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 ByteString (com.google.protobuf.ByteString)1 AuthenticateRequest (io.etcd.jetcd.api.AuthenticateRequest)1 VertxAuthGrpc (io.etcd.jetcd.api.VertxAuthGrpc)1 CallCredentials (io.grpc.CallCredentials)1 ClientInterceptor (io.grpc.ClientInterceptor)1 Metadata (io.grpc.Metadata)1 Status (io.grpc.Status)1 MetadataUtils (io.grpc.stub.MetadataUtils)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Executor (java.util.concurrent.Executor)1 BiConsumer (java.util.function.BiConsumer)1