mitreid-connect
クラス | 公開メンバ関数 | 非公開メンバ関数 | 非公開変数類 | 静的非公開変数類 | 全メンバ一覧
org.mitre.oauth2.introspectingfilter.IntrospectingTokenService クラス
org.mitre.oauth2.introspectingfilter.IntrospectingTokenService の継承関係図
Inheritance graph
org.mitre.oauth2.introspectingfilter.IntrospectingTokenService 連携図
Collaboration graph

クラス

class  TokenCacheObject
 

公開メンバ関数

 IntrospectingTokenService ()
 
 IntrospectingTokenService (HttpClient httpClient)
 
IntrospectionConfigurationService getIntrospectionConfigurationService ()
 
void setIntrospectionConfigurationService (IntrospectionConfigurationService introspectionUrlProvider)
 
void setIntrospectionAuthorityGranter (IntrospectionAuthorityGranter introspectionAuthorityGranter)
 
IntrospectionAuthorityGranter getIntrospectionAuthorityGranter ()
 
int getDefaultExpireTime ()
 
void setDefaultExpireTime (int defaultExpireTime)
 
boolean isForceCacheExpireTime ()
 
void setForceCacheExpireTime (boolean forceCacheExpireTime)
 
boolean isCacheNonExpiringTokens ()
 
void setCacheNonExpiringTokens (boolean cacheNonExpiringTokens)
 
boolean isCacheTokens ()
 
void setCacheTokens (boolean cacheTokens)
 
OAuth2Authentication loadAuthentication (String accessToken) throws AuthenticationException
 
OAuth2AccessToken readAccessToken (String accessToken)
 

非公開メンバ関数

TokenCacheObject checkCache (String key)
 
OAuth2Request createStoredRequest (final JsonObject token)
 
Authentication createUserAuthentication (JsonObject token)
 
OAuth2AccessToken createAccessToken (final JsonObject token, final String tokenString)
 
TokenCacheObject parseToken (String accessToken)
 

非公開変数類

IntrospectionConfigurationService introspectionConfigurationService
 
IntrospectionAuthorityGranter introspectionAuthorityGranter = new SimpleIntrospectionAuthorityGranter()
 
int defaultExpireTime = 300000
 
boolean forceCacheExpireTime = false
 
boolean cacheNonExpiringTokens = false
 
boolean cacheTokens = true
 
HttpComponentsClientHttpRequestFactory factory
 
Map< String, TokenCacheObjectauthCache = new HashMap<>()
 

静的非公開変数類

static final Logger logger = LoggerFactory.getLogger(IntrospectingTokenService.class)
 

詳解

This ResourceServerTokenServices implementation introspects incoming tokens at a server's introspection endpoint URL and passes an Authentication object along based on the response from the introspection endpoint.

著者
jricher

構築子と解体子

◆ IntrospectingTokenService() [1/2]

org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.IntrospectingTokenService ( )
inline
79  {
80  this(HttpClientBuilder.create().useSystemProperties().build());
81  }

◆ IntrospectingTokenService() [2/2]

org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.IntrospectingTokenService ( HttpClient  httpClient)
inline
83  {
84  this.factory = new HttpComponentsClientHttpRequestFactory(httpClient);
85  }
HttpComponentsClientHttpRequestFactory factory
Definition: IntrospectingTokenService.java:77

関数詳解

◆ checkCache()

TokenCacheObject org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.checkCache ( String  key)
inlineprivate

Check to see if the introspection end point response for a token has been cached locally This call will return the token if it has been cached and is still valid according to the cache expire time on the TokenCacheObject. If a cached value has been found but is expired, either by default expire times or the token's own expire time, then the token is removed from the cache and null is returned.

引数
keyis the token to check
戻り値
the cached TokenCacheObject or null
217  {
218  if (cacheTokens && authCache.containsKey(key)) {
219  TokenCacheObject tco = authCache.get(key);
220 
221  if (tco != null && tco.cacheExpire != null && tco.cacheExpire.after(new Date())) {
222  return tco;
223  } else {
224  // if the token is expired, don't keep things around.
225  authCache.remove(key);
226  }
227  }
228  return null;
229  }
boolean cacheTokens
Definition: IntrospectingTokenService.java:75
Map< String, TokenCacheObject > authCache
Definition: IntrospectingTokenService.java:110

◆ createAccessToken()

OAuth2AccessToken org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.createAccessToken ( final JsonObject  token,
final String  tokenString 
)
inlineprivate
253  {
254  OAuth2AccessToken accessToken = new OAuth2AccessTokenImpl(token, tokenString);
255  return accessToken;
256  }

◆ createStoredRequest()

OAuth2Request org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.createStoredRequest ( final JsonObject  token)
inlineprivate
231  {
232  String clientId = token.get("client_id").getAsString();
233  Set<String> scopes = new HashSet<>();
234  if (token.has("scope")) {
235  scopes.addAll(OAuth2Utils.parseParameterList(token.get("scope").getAsString()));
236  }
237  Map<String, String> parameters = new HashMap<>();
238  parameters.put("client_id", clientId);
239  parameters.put("scope", OAuth2Utils.formatParameterList(scopes));
240  OAuth2Request storedRequest = new OAuth2Request(parameters, clientId, null, true, scopes, null, null, null, null);
241  return storedRequest;
242  }

◆ createUserAuthentication()

Authentication org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.createUserAuthentication ( JsonObject  token)
inlineprivate
244  {
245  JsonElement userId = token.get("user_id");
246  if(userId == null) {
247  return null;
248  }
249 
250  return new PreAuthenticatedAuthenticationToken(userId.getAsString(), token, introspectionAuthorityGranter.getAuthorities(token));
251  }
IntrospectionAuthorityGranter introspectionAuthorityGranter
Definition: IntrospectingTokenService.java:70
List< GrantedAuthority > getAuthorities(JsonObject introspectionResponse)

◆ getDefaultExpireTime()

int org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.getDefaultExpireTime ( )
inline

get the default cache expire time in milliseconds

戻り値
148  {
149  return defaultExpireTime;
150  }
int defaultExpireTime
Definition: IntrospectingTokenService.java:72

◆ getIntrospectionAuthorityGranter()

IntrospectionAuthorityGranter org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.getIntrospectionAuthorityGranter ( )
inline
戻り値
the introspectionAuthorityGranter
140  {
142  }
IntrospectionAuthorityGranter introspectionAuthorityGranter
Definition: IntrospectingTokenService.java:70

◆ getIntrospectionConfigurationService()

IntrospectionConfigurationService org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.getIntrospectionConfigurationService ( )
inline
戻り値
the introspectionConfigurationService
119  {
121  }
IntrospectionConfigurationService introspectionConfigurationService
Definition: IntrospectingTokenService.java:69

◆ isCacheNonExpiringTokens()

boolean org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.isCacheNonExpiringTokens ( )
inline

Are non-expiring tokens cached using the default cache time

戻り値
state of cacheNonExpiringTokens
180  {
181  return cacheNonExpiringTokens;
182  }
boolean cacheNonExpiringTokens
Definition: IntrospectingTokenService.java:74

◆ isCacheTokens()

boolean org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.isCacheTokens ( )
inline

Is the service caching tokens, or is it hitting the introspection end point every time

戻り値
true is caching tokens locally, false hits the introspection end point every time
196  {
197  return cacheTokens;
198  }
boolean cacheTokens
Definition: IntrospectingTokenService.java:75

◆ isForceCacheExpireTime()

boolean org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.isForceCacheExpireTime ( )
inline

check if forcing a cache expire time maximum value

戻り値
the forceCacheExpireTime setting
164  {
165  return forceCacheExpireTime;
166  }
boolean forceCacheExpireTime
Definition: IntrospectingTokenService.java:73

◆ loadAuthentication()

OAuth2Authentication org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.loadAuthentication ( String  accessToken) throws AuthenticationException
inline
354  {
355  // First check if the in memory cache has an Authentication object, and
356  // that it is still valid
357  // If Valid, return it
358  TokenCacheObject cacheAuth = checkCache(accessToken);
359  if (cacheAuth != null) {
360  return cacheAuth.auth;
361  } else {
362  cacheAuth = parseToken(accessToken);
363  if (cacheAuth != null) {
364  return cacheAuth.auth;
365  } else {
366  return null;
367  }
368  }
369  }
TokenCacheObject checkCache(String key)
Definition: IntrospectingTokenService.java:217
TokenCacheObject parseToken(String accessToken)
Definition: IntrospectingTokenService.java:265

◆ parseToken()

TokenCacheObject org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.parseToken ( String  accessToken)
inlineprivate

Validate a token string against the introspection endpoint, then parse it and store it in the local cache if caching is enabled.

引数
accessTokenToken to pass to the introspection endpoint
戻り値
TokenCacheObject containing authentication and token if the token was valid, otherwise null
265  {
266 
267  // find out which URL to ask
268  String introspectionUrl;
269  RegisteredClient client;
270  try {
271  introspectionUrl = introspectionConfigurationService.getIntrospectionUrl(accessToken);
273  } catch (IllegalArgumentException e) {
274  logger.error("Unable to load introspection URL or client configuration", e);
275  return null;
276  }
277  // Use the SpringFramework RestTemplate to send the request to the
278  // endpoint
279  String validatedToken = null;
280 
281  RestTemplate restTemplate;
282  MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
283 
284  final String clientId = client.getClientId();
285  final String clientSecret = client.getClientSecret();
286 
287  if (SECRET_BASIC.equals(client.getTokenEndpointAuthMethod())){
288  // use BASIC auth if configured to do so
289  restTemplate = new RestTemplate(factory) {
290 
291  @Override
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)) ));
296  return httpRequest;
297  }
298  };
299  } else { //Alternatively use form based auth
300  restTemplate = new RestTemplate(factory);
301 
302  form.add("client_id", clientId);
303  form.add("client_secret", clientSecret);
304  }
305 
306  form.add("token", accessToken);
307 
308  try {
309  validatedToken = restTemplate.postForObject(introspectionUrl, form, String.class);
310  } catch (RestClientException rce) {
311  logger.error("validateToken", rce);
312  return null;
313  }
314  if (validatedToken != null) {
315  // parse the json
316  JsonElement jsonRoot = new JsonParser().parse(validatedToken);
317  if (!jsonRoot.isJsonObject()) {
318  return null; // didn't get a proper JSON object
319  }
320 
321  JsonObject tokenResponse = jsonRoot.getAsJsonObject();
322 
323  if (tokenResponse.get("error") != null) {
324  // report an error?
325  logger.error("Got an error back: " + tokenResponse.get("error") + ", " + tokenResponse.get("error_description"));
326  return null;
327  }
328 
329  if (!tokenResponse.get("active").getAsBoolean()) {
330  // non-valid token
331  logger.info("Server returned non-active token");
332  return null;
333  }
334  // create an OAuth2Authentication
335  OAuth2Authentication auth = new OAuth2Authentication(createStoredRequest(tokenResponse), createUserAuthentication(tokenResponse));
336  // create an OAuth2AccessToken
337  OAuth2AccessToken token = createAccessToken(tokenResponse, accessToken);
338 
339  if (token.getExpiration() == null || token.getExpiration().after(new Date())) {
340  // Store them in the cache
341  TokenCacheObject tco = new TokenCacheObject(token, auth);
342  if (cacheTokens && (cacheNonExpiringTokens || token.getExpiration() != null)) {
343  authCache.put(accessToken, tco);
344  }
345  return tco;
346  }
347  }
348 
349  // when the token is invalid for whatever reason
350  return null;
351  }
boolean cacheTokens
Definition: IntrospectingTokenService.java:75
Authentication createUserAuthentication(JsonObject token)
Definition: IntrospectingTokenService.java:244
HttpComponentsClientHttpRequestFactory factory
Definition: IntrospectingTokenService.java:77
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

◆ readAccessToken()

OAuth2AccessToken org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.readAccessToken ( String  accessToken)
inline
372  {
373  // First check if the in memory cache has a Token object, and that it is
374  // still valid
375  // If Valid, return it
376  TokenCacheObject cacheAuth = checkCache(accessToken);
377  if (cacheAuth != null) {
378  return cacheAuth.token;
379  } else {
380  cacheAuth = parseToken(accessToken);
381  if (cacheAuth != null) {
382  return cacheAuth.token;
383  } else {
384  return null;
385  }
386  }
387  }
TokenCacheObject checkCache(String key)
Definition: IntrospectingTokenService.java:217
TokenCacheObject parseToken(String accessToken)
Definition: IntrospectingTokenService.java:265

◆ setCacheNonExpiringTokens()

void org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.setCacheNonExpiringTokens ( boolean  cacheNonExpiringTokens)
inline

should non-expiring tokens be cached using the default cache timeout

引数
cacheNonExpiringTokens
188  {
190  }
boolean cacheNonExpiringTokens
Definition: IntrospectingTokenService.java:74

◆ setCacheTokens()

void org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.setCacheTokens ( boolean  cacheTokens)
inline

Configure if the client should cache tokens locally or not

引数
cacheTokens
204  {
205  this.cacheTokens = cacheTokens;
206  }
boolean cacheTokens
Definition: IntrospectingTokenService.java:75

◆ setDefaultExpireTime()

void org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.setDefaultExpireTime ( int  defaultExpireTime)
inline

set the default cache expire time in milliseconds

引数
defaultExpireTime
156  {
158  }
int defaultExpireTime
Definition: IntrospectingTokenService.java:72

◆ setForceCacheExpireTime()

void org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.setForceCacheExpireTime ( boolean  forceCacheExpireTime)
inline

set forcing a cache expire time maximum value

引数
forceCacheExpireTime
172  {
174  }
boolean forceCacheExpireTime
Definition: IntrospectingTokenService.java:73

◆ setIntrospectionAuthorityGranter()

void org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.setIntrospectionAuthorityGranter ( IntrospectionAuthorityGranter  introspectionAuthorityGranter)
inline
引数
introspectionAuthorityGranterthe introspectionAuthorityGranter to set
133  {
135  }
IntrospectionAuthorityGranter introspectionAuthorityGranter
Definition: IntrospectingTokenService.java:70

◆ setIntrospectionConfigurationService()

void org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.setIntrospectionConfigurationService ( IntrospectionConfigurationService  introspectionUrlProvider)
inline
引数
introspectionConfigurationServicethe introspectionConfigurationService to set
126  {
127  this.introspectionConfigurationService = introspectionUrlProvider;
128  }
IntrospectionConfigurationService introspectionConfigurationService
Definition: IntrospectingTokenService.java:69

メンバ詳解

◆ authCache

Map<String, TokenCacheObject> org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.authCache = new HashMap<>()
private

◆ cacheNonExpiringTokens

boolean org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.cacheNonExpiringTokens = false
private

◆ cacheTokens

boolean org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.cacheTokens = true
private

◆ defaultExpireTime

int org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.defaultExpireTime = 300000
private

◆ factory

HttpComponentsClientHttpRequestFactory org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.factory
private

◆ forceCacheExpireTime

boolean org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.forceCacheExpireTime = false
private

◆ introspectionAuthorityGranter

IntrospectionAuthorityGranter org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.introspectionAuthorityGranter = new SimpleIntrospectionAuthorityGranter()
private

◆ introspectionConfigurationService

IntrospectionConfigurationService org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.introspectionConfigurationService
private

◆ logger

final Logger org.mitre.oauth2.introspectingfilter.IntrospectingTokenService.logger = LoggerFactory.getLogger(IntrospectingTokenService.class)
staticprivate

Logger for this class


このクラス詳解は次のファイルから抽出されました: