Validate a token string against the introspection endpoint, then parse it and store it in the local cache if caching is enabled.
268 String introspectionUrl;
269 RegisteredClient client;
273 }
catch (IllegalArgumentException e) {
274 logger.error(
"Unable to load introspection URL or client configuration", e);
279 String validatedToken = null;
281 RestTemplate restTemplate;
282 MultiValueMap<String, String> form =
new LinkedMultiValueMap<>();
284 final String clientId = client.getClientId();
285 final String clientSecret = client.getClientSecret();
287 if (SECRET_BASIC.equals(client.getTokenEndpointAuthMethod())){
289 restTemplate =
new RestTemplate(
factory) {
292 protected ClientHttpRequest createRequest(URI url, HttpMethod method)
throws IOException {
293 ClientHttpRequest httpRequest = super.createRequest(url, method);
294 httpRequest.getHeaders().add(
"Authorization",
295 String.format(
"Basic %s", Base64.encode(String.format(
"%s:%s", clientId, clientSecret)) ));
300 restTemplate =
new RestTemplate(
factory);
302 form.add(
"client_id", clientId);
303 form.add(
"client_secret", clientSecret);
306 form.add(
"token", accessToken);
309 validatedToken = restTemplate.postForObject(introspectionUrl, form, String.class);
310 }
catch (RestClientException rce) {
311 logger.error(
"validateToken", rce);
314 if (validatedToken != null) {
316 JsonElement jsonRoot =
new JsonParser().parse(validatedToken);
317 if (!jsonRoot.isJsonObject()) {
321 JsonObject tokenResponse = jsonRoot.getAsJsonObject();
323 if (tokenResponse.get(
"error") != null) {
325 logger.error(
"Got an error back: " + tokenResponse.get(
"error") +
", " + tokenResponse.get(
"error_description"));
329 if (!tokenResponse.get(
"active").getAsBoolean()) {
331 logger.info(
"Server returned non-active token");
339 if (token.getExpiration() == null || token.getExpiration().after(
new Date())) {
341 TokenCacheObject tco =
new TokenCacheObject(token, auth);
String getIntrospectionUrl(String accessToken)
boolean cacheTokens
Definition: IntrospectingTokenService.java:75
Authentication createUserAuthentication(JsonObject token)
Definition: IntrospectingTokenService.java:244
HttpComponentsClientHttpRequestFactory factory
Definition: IntrospectingTokenService.java:77
RegisteredClient getClientConfiguration(String accessToken)
static final Logger logger
Definition: IntrospectingTokenService.java:114
OAuth2Request createStoredRequest(final JsonObject token)
Definition: IntrospectingTokenService.java:231
OAuth2AccessToken createAccessToken(final JsonObject token, final String tokenString)
Definition: IntrospectingTokenService.java:253
Map< String, TokenCacheObject > authCache
Definition: IntrospectingTokenService.java:110
IntrospectionConfigurationService introspectionConfigurationService
Definition: IntrospectingTokenService.java:69
boolean cacheNonExpiringTokens
Definition: IntrospectingTokenService.java:74