forked from secureCodeBox/defectdojo-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
secureCodeBox#121 Extract Auth Header Facotry from Intermediate Class
Signed-off-by: Sven Strittmatter <[email protected]>
- Loading branch information
1 parent
a662260
commit f1cc201
Showing
6 changed files
with
122 additions
and
59 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/io/securecodebox/persistence/defectdojo/http/AuthHeaderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.securecodebox.persistence.defectdojo.http; | ||
|
||
import io.securecodebox.persistence.defectdojo.config.Config; | ||
import lombok.NonNull; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
/** | ||
* Utility class to create HTTP authorization headers | ||
*/ | ||
@Slf4j | ||
public final class AuthHeaderFactory { | ||
private final Config config; | ||
@Setter | ||
@NonNull | ||
private ProxyConfig proxyConfig = ProxyConfig.NULL; | ||
|
||
public AuthHeaderFactory(@NonNull Config config) { | ||
super(); | ||
this.config = config; | ||
} | ||
|
||
/** | ||
* This method generates appropriate authorization headers | ||
* | ||
* @return never {@code null} | ||
*/ | ||
public HttpHeaders generateAuthorizationHeaders() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set(HttpHeaders.AUTHORIZATION, "Token " + this.config.getApiKey()); | ||
|
||
if (proxyConfig.isComplete()) { | ||
log.info("Setting Proxy Auth Header..."); | ||
headers.set(HttpHeaders.PROXY_AUTHORIZATION, "Basic " + encodeProxyCredentials(proxyConfig)); | ||
} | ||
|
||
return headers; | ||
} | ||
|
||
String encodeProxyCredentials(@NonNull final ProxyConfig cfg) { | ||
final var credential = String.format("%s:%s", cfg.getUser(), cfg.getPassword()); | ||
return Base64.getEncoder().encodeToString(credential.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/test/java/io/securecodebox/persistence/defectdojo/http/AuthHeaderFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.securecodebox.persistence.defectdojo.http; | ||
|
||
import io.securecodebox.persistence.defectdojo.config.Config; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.HttpHeaders; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* Tests for {@link AuthHeaderFactory} | ||
*/ | ||
class AuthHeaderFactoryTest { | ||
private final Config config = new Config("url", "apikey"); | ||
private final AuthHeaderFactory sut = new AuthHeaderFactory(config); | ||
|
||
@Test | ||
void setProxyConfig_doesNotAllowNull() { | ||
assertThrows(NullPointerException.class, () -> sut.setProxyConfig(null)); | ||
} | ||
|
||
@Test | ||
void generateAuthorizationHeaders_withoutProxyAuth() { | ||
assertAll( | ||
() -> assertThat( | ||
sut.generateAuthorizationHeaders().get(HttpHeaders.AUTHORIZATION), | ||
contains("Token apikey")), | ||
() -> assertThat( | ||
sut.generateAuthorizationHeaders().get(HttpHeaders.PROXY_AUTHORIZATION), | ||
not(contains("Basic dXNlcjpwdw=="))) | ||
); | ||
} | ||
|
||
@Test | ||
void generateAuthorizationHeaders_withProxyAuth() { | ||
final ProxyConfig proxyConfig = ProxyConfig.builder() | ||
.user("user") | ||
.password("pw") | ||
.host("host") | ||
.port(42) | ||
.build(); | ||
sut.setProxyConfig(proxyConfig); | ||
|
||
assertAll( | ||
() -> assertThat( | ||
sut.generateAuthorizationHeaders().get(HttpHeaders.AUTHORIZATION), | ||
contains("Token apikey")), | ||
() -> assertThat( | ||
sut.generateAuthorizationHeaders().get(HttpHeaders.PROXY_AUTHORIZATION), | ||
contains("Basic dXNlcjpwdw==")) | ||
); | ||
} | ||
|
||
@Test | ||
void encodeProxyCredentials() { | ||
final var proxyConfig = ProxyConfig.builder() | ||
.user("bärtram") | ||
.password("gohze8Ae") | ||
.build(); | ||
|
||
assertThat(sut.encodeProxyCredentials(proxyConfig), is("YsOkcnRyYW06Z29oemU4QWU=")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters