Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hard dependency on Jackson in OAuth 2 support #969

Merged
merged 1 commit into from
Feb 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.TrustEverythingTrustManager;

import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import javax.net.ssl.*;
import java.io.*;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -72,7 +74,7 @@ public class OAuth2ClientCredentialsGrantCredentialsProvider extends RefreshProt

private final Map<String, String> parameters;

private final ObjectMapper objectMapper = new ObjectMapper();
private final AtomicReference<Function<String, Token>> tokenExtractor = new AtomicReference<>();

private final String id;

Expand Down Expand Up @@ -214,14 +216,8 @@ protected String usernameFromToken(Token token) {
}

protected Token parseToken(String response) {
try {
Map<?, ?> map = objectMapper.readValue(response, Map.class);
int expiresIn = ((Number) map.get("expires_in")).intValue();
Instant receivedAt = Instant.now();
return new Token(map.get("access_token").toString(), expiresIn, receivedAt);
} catch (IOException e) {
throw new OAuthTokenManagementException("Error while parsing OAuth 2 token", e);
}
return this.tokenExtractor.updateAndGet(current ->
current == null ? new JacksonTokenLookup() : current).apply(response);
}

@Override
Expand Down Expand Up @@ -595,4 +591,21 @@ private SSLSocketFactory sslSocketFactory() {
}

}
}

private static class JacksonTokenLookup implements Function<String, Token> {

private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public Token apply(String response) {
try {
Map<?, ?> map = objectMapper.readValue(response, Map.class);
int expiresIn = ((Number) map.get("expires_in")).intValue();
Instant receivedAt = Instant.now();
return new Token(map.get("access_token").toString(), expiresIn, receivedAt);
} catch (IOException e) {
throw new OAuthTokenManagementException("Error while parsing OAuth 2 token", e);
}
}
}
}