|
15 | 15 | *******************************************************************************/
|
16 | 16 | package cz.muni.ics.oauth2.service.impl;
|
17 | 17 |
|
18 |
| -import static com.google.common.collect.Maps.newLinkedHashMap; |
19 |
| - |
20 | 18 | import com.google.common.base.Joiner;
|
21 | 19 | import com.google.common.collect.Sets;
|
22 |
| -import com.google.common.collect.Sets; |
| 20 | +import com.nimbusds.jwt.JWT; |
| 21 | +import com.nimbusds.jwt.JWTClaimsSet; |
| 22 | +import cz.muni.ics.oauth2.model.AuthenticationHolderEntity; |
| 23 | +import cz.muni.ics.oauth2.model.AuthenticationStatement; |
23 | 24 | import cz.muni.ics.oauth2.model.OAuth2AccessTokenEntity;
|
24 | 25 | import cz.muni.ics.oauth2.model.OAuth2RefreshTokenEntity;
|
| 26 | +import cz.muni.ics.oauth2.model.SamlAuthenticationDetails; |
| 27 | +import cz.muni.ics.oauth2.model.SavedUserAuthentication; |
25 | 28 | import cz.muni.ics.oauth2.service.IntrospectionResultAssembler;
|
26 |
| -import cz.muni.ics.openid.connect.model.UserInfo; |
27 |
| -import java.text.ParseException; |
28 |
| -import java.util.Map; |
29 |
| -import java.util.Set; |
30 | 29 | import lombok.extern.slf4j.Slf4j;
|
| 30 | +import org.joda.time.DateTime; |
| 31 | +import org.joda.time.format.DateTimeFormatter; |
| 32 | +import org.joda.time.format.ISODateTimeFormat; |
| 33 | +import org.springframework.security.oauth2.common.OAuth2AccessToken; |
31 | 34 | import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
32 | 35 | import org.springframework.stereotype.Service;
|
| 36 | +import org.springframework.util.StringUtils; |
| 37 | + |
| 38 | +import java.sql.Timestamp; |
| 39 | +import java.text.ParseException; |
| 40 | +import java.util.HashSet; |
| 41 | +import java.util.LinkedHashMap; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Map; |
| 44 | +import java.util.Set; |
| 45 | + |
| 46 | +import static com.google.common.collect.Maps.newLinkedHashMap; |
33 | 47 |
|
34 | 48 | /**
|
35 | 49 | * Default implementation of the {@link IntrospectionResultAssembler} interface.
|
|
39 | 53 | public class DefaultIntrospectionResultAssembler implements IntrospectionResultAssembler {
|
40 | 54 |
|
41 | 55 | @Override
|
42 |
| - public Map<String, Object> assembleFrom(OAuth2AccessTokenEntity accessToken, UserInfo userInfo, Set<String> authScopes) { |
43 |
| - |
44 |
| - Map<String, Object> result = newLinkedHashMap(); |
45 |
| - OAuth2Authentication authentication = accessToken.getAuthenticationHolder().getAuthentication(); |
46 |
| - |
47 |
| - result.put(ACTIVE, true); |
48 |
| - |
49 |
| - |
50 |
| - Set<String> scopes = Sets.intersection(authScopes, accessToken.getScope()); |
51 |
| - result.put(SCOPE, Joiner.on(SCOPE_SEPARATOR).join(scopes)); |
52 |
| - |
53 |
| - if (accessToken.getExpiration() != null) { |
54 |
| - try { |
55 |
| - result.put(EXPIRES_AT, dateFormat.valueToString(accessToken.getExpiration())); |
56 |
| - result.put(EXP, accessToken.getExpiration().getTime() / 1000L); |
57 |
| - } catch (ParseException e) { |
58 |
| - log.error("Parse exception in token introspection", e); |
59 |
| - } |
60 |
| - } |
61 |
| - |
62 |
| - if (userInfo != null) { |
63 |
| - // if we have a UserInfo, use that for the subject |
64 |
| - result.put(SUB, userInfo.getSub()); |
| 56 | + public Map<String, Object> assembleFrom(OAuth2AccessTokenEntity token, Set<String> introspectionRequesterScopes) { |
| 57 | + AuthenticationHolderEntity authenticationHolder = token.getAuthenticationHolder(); |
| 58 | + OAuth2Authentication authentication = (authenticationHolder != null) ? |
| 59 | + authenticationHolder.getAuthentication() : null; |
| 60 | + |
| 61 | + Set<String> scopes = Sets.intersection(introspectionRequesterScopes, token.getScope()); |
| 62 | + String scope = Joiner.on(SCOPE_SEPARATOR).join(scopes); |
| 63 | + Long exp = null; |
| 64 | + if (token.getExpiration() != null) { |
| 65 | + exp = token.getExpiration().getTime() / 1000L; |
65 | 66 | } else {
|
66 |
| - // otherwise, use the authentication's username |
67 |
| - result.put(SUB, authentication.getName()); |
| 67 | + log.warn("WARNING - ACCESS TOKEN WITHOUT EXPIRATION DATE DETECTED ('{}')", token); |
68 | 68 | }
|
69 | 69 |
|
70 |
| - if(authentication.getUserAuthentication() != null) { |
71 |
| - result.put(USER_ID, authentication.getUserAuthentication().getName()); |
| 70 | + String clientId = (authentication != null && authentication.getOAuth2Request() != null) ? |
| 71 | + authentication.getOAuth2Request().getClientId() : null; |
| 72 | + if (clientId == null) { |
| 73 | + clientId = (token.getClient() != null) ? token.getClient().getClientId() : null; |
72 | 74 | }
|
73 | 75 |
|
74 |
| - result.put(CLIENT_ID, authentication.getOAuth2Request().getClientId()); |
| 76 | + String tokenType = OAuth2AccessToken.BEARER_TYPE; |
| 77 | + JWT jwtValue = token.getJwtValue(); |
| 78 | + String username = (authenticationHolder != null |
| 79 | + && authenticationHolder.getAuthentication() != null |
| 80 | + && authenticationHolder.getAuthentication().getUserAuthentication() != null) ? |
| 81 | + authenticationHolder.getAuthentication().getUserAuthentication().getName() : null; |
75 | 82 |
|
76 |
| - result.put(TOKEN_TYPE, accessToken.getTokenType()); |
| 83 | + Map<String, Object> result = assemble(scope, exp, username, clientId, tokenType, jwtValue, authenticationHolder); |
77 | 84 |
|
| 85 | + if (!token.isExpired()) { |
| 86 | + result.put(ACTIVE, true); |
| 87 | + } else { |
| 88 | + result.clear(); |
| 89 | + result.put(ACTIVE, false); |
| 90 | + } |
78 | 91 | return result;
|
79 | 92 | }
|
80 | 93 |
|
81 | 94 | @Override
|
82 |
| - public Map<String, Object> assembleFrom(OAuth2RefreshTokenEntity refreshToken, UserInfo userInfo, Set<String> authScopes) { |
| 95 | + public Map<String, Object> assembleFrom(OAuth2RefreshTokenEntity token, Set<String> introspectionRequesterScopes) { |
| 96 | + AuthenticationHolderEntity authenticationHolder = token.getAuthenticationHolder(); |
| 97 | + OAuth2Authentication authentication = (authenticationHolder != null) ? |
| 98 | + authenticationHolder.getAuthentication() : null; |
| 99 | + Set<String> tokenScopes = (authentication != null && authentication.getOAuth2Request() != null) ? |
| 100 | + authentication.getOAuth2Request().getScope() : new HashSet<>(); |
| 101 | + |
| 102 | + Set<String> scopes = Sets.intersection(introspectionRequesterScopes, tokenScopes); |
| 103 | + String scope = Joiner.on(SCOPE_SEPARATOR).join(scopes); |
| 104 | + Long exp = null; |
| 105 | + if (token.getExpiration() != null) { |
| 106 | + exp = token.getExpiration().getTime() / 1000L; |
| 107 | + } else { |
| 108 | + log.warn("WARNING - REFRESH TOKEN WITHOUT EXPIRATION DATE DETECTED ('{}')", token); |
| 109 | + } |
83 | 110 |
|
84 |
| - Map<String, Object> result = newLinkedHashMap(); |
85 |
| - OAuth2Authentication authentication = refreshToken.getAuthenticationHolder().getAuthentication(); |
| 111 | + String clientId = (authentication != null && authentication.getOAuth2Request() != null) ? |
| 112 | + authentication.getOAuth2Request().getClientId() : null; |
| 113 | + String username = (authentication != null && authentication.getUserAuthentication() != null) ? |
| 114 | + authentication.getUserAuthentication().getName() : null; |
| 115 | + String tokenType = "refresh_token"; |
| 116 | + JWT jwtValue = token.getJwt(); |
86 | 117 |
|
87 |
| - result.put(ACTIVE, true); |
| 118 | + Map<String, Object> result = assemble(scope, exp, username, clientId, tokenType, jwtValue, authenticationHolder); |
88 | 119 |
|
89 |
| - Set<String> scopes = Sets.intersection(authScopes, authentication.getOAuth2Request().getScope()); |
| 120 | + if (!token.isExpired()) { |
| 121 | + result.put(ACTIVE, true); |
| 122 | + } else { |
| 123 | + result.clear(); |
| 124 | + result.put(ACTIVE, false); |
| 125 | + } |
| 126 | + return result; |
| 127 | + } |
90 | 128 |
|
91 |
| - result.put(SCOPE, Joiner.on(SCOPE_SEPARATOR).join(scopes)); |
| 129 | + private Map<String, Object> assemble(String scope, |
| 130 | + Long exp, |
| 131 | + String username, |
| 132 | + String clientId, |
| 133 | + String tokenType, |
| 134 | + JWT jwtValue, |
| 135 | + AuthenticationHolderEntity authenticationHolder) |
| 136 | + { |
| 137 | + Map<String, Object> result = new LinkedHashMap<>(); |
| 138 | + if (scope != null && !scope.isEmpty()) { |
| 139 | + result.put(SCOPE, scope); |
| 140 | + } |
| 141 | + if (StringUtils.hasText(clientId)) { |
| 142 | + result.put(CLIENT_ID, clientId); |
| 143 | + } |
| 144 | + if (StringUtils.hasText(tokenType)) { |
| 145 | + result.put(TOKEN_TYPE, tokenType); |
| 146 | + } |
| 147 | + if (exp != null) { |
| 148 | + result.put(EXP, exp); |
| 149 | + } |
| 150 | + if (StringUtils.hasText(username)) { |
| 151 | + result.put(USERNAME, username); |
| 152 | + } |
| 153 | + if (jwtValue != null) { |
| 154 | + fillDataFromJwt(jwtValue, result); |
| 155 | + } |
| 156 | + if (authenticationHolder != null && authenticationHolder.getUserAuth() != null) { |
| 157 | + fillAcrAndAuthTime(authenticationHolder.getUserAuth(), result); |
| 158 | + } |
| 159 | + return result; |
| 160 | + } |
92 | 161 |
|
93 |
| - if (refreshToken.getExpiration() != null) { |
94 |
| - try { |
95 |
| - result.put(EXPIRES_AT, dateFormat.valueToString(refreshToken.getExpiration())); |
96 |
| - result.put(EXP, refreshToken.getExpiration().getTime() / 1000L); |
97 |
| - } catch (ParseException e) { |
98 |
| - log.error("Parse exception in token introspection", e); |
| 162 | + private void fillDataFromJwt(JWT atJwt, Map<String, Object> result) { |
| 163 | + try { |
| 164 | + JWTClaimsSet atClaimsSet = atJwt.getJWTClaimsSet(); |
| 165 | + if (atClaimsSet != null) { |
| 166 | + if (atClaimsSet.getIssueTime() != null) { |
| 167 | + result.put(IAT, atClaimsSet.getIssueTime().getTime() / 1000L); |
| 168 | + } |
| 169 | + if (atClaimsSet.getNotBeforeTime() != null) { |
| 170 | + result.put(NBF, atClaimsSet.getNotBeforeTime().getTime() / 1000L); |
| 171 | + } |
| 172 | + if (StringUtils.hasText(atClaimsSet.getSubject())) { |
| 173 | + result.put(SUB, atClaimsSet.getSubject()); |
| 174 | + } |
| 175 | + if (atClaimsSet.getAudience() != null) { |
| 176 | + result.put(AUD, atClaimsSet.getAudience()); |
| 177 | + } |
| 178 | + if (StringUtils.hasText(atClaimsSet.getIssuer())) { |
| 179 | + result.put(ISS, atClaimsSet.getIssuer()); |
| 180 | + } |
| 181 | + if (StringUtils.hasText(atClaimsSet.getJWTID())) { |
| 182 | + result.put(JTI, atClaimsSet.getJWTID()); |
| 183 | + } |
99 | 184 | }
|
| 185 | + } catch (ParseException e) { |
| 186 | + log.warn("Caught exception while introspecting token and parsing JWT value '{}'", atJwt, e); |
100 | 187 | }
|
| 188 | + } |
101 | 189 |
|
102 |
| - |
103 |
| - if (userInfo != null) { |
104 |
| - // if we have a UserInfo, use that for the subject |
105 |
| - result.put(SUB, userInfo.getSub()); |
106 |
| - } else { |
107 |
| - // otherwise, use the authentication's username |
108 |
| - result.put(SUB, authentication.getName()); |
| 190 | + private void fillAcrAndAuthTime(SavedUserAuthentication savedUserAuthentication, Map<String, Object> result) { |
| 191 | + if (StringUtils.hasText(savedUserAuthentication.getAcr())) { |
| 192 | + result.put(ACR, savedUserAuthentication.getAcr()); |
109 | 193 | }
|
110 |
| - |
111 |
| - if(authentication.getUserAuthentication() != null) { |
112 |
| - result.put(USER_ID, authentication.getUserAuthentication().getName()); |
| 194 | + if (savedUserAuthentication.getAuthTime() != null) { |
| 195 | + result.put(AUTH_TIME, savedUserAuthentication.getAuthTime()); |
113 | 196 | }
|
114 |
| - |
115 |
| - result.put(CLIENT_ID, authentication.getOAuth2Request().getClientId()); |
116 |
| - |
117 |
| - return result; |
118 | 197 | }
|
| 198 | + |
119 | 199 | }
|
0 commit comments