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 Proxy Credential Facotry from Intermediate …
…Class Signed-off-by: Sven Strittmatter <[email protected]>
- Loading branch information
1 parent
574cf01
commit 2f23058
Showing
4 changed files
with
86 additions
and
56 deletions.
There are no files selected for viewing
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
34 changes: 34 additions & 0 deletions
34
src/main/java/io/securecodebox/persistence/defectdojo/http/ProxyCredentialFactory.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,34 @@ | ||
package io.securecodebox.persistence.defectdojo.http; | ||
|
||
import lombok.NonNull; | ||
import org.apache.http.auth.AuthScope; | ||
import org.apache.http.auth.Credentials; | ||
import org.apache.http.auth.UsernamePasswordCredentials; | ||
import org.apache.http.client.CredentialsProvider; | ||
import org.apache.http.impl.client.BasicCredentialsProvider; | ||
|
||
/** | ||
* Utility class to create credentials to authenticate against a HTTP proxy | ||
*/ | ||
final class ProxyCredentialFactory { | ||
private final ProxyConfig proxyConfig; | ||
|
||
ProxyCredentialFactory(@NonNull ProxyConfig proxyConfig) { | ||
super(); | ||
this.proxyConfig = proxyConfig; | ||
} | ||
|
||
CredentialsProvider createCredentialsProvider() { | ||
final var provider = new BasicCredentialsProvider(); | ||
provider.setCredentials(createAuthScope(), createCredentials()); | ||
return provider; | ||
} | ||
|
||
AuthScope createAuthScope() { | ||
return new AuthScope(proxyConfig.getHost(), proxyConfig.getPort()); | ||
} | ||
|
||
Credentials createCredentials() { | ||
return new UsernamePasswordCredentials(proxyConfig.getUser(), proxyConfig.getPassword()); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/test/java/io/securecodebox/persistence/defectdojo/http/ProxyCredentialFactoryTest.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,51 @@ | ||
package io.securecodebox.persistence.defectdojo.http; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
/** | ||
* Tests for {@link ProxyCredentialFactory} | ||
*/ | ||
class ProxyCredentialFactoryTest { | ||
private final ProxyConfig config = ProxyConfig.builder() | ||
.user("user") | ||
.password("pw") | ||
.host("host") | ||
.port(42) | ||
.build(); | ||
private final ProxyCredentialFactory sut = new ProxyCredentialFactory(config); | ||
|
||
@Test | ||
void createCredentialsProvider() { | ||
final var result = sut.createCredentialsProvider(); | ||
final var credentials = result.getCredentials(sut.createAuthScope()); | ||
|
||
assertAll( | ||
() -> assertThat(credentials.getUserPrincipal().getName(), is(config.getUser())), | ||
() -> assertThat(credentials.getPassword(), is(config.getPassword())) | ||
); | ||
} | ||
|
||
@Test | ||
void createAuthScope() { | ||
final var result = sut.createAuthScope(); | ||
|
||
assertAll( | ||
() -> assertThat(result.getHost(), is(config.getHost())), | ||
() -> assertThat(result.getPort(), is(config.getPort())) | ||
); | ||
} | ||
|
||
@Test | ||
void createCredentials() { | ||
final var result = sut.createCredentials(); | ||
|
||
assertAll( | ||
() -> assertThat(result.getUserPrincipal().getName(), is(config.getUser())), | ||
() -> assertThat(result.getPassword(), is(config.getPassword())) | ||
); | ||
} | ||
} |