From bccef328c71d272eef1242073df8debd6a823a06 Mon Sep 17 00:00:00 2001 From: Bogdan Irimie Date: Wed, 30 Aug 2023 18:11:54 +0300 Subject: [PATCH] Enable the usage of bearer tokens (#14) --- pom.xml | 2 +- src/main/java/com/aserto/ChannelBuilder.java | 12 ++++++++++++ src/main/java/com/aserto/model/Config.java | 14 ++++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 0e7b87a..3ec2eac 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.aserto aserto-java - 0.20.7 + 0.20.8 ${project.groupId}:${project.artifactId} Java SDK to interact with aserto services diff --git a/src/main/java/com/aserto/ChannelBuilder.java b/src/main/java/com/aserto/ChannelBuilder.java index 77d6f9e..2fe06fd 100644 --- a/src/main/java/com/aserto/ChannelBuilder.java +++ b/src/main/java/com/aserto/ChannelBuilder.java @@ -48,6 +48,12 @@ public ChannelBuilder withAPIKeyAuth(String apiKey) { return this; } + public ChannelBuilder withTokenAuth(String token) { + cfg.setToken(token); + + return this; + } + public ChannelBuilder withInsecure(Boolean insecure) { cfg.setInsecure(insecure); @@ -69,8 +75,14 @@ public ManagedChannel build() throws SSLException { metadata.put(asertoTenantId, cfg.getTenantId()); } + if (cfg.getApiKey() != null && cfg.getToken() != null) { + throw new IllegalArgumentException("ApiKey and Token cannot be both specified"); + } + if (cfg.getApiKey() != null) { metadata.put(authorization, "basic " + cfg.getApiKey()); + } else if (cfg.getToken() != null) { + metadata.put(authorization, "bearer " + cfg.getToken()); } NettyChannelBuilder channelBuilder = NettyChannelBuilder diff --git a/src/main/java/com/aserto/model/Config.java b/src/main/java/com/aserto/model/Config.java index 12821b9..84230c0 100644 --- a/src/main/java/com/aserto/model/Config.java +++ b/src/main/java/com/aserto/model/Config.java @@ -5,17 +5,19 @@ public class Config { private int port; private String apiKey; private String tenantId; + private String token; private Boolean insecure = false; - private String caCertPath = ""; + private String caCertPath; public Config() { } - public Config(String host, int port, String apiKey, String tenantID, Boolean insecure, String caCertPath) { + public Config(String host, int port, String apiKey, String tenantID, String token, Boolean insecure, String caCertPath) { this.host = host; this.port = port; this.apiKey = apiKey; this.tenantId = tenantID; + this.token = token; this.insecure = insecure; this.caCertPath = caCertPath; } @@ -52,6 +54,14 @@ public void setTenantId(String tenantId) { this.tenantId = tenantId; } + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + public Boolean getInsecure() { return insecure; }