Skip to content

Commit

Permalink
Add support for loading token by filepath or environment var
Browse files Browse the repository at this point in the history
  • Loading branch information
micahlee committed Dec 21, 2018
1 parent abc8820 commit fb7a22c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,16 @@ Conjur conjur = new Conjur(credentials);

#### Authorization Token
```java
String authTokenString = new String(Files.readAllBytes(Paths.get('path/to/conjur/authentication/token.json')));
Token token = Token.fromJson(authTokenString);
Token token = Token.fromFile(Paths.get('path/to/conjur/authentication/token.json'));
Conjur conjur = new Conjur(token);
```

Alternatively, to use the `CONJUR_AUTHN_TOKEN_FILE` environment variable:
```bash
export CONJUR_AUTHN_TOKEN_FILE="path/to/conjur/authentication/token.json"
```
```java
Token token = Token.fromEnv();
Conjur conjur = new Conjur(token);
```

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/conjur/api/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Token {
private static final DateTimeFormatter DATE_TIME_FORMATTER =
// tokens use dates like 2013-10-01 18:48:32 UTC
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss ZZZ");
private static final String TOKEN_FILE_ENV_VARIABLE = "CONJUR_AUTHN_TOKEN_FILE";

// Hold our fields in here to facilitate json serialization/deserialization
private static class Fields {
Expand Down Expand Up @@ -123,6 +124,16 @@ public static Token fromJson(String json){
return new Token(json);
}

public static Token fromFile(Path filepath) {
String json = File.readAllBytes(filepath);
return fromJson(json);
}

public static Token fromEnv() {
String tokenFilePath = System.getenv(TOKEN_FILE_ENV_VARIABLE);
return fromFile(Paths.get(tokenFilePath));
}

private String fromBase64(String base64){
return new String(Base64.decodeBase64(base64), StandardCharsets.UTF_8);
}
Expand Down

0 comments on commit fb7a22c

Please sign in to comment.