-
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.
Loading status checks…
Spring boot 3 upgrade (#4)
* upgrade for support of spring boot 3 * upgrade to spring boot 3.3.5 * convert to Record & add JsonProperties * upgrade gradle wrapper * use Configuration annotation * add ConditionalOnProperty directly * delete OAuthConfig * Change tests from Groovy/Spock into Java/Junit * remove conditioal property * set expiresIn to unixTimeStampInSeconds * fix test to make sure authToken is UnixTimeStampInSeconds * change expired logic to use Duration between and set buffer to 30 seconds * remove unused restClient parameter * change variable name * fix test * correctly set conditionalProperty * get RestClient in constructor * fix test * remove durable debug logging * change variable name and formatting * create test to get new token when it has expired * create default restclient * remove unused dependency * change property value * change version * refactor * throw IllegalStateException if token fetching failed * ignore a test * yes tired sry --------- Co-authored-by: Hknots <[email protected]>
1 parent
6ae3d9c
commit 6671b71
Showing
22 changed files
with
436 additions
and
329 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version = '1.3.0-SNAPSHOT' | ||
version=1.3.0-SNAPSHOT |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
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,21 @@ | ||
package no.fint.oauth; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record AuthToken( | ||
@JsonProperty("access_token") String accessToken, | ||
@JsonProperty("token_type") String tokenType, | ||
@JsonProperty("expires_in") long expirationTimestampMillis, | ||
@JsonProperty("acr") String acr, | ||
@JsonProperty("scope") String scope | ||
) { | ||
|
||
public AuthToken(String accessToken, String tokenType, long expirationTimestampMillis, String acr, String scope) { | ||
this.accessToken = accessToken; | ||
this.tokenType = tokenType; | ||
this.expirationTimestampMillis = System.currentTimeMillis() + (expirationTimestampMillis * 1000); | ||
this.acr = acr; | ||
this.scope = scope; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,81 @@ | ||
package no.fint.oauth; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.util.ObjectUtils; | ||
import org.springframework.web.client.RestClient; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
|
||
@Slf4j | ||
@Component | ||
@ConditionalOnProperty(value = "fint.oauth.enabled", havingValue = "true") | ||
public class TokenInstance { | ||
|
||
private final RestClient oauthRestClient; | ||
private final OAuthTokenProps props; | ||
private final MultiValueMap<String, String> formData; | ||
private AuthToken authToken; | ||
|
||
public TokenInstance(OAuthTokenProps props, RestClient oauthRestClient) { | ||
this.props = props; | ||
this.oauthRestClient = oauthRestClient; | ||
this.formData = createFormData(); | ||
} | ||
|
||
@PostConstruct | ||
public void init() { | ||
if (ObjectUtils.isEmpty(props.getAccessTokenUri())) { | ||
log.info("No Access-token-url configured, will not initialize access token"); | ||
} else { | ||
refreshToken(); | ||
} | ||
} | ||
|
||
public boolean isNull() { | ||
return authToken == null; | ||
} | ||
|
||
public boolean hasExpired() { | ||
Duration duration = Duration.between(Instant.now(), Instant.ofEpochMilli(authToken.expirationTimestampMillis())); | ||
return duration.isNegative() || duration.getSeconds() < 30; | ||
} | ||
|
||
public String getAccessToken() { | ||
return authToken.accessToken(); | ||
} | ||
|
||
public void refreshToken() { | ||
ResponseEntity<AuthToken> response = oauthRestClient.post() | ||
.uri(props.getAccessTokenUri()) | ||
.body(formData) | ||
.retrieve() | ||
.toEntity(AuthToken.class); | ||
if (response.getStatusCode().is2xxSuccessful()) { | ||
authToken = response.getBody(); | ||
} else { | ||
throw new IllegalStateException("Unable to refresh token"); | ||
} | ||
} | ||
|
||
private MultiValueMap<String, String> createFormData() { | ||
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(); | ||
|
||
formData.add("grant_type", "password"); | ||
formData.add("client_id", props.getClientId()); | ||
formData.add("client_secret", props.getClientSecret()); | ||
formData.add("username", props.getUsername()); | ||
formData.add("password", props.getPassword()); | ||
formData.add("scope", props.getScope()); | ||
|
||
return formData; | ||
} | ||
|
||
} |
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,15 @@ | ||
package no.fint.oauth.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@Configuration | ||
public class RestClientConfig { | ||
|
||
@Bean("oauthRestClient") | ||
public RestClient oauthRestClient() { | ||
return RestClient.builder().build(); | ||
} | ||
|
||
} |
21 changes: 0 additions & 21 deletions
21
src/test/groovy/no/fint/oauth/OAuthConfigJsonIntegrationSpec.groovy
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.