-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from medizininformatik-initiative/release/v5.0.0
Release v5.0.0
- Loading branch information
Showing
13 changed files
with
4,546 additions
and
45 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
15 changes: 15 additions & 0 deletions
15
src/main/java/de/numcodex/feasibility_gui_backend/query/broker/OAuthClientException.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,15 @@ | ||
package de.numcodex.feasibility_gui_backend.query.broker; | ||
|
||
public class OAuthClientException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = -5840162115734733430L; | ||
|
||
public OAuthClientException(String message) { | ||
super(message); | ||
} | ||
|
||
public OAuthClientException(String message, Exception cause) { | ||
super(message, cause); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/de/numcodex/feasibility_gui_backend/query/broker/OAuthInterceptor.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,81 @@ | ||
package de.numcodex.feasibility_gui_backend.query.broker; | ||
|
||
import ca.uhn.fhir.rest.api.Constants; | ||
import ca.uhn.fhir.rest.client.api.IClientInterceptor; | ||
import ca.uhn.fhir.rest.client.api.IHttpRequest; | ||
import ca.uhn.fhir.rest.client.api.IHttpResponse; | ||
import com.nimbusds.oauth2.sdk.AccessTokenResponse; | ||
import com.nimbusds.oauth2.sdk.ClientCredentialsGrant; | ||
import com.nimbusds.oauth2.sdk.GeneralException; | ||
import com.nimbusds.oauth2.sdk.TokenErrorResponse; | ||
import com.nimbusds.oauth2.sdk.TokenRequest; | ||
import com.nimbusds.oauth2.sdk.TokenResponse; | ||
import com.nimbusds.oauth2.sdk.auth.ClientSecretBasic; | ||
import com.nimbusds.oauth2.sdk.auth.Secret; | ||
import com.nimbusds.oauth2.sdk.http.HTTPRequest; | ||
import com.nimbusds.oauth2.sdk.id.ClientID; | ||
import com.nimbusds.oauth2.sdk.id.Issuer; | ||
import com.nimbusds.oauth2.sdk.token.AccessToken; | ||
import com.nimbusds.openid.connect.sdk.op.OIDCProviderMetadata; | ||
import lombok.NonNull; | ||
import org.joda.time.DateTime; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
public final class OAuthInterceptor implements IClientInterceptor { | ||
|
||
private static final int TOKEN_EXPIRY_THRESHOLD = 5000; | ||
private HTTPRequest tokenRequest; | ||
private AccessToken token; | ||
private DateTime tokenExpiry; | ||
private Issuer issuer; | ||
private ClientSecretBasic clientAuth; | ||
|
||
public OAuthInterceptor(@NonNull String oauthIssuerUrl, @NonNull String oauthClientId, | ||
@NonNull String oauthClientSecret) { | ||
clientAuth = new ClientSecretBasic(new ClientID(oauthClientId), new Secret(oauthClientSecret)); | ||
issuer = new Issuer(oauthIssuerUrl); | ||
} | ||
|
||
public String getToken() { | ||
if (token == null || DateTime.now().plus(TOKEN_EXPIRY_THRESHOLD).isAfter(tokenExpiry)) { | ||
try { | ||
TokenResponse response = TokenResponse.parse(getTokenRequest().send()); | ||
if (!response.indicatesSuccess()) { | ||
TokenErrorResponse errorResponse = response.toErrorResponse(); | ||
throw new OAuthClientException(errorResponse.getErrorObject().getCode() + " - " | ||
+ errorResponse.getErrorObject().getDescription()); | ||
} | ||
AccessTokenResponse successResponse = response.toSuccessResponse(); | ||
|
||
token = successResponse.getTokens().getAccessToken(); | ||
tokenExpiry = DateTime.now().plus(token.getLifetime() * 1000); | ||
} catch (GeneralException | IOException e) { | ||
throw new OAuthClientException("Request for OAuth2 access token failed", e); | ||
} | ||
} | ||
return token.getValue(); | ||
} | ||
|
||
private HTTPRequest getTokenRequest() throws GeneralException, IOException { | ||
if (tokenRequest == null) { | ||
tokenRequest = new TokenRequest(getTokenUri(), clientAuth, new ClientCredentialsGrant()).toHTTPRequest(); | ||
} | ||
return tokenRequest; | ||
} | ||
|
||
private URI getTokenUri() throws GeneralException, IOException { | ||
return OIDCProviderMetadata.resolve(issuer).getTokenEndpointURI(); | ||
} | ||
|
||
@Override | ||
public void interceptRequest(IHttpRequest theRequest) { | ||
theRequest.addHeader(Constants.HEADER_AUTHORIZATION, | ||
Constants.HEADER_AUTHORIZATION_VALPREFIX_BEARER + getToken()); | ||
} | ||
|
||
@Override | ||
public void interceptResponse(IHttpResponse theResponse) throws IOException { | ||
} | ||
} |
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
Oops, something went wrong.