Skip to content

Commit c0811fa

Browse files
committed
Remove hard dependency on Jackson in OAuth 2 support
It's possible to override the token parsing method, to avoid using Jackson, but the class would have a Jackson mapper in its properties. This commit introduces an abstraction to parse the token and the default Jackson implementation is instantianted on the fly.
1 parent af8036d commit c0811fa

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

Diff for: src/main/java/com/rabbitmq/client/impl/OAuth2ClientCredentialsGrantCredentialsProvider.java

+23-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import com.fasterxml.jackson.databind.ObjectMapper;
1919
import com.rabbitmq.client.TrustEverythingTrustManager;
2020

21+
import java.util.concurrent.atomic.AtomicReference;
22+
import java.util.function.Function;
2123
import javax.net.ssl.*;
2224
import java.io.*;
2325
import java.net.HttpURLConnection;
@@ -72,7 +74,7 @@ public class OAuth2ClientCredentialsGrantCredentialsProvider extends RefreshProt
7274

7375
private final Map<String, String> parameters;
7476

75-
private final ObjectMapper objectMapper = new ObjectMapper();
77+
private final AtomicReference<Function<String, Token>> tokenExtractor = new AtomicReference<>();
7678

7779
private final String id;
7880

@@ -214,14 +216,8 @@ protected String usernameFromToken(Token token) {
214216
}
215217

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

227223
@Override
@@ -595,4 +591,21 @@ private SSLSocketFactory sslSocketFactory() {
595591
}
596592

597593
}
598-
}
594+
595+
private static class JacksonTokenLookup implements Function<String, Token> {
596+
597+
private final ObjectMapper objectMapper = new ObjectMapper();
598+
599+
@Override
600+
public Token apply(String response) {
601+
try {
602+
Map<?, ?> map = objectMapper.readValue(response, Map.class);
603+
int expiresIn = ((Number) map.get("expires_in")).intValue();
604+
Instant receivedAt = Instant.now();
605+
return new Token(map.get("access_token").toString(), expiresIn, receivedAt);
606+
} catch (IOException e) {
607+
throw new OAuthTokenManagementException("Error while parsing OAuth 2 token", e);
608+
}
609+
}
610+
}
611+
}

0 commit comments

Comments
 (0)