Skip to content

Commit

Permalink
add api key auth and implement for nabu plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
grapigeau committed Sep 12, 2024
1 parent 7e5b22d commit e82856a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ArchiveCaseAction extends JsonParameterisedAction {
static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient();
private final ObjectNode parameters;
private final ObjectNode rootParameters = MAPPER.createObjectNode();
private Optional<String> authenticationHeader = Optional.empty();

private static final Counter NabuRequestErrors =
Counter.build(
Expand Down Expand Up @@ -172,21 +173,29 @@ public ActionState perform(
HttpRequest.BodyPublisher body;
try {
body = HttpRequest.BodyPublishers.ofString(createRequestBody());
final var authentication = owner.get().NabuToken();
authenticationHeader =
authentication == null ? Optional.empty() : Optional.of(authentication);
} catch (final Exception e) {
e.printStackTrace();
this.errors = Collections.singletonList(e.getMessage());
return ActionState.FAILED;
}
final var baseUrl = owner.get().NabuUrl();

final var builder = HttpRequest.newBuilder(URI.create(baseUrl + "/case"));

authenticationHeader.ifPresent(header -> builder.header("X-API-KEY", header));

final var request =
HttpRequest.newBuilder(URI.create(baseUrl + "/case"))
.header("Content-type", "application/json")
.header("Accept", "application/json")
.POST(body)
.build();
builder
.header("Content-type", "application/json")
.header("Accept", "application/json")
.POST(body)
.build();

owner.log("NABU REQUEST: " + request, LogLevel.DEBUG, null);

try (var timer = NabuRequestTime.start(baseUrl)) {
var response =
HTTP_CLIENT.send(request, new JsonListBodyHandler<>(MAPPER, NabuCaseArchiveDto.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package ca.on.oicr.gsi.shesmu.nabu;

import ca.on.oicr.gsi.shesmu.plugin.authentication.AuthenticationConfiguration;

public class NabuConfiguration {

private AuthenticationConfiguration authentication;
private String url;

public AuthenticationConfiguration getAuthentication() {
return authentication;
}

public String getUrl() {
return url;
}

public void setAuthentication(AuthenticationConfiguration authentication) {
this.authentication = authentication;
}

public void setUrl(String url) {
this.url = url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,12 @@ protected Optional<Integer> update(NabuConfiguration value) {
public String NabuUrl() {
return config.get().getUrl();
}

public String NabuToken() throws RuntimeException {
try {
return config.get().getAuthentication().prepareAuthentication();
} catch (IOException e) {
throw new RuntimeException("Unable to get authentication method");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
@JsonSubTypes({
@JsonSubTypes.Type(value = AuthenticationConfigurationBasic.class, name = "basic"),
@JsonSubTypes.Type(value = AuthenticationConfigurationBearer.class, name = "bearer"),
@JsonSubTypes.Type(value = AuthenticationConfigurationAPIkey.class, name = "apikey"),
@JsonSubTypes.Type(value = AuthenticationConfigurationFileBasic.class, name = "basic-file"),
@JsonSubTypes.Type(value = AuthenticationConfigurationFileBearer.class, name = "bearer-file")
@JsonSubTypes.Type(value = AuthenticationConfigurationFileBearer.class, name = "bearer-file"),
@JsonSubTypes.Type(value = AuthenticationConfigurationFileAPIkey.class, name = "apikey-file")
})
public abstract sealed class AuthenticationConfiguration
permits AuthenticationConfigurationBasic,
AuthenticationConfigurationBearer,
AuthenticationConfigurationAPIkey,
AuthenticationConfigurationFileBasic,
AuthenticationConfigurationFileBearer {
AuthenticationConfigurationFileBearer,
AuthenticationConfigurationFileAPIkey {

public static void addAuthenticationHeader(
AuthenticationConfiguration authentication, Builder request) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ca.on.oicr.gsi.shesmu.plugin.authentication;

import java.io.IOException;

public final class AuthenticationConfigurationAPIkey extends AuthenticationConfiguration {
private String apikey;

public String getAPIkey() {
return apikey;
}

@Override
public String prepareAuthentication() throws IOException {
return apikey;
}

public void setAPIkey(String apikey) {
this.apikey = apikey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ca.on.oicr.gsi.shesmu.plugin.authentication;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public final class AuthenticationConfigurationFileAPIkey extends AuthenticationConfiguration {
private String apikeyFile;

public String getAPIkeyFile() {
return apikeyFile;
}

@Override
public String prepareAuthentication() throws IOException {
return Files.readString(Paths.get(apikeyFile)).trim();
}

public void setAPIkeyFile(String apikeyFile) {
this.apikeyFile = apikeyFile;
}
}

0 comments on commit e82856a

Please sign in to comment.