|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2014 The MITRE Corporation |
| 3 | + * and the MIT Kerberos and Internet Trust Consortium |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + ******************************************************************************/ |
| 17 | +package org.mitre.oauth2.service.impl; |
| 18 | + |
| 19 | +import com.google.common.base.Joiner; |
| 20 | +import org.mitre.oauth2.model.OAuth2AccessTokenEntity; |
| 21 | +import org.mitre.oauth2.model.OAuth2RefreshTokenEntity; |
| 22 | +import org.mitre.oauth2.service.IntrospectionResultAssembler; |
| 23 | +import org.mitre.openid.connect.model.UserInfo; |
| 24 | +import org.springframework.security.oauth2.provider.OAuth2Authentication; |
| 25 | +import org.springframework.stereotype.Service; |
| 26 | + |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static com.google.common.collect.Maps.newLinkedHashMap; |
| 30 | + |
| 31 | +/** |
| 32 | + * Default implementation of the {@link IntrospectionResultAssembler} interface. |
| 33 | + */ |
| 34 | +@Service |
| 35 | +public class DefaultIntrospectionResultAssembler implements IntrospectionResultAssembler { |
| 36 | + |
| 37 | + @Override |
| 38 | + public Map<String, Object> assembleFrom(OAuth2AccessTokenEntity accessToken, UserInfo userInfo) { |
| 39 | + |
| 40 | + Map<String, Object> result = newLinkedHashMap(); |
| 41 | + OAuth2Authentication authentication = accessToken.getAuthenticationHolder().getAuthentication(); |
| 42 | + |
| 43 | + result.put("active", true); |
| 44 | + |
| 45 | + result.put("scope", Joiner.on(" ").join(accessToken.getScope())); |
| 46 | + |
| 47 | + if (accessToken.getExpiration() != null) { |
| 48 | + result.put("exp", accessToken.getExpiration()); |
| 49 | + } |
| 50 | + |
| 51 | + if (userInfo != null) { |
| 52 | + // if we have a UserInfo, use that for the subject |
| 53 | + result.put("sub", userInfo.getSub()); |
| 54 | + } else { |
| 55 | + // otherwise, use the authentication's username |
| 56 | + result.put("sub", authentication.getName()); |
| 57 | + } |
| 58 | + |
| 59 | + result.put("user_id", authentication.getName()); |
| 60 | + |
| 61 | + result.put("client_id", authentication.getOAuth2Request().getClientId()); |
| 62 | + |
| 63 | + result.put("token_type", accessToken.getTokenType()); |
| 64 | + |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Map<String, Object> assembleFrom(OAuth2RefreshTokenEntity refreshToken, UserInfo userInfo) { |
| 70 | + |
| 71 | + Map<String, Object> result = newLinkedHashMap(); |
| 72 | + OAuth2Authentication authentication = refreshToken.getAuthenticationHolder().getAuthentication(); |
| 73 | + |
| 74 | + result.put("active", true); |
| 75 | + |
| 76 | + result.put("scope", Joiner.on(" ").join(authentication.getOAuth2Request().getScope())); |
| 77 | + |
| 78 | + if (refreshToken.getExpiration() != null) { |
| 79 | + result.put("exp", refreshToken.getExpiration()); |
| 80 | + } |
| 81 | + |
| 82 | + if (userInfo != null) { |
| 83 | + // if we have a UserInfo, use that for the subject |
| 84 | + result.put("sub", userInfo.getSub()); |
| 85 | + } else { |
| 86 | + // otherwise, use the authentication's username |
| 87 | + result.put("sub", authentication.getName()); |
| 88 | + } |
| 89 | + |
| 90 | + result.put("user_id", authentication.getName()); |
| 91 | + |
| 92 | + result.put("client_id", authentication.getOAuth2Request().getClientId()); |
| 93 | + |
| 94 | + return result; |
| 95 | + } |
| 96 | +} |
0 commit comments