-
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.
client credentials workflow implementation
- Loading branch information
Showing
8 changed files
with
356 additions
and
10 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
40 changes: 40 additions & 0 deletions
40
...izininformatik_initiative/processes/common/fhir/client/interceptor/OAuth2Interceptor.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,40 @@ | ||
package de.medizininformatik_initiative.processes.common.fhir.client.interceptor; | ||
|
||
import java.util.Objects; | ||
|
||
import org.springframework.beans.factory.InitializingBean; | ||
|
||
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 de.medizininformatik_initiative.processes.common.fhir.client.token.TokenProvider; | ||
|
||
public class OAuth2Interceptor implements IClientInterceptor, InitializingBean | ||
{ | ||
private final TokenProvider tokenProvider; | ||
|
||
public OAuth2Interceptor(TokenProvider tokenProvider) | ||
{ | ||
this.tokenProvider = tokenProvider; | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() | ||
{ | ||
Objects.requireNonNull(tokenProvider, "tokenProvider"); | ||
} | ||
|
||
@Override | ||
public void interceptRequest(IHttpRequest theRequest) | ||
{ | ||
theRequest.addHeader(Constants.HEADER_AUTHORIZATION, | ||
Constants.HEADER_AUTHORIZATION_VALPREFIX_BEARER + tokenProvider.getToken()); | ||
} | ||
|
||
@Override | ||
public void interceptResponse(IHttpResponse theResponse) | ||
{ | ||
// do not intercept response | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
.../java/de/medizininformatik_initiative/processes/common/fhir/client/token/AccessToken.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 de.medizininformatik_initiative.processes.common.fhir.client.token; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class AccessToken | ||
{ | ||
private static final int BUFFER = 10; | ||
|
||
private final String token; | ||
|
||
private final LocalDateTime expiresAt; | ||
|
||
@JsonCreator | ||
public AccessToken(@JsonProperty("access_token") String token, @JsonProperty("expires_in") int expiresIn) | ||
{ | ||
this.token = token; | ||
this.expiresAt = LocalDateTime.now().plusSeconds(expiresIn); | ||
} | ||
|
||
public String get() | ||
{ | ||
return token; | ||
} | ||
|
||
public boolean isExpired() | ||
{ | ||
return LocalDateTime.now().plusSeconds(BUFFER).isAfter(expiresAt); | ||
} | ||
} |
Oops, something went wrong.