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));
}
}
Aggregations