-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
София Корватовская
committed
Feb 1, 2025
1 parent
cc42749
commit 8b5fd88
Showing
15 changed files
with
384 additions
and
16 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
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
25 changes: 25 additions & 0 deletions
25
src/main/java/Diadoc/Api/auth/CredentialsSchemeFactory.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,25 @@ | ||
package Diadoc.Api.auth; | ||
|
||
import Diadoc.Api.auth.oidc.DiadocOidcCredentials; | ||
import Diadoc.Api.auth.oidc.OidcAuthScheme; | ||
import org.apache.http.auth.AuthScheme; | ||
import org.apache.http.auth.Credentials; | ||
|
||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
public class CredentialsSchemeFactory { | ||
|
||
private static final Map<Class<? extends Credentials>, Supplier<AuthScheme>> SCHEME_SUPPLIERS = Map.of( | ||
DiadocOidcCredentials.class, OidcAuthScheme::new, | ||
DiadocCredentials.class, DiadocAuthScheme::new | ||
); | ||
|
||
public AuthScheme createScheme(Credentials credentials) { | ||
Supplier<AuthScheme> schemeSupplier = SCHEME_SUPPLIERS.get(credentials.getClass()); | ||
if (schemeSupplier == null) { | ||
throw new IllegalArgumentException("Unsupported credentials type: " + credentials.getClass().getName()); | ||
} | ||
return schemeSupplier.get(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package Diadoc.Api.auth; | ||
|
||
import Diadoc.Api.httpClient.DiadocHttpClient; | ||
import org.apache.http.client.CredentialsProvider; | ||
|
||
public interface IAuthManager { | ||
|
||
AuthenticateClient createAuthenticateClient(DiadocHttpClient httpClient); | ||
|
||
CredentialsProvider getCredentialsProvider(); | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/Diadoc/Api/auth/oidc/ContainerTokenProvider.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 Diadoc.Api.auth.oidc; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Default implementation | ||
*/ | ||
public class ContainerTokenProvider implements TokenProvider { | ||
|
||
private volatile String token; | ||
|
||
@Override | ||
public String getToken() { | ||
return token; | ||
} | ||
|
||
public void setToken(String token) { | ||
this.token = token; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
ContainerTokenProvider that = (ContainerTokenProvider) o; | ||
return Objects.equals(token, that.token); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(token); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/Diadoc/Api/auth/oidc/DiadocOidcCredentials.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,35 @@ | ||
package Diadoc.Api.auth.oidc; | ||
|
||
import org.apache.http.annotation.Contract; | ||
import org.apache.http.annotation.ThreadingBehavior; | ||
import org.apache.http.auth.BasicUserPrincipal; | ||
import org.apache.http.auth.Credentials; | ||
|
||
import java.security.Principal; | ||
|
||
@Contract(threading = ThreadingBehavior.IMMUTABLE) | ||
public class DiadocOidcCredentials implements Credentials { | ||
|
||
private final TokenProvider tokenProvider; | ||
private final Principal userPrincipal; | ||
|
||
public DiadocOidcCredentials(TokenProvider tokenProvider) { | ||
this.tokenProvider = tokenProvider; | ||
this.userPrincipal = new BasicUserPrincipal("DefaultUser"); | ||
} | ||
|
||
|
||
public TokenProvider getTokenProvider() { | ||
return tokenProvider; | ||
} | ||
|
||
@Override | ||
public Principal getUserPrincipal() { | ||
return userPrincipal; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return ""; | ||
} | ||
} |
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,37 @@ | ||
package Diadoc.Api.auth.oidc; | ||
|
||
import Diadoc.Api.auth.AuthenticateClient; | ||
import Diadoc.Api.auth.IAuthManager; | ||
import Diadoc.Api.httpClient.DiadocHttpClient; | ||
import org.apache.http.auth.AuthScope; | ||
import org.apache.http.client.CredentialsProvider; | ||
import org.apache.http.impl.client.BasicCredentialsProvider; | ||
|
||
import java.util.Objects; | ||
|
||
public class OidcAuthManager implements IAuthManager { | ||
|
||
private final TokenProvider tokenProvider; | ||
private final CredentialsProvider credentialsProvider; | ||
|
||
public OidcAuthManager(TokenProvider tokenProvider) { | ||
Objects.requireNonNull(tokenProvider, "tokenProvider cannot be null"); | ||
this.tokenProvider = tokenProvider; | ||
credentialsProvider = new BasicCredentialsProvider(); | ||
credentialsProvider.setCredentials(AuthScope.ANY, new DiadocOidcCredentials(tokenProvider)); | ||
} | ||
|
||
@Override | ||
public AuthenticateClient createAuthenticateClient(DiadocHttpClient httpClient) { | ||
return new OidcAuthenticateClient(); | ||
} | ||
|
||
@Override | ||
public CredentialsProvider getCredentialsProvider() { | ||
return credentialsProvider; | ||
} | ||
|
||
public TokenProvider getTokenProvider() { | ||
return tokenProvider; | ||
} | ||
} |
Oops, something went wrong.