DefaultIntrospectionResultAssembler.java

  1. /*******************************************************************************
  2.  * Copyright 2017 The MIT Internet Trust Consortium
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *******************************************************************************/
  16. package org.mitre.oauth2.service.impl;

  17. import static com.google.common.collect.Maps.newLinkedHashMap;

  18. import java.text.ParseException;
  19. import java.util.Map;
  20. import java.util.Set;

  21. import org.mitre.oauth2.model.OAuth2AccessTokenEntity;
  22. import org.mitre.oauth2.model.OAuth2RefreshTokenEntity;
  23. import org.mitre.oauth2.service.IntrospectionResultAssembler;
  24. import org.mitre.openid.connect.model.UserInfo;
  25. import org.mitre.uma.model.Permission;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.security.oauth2.provider.OAuth2Authentication;
  29. import org.springframework.stereotype.Service;

  30. import com.google.common.base.Joiner;
  31. import com.google.common.collect.Sets;

  32. /**
  33.  * Default implementation of the {@link IntrospectionResultAssembler} interface.
  34.  */
  35. @Service
  36. public class DefaultIntrospectionResultAssembler implements IntrospectionResultAssembler {

  37.     /**
  38.      * Logger for this class
  39.      */
  40.     private static final Logger logger = LoggerFactory.getLogger(DefaultIntrospectionResultAssembler.class);

  41.     @Override
  42.     public Map<String, Object> assembleFrom(OAuth2AccessTokenEntity accessToken, UserInfo userInfo, Set<String> authScopes) {

  43.         Map<String, Object> result = newLinkedHashMap();
  44.         OAuth2Authentication authentication = accessToken.getAuthenticationHolder().getAuthentication();

  45.         result.put(ACTIVE, true);

  46.         if (accessToken.getPermissions() != null && !accessToken.getPermissions().isEmpty()) {

  47.             Set<Object> permissions = Sets.newHashSet();

  48.             for (Permission perm : accessToken.getPermissions()) {
  49.                 Map<String, Object> o = newLinkedHashMap();
  50.                 o.put("resource_set_id", perm.getResourceSet().getId().toString());
  51.                 Set<String> scopes = Sets.newHashSet(perm.getScopes());
  52.                 o.put("scopes", scopes);
  53.                 permissions.add(o);
  54.             }

  55.             result.put("permissions", permissions);

  56.         } else {
  57.             Set<String> scopes = Sets.intersection(authScopes, accessToken.getScope());

  58.             result.put(SCOPE, Joiner.on(SCOPE_SEPARATOR).join(scopes));

  59.         }

  60.         if (accessToken.getExpiration() != null) {
  61.             try {
  62.                 result.put(EXPIRES_AT, dateFormat.valueToString(accessToken.getExpiration()));
  63.                 result.put(EXP, accessToken.getExpiration().getTime() / 1000L);
  64.             } catch (ParseException e) {
  65.                 logger.error("Parse exception in token introspection", e);
  66.             }
  67.         }

  68.         if (userInfo != null) {
  69.             // if we have a UserInfo, use that for the subject
  70.             result.put(SUB, userInfo.getSub());
  71.         } else {
  72.             // otherwise, use the authentication's username
  73.             result.put(SUB, authentication.getName());
  74.         }

  75.         if(authentication.getUserAuthentication() != null) {
  76.             result.put(USER_ID, authentication.getUserAuthentication().getName());
  77.         }

  78.         result.put(CLIENT_ID, authentication.getOAuth2Request().getClientId());

  79.         result.put(TOKEN_TYPE, accessToken.getTokenType());

  80.         return result;
  81.     }

  82.     @Override
  83.     public Map<String, Object> assembleFrom(OAuth2RefreshTokenEntity refreshToken, UserInfo userInfo, Set<String> authScopes) {

  84.         Map<String, Object> result = newLinkedHashMap();
  85.         OAuth2Authentication authentication = refreshToken.getAuthenticationHolder().getAuthentication();

  86.         result.put(ACTIVE, true);

  87.         Set<String> scopes = Sets.intersection(authScopes, authentication.getOAuth2Request().getScope());

  88.         result.put(SCOPE, Joiner.on(SCOPE_SEPARATOR).join(scopes));

  89.         if (refreshToken.getExpiration() != null) {
  90.             try {
  91.                 result.put(EXPIRES_AT, dateFormat.valueToString(refreshToken.getExpiration()));
  92.                 result.put(EXP, refreshToken.getExpiration().getTime() / 1000L);
  93.             } catch (ParseException e) {
  94.                 logger.error("Parse exception in token introspection", e);
  95.             }
  96.         }


  97.         if (userInfo != null) {
  98.             // if we have a UserInfo, use that for the subject
  99.             result.put(SUB, userInfo.getSub());
  100.         } else {
  101.             // otherwise, use the authentication's username
  102.             result.put(SUB, authentication.getName());
  103.         }

  104.         if(authentication.getUserAuthentication() != null) {
  105.             result.put(USER_ID, authentication.getUserAuthentication().getName());
  106.         }

  107.         result.put(CLIENT_ID, authentication.getOAuth2Request().getClientId());

  108.         return result;
  109.     }
  110. }