Skip to content

Commit

Permalink
Enable the usage of bearer tokens (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanIrimie authored Aug 30, 2023
1 parent bebaa6a commit bccef32
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.aserto</groupId>
<artifactId>aserto-java</artifactId>
<version>0.20.7</version>
<version>0.20.8</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>Java SDK to interact with aserto services</description>
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/aserto/ChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/aserto/model/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit bccef32

Please sign in to comment.