Skip to content

Commit 9dfac35

Browse files
Alexander Imfeldjricher
authored andcommitted
Introduce introspection result assembler to allow for customized introspection results
1 parent bf00c1f commit 9dfac35

File tree

5 files changed

+424
-199
lines changed

5 files changed

+424
-199
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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;
18+
19+
import org.mitre.oauth2.model.OAuth2AccessTokenEntity;
20+
import org.mitre.oauth2.model.OAuth2RefreshTokenEntity;
21+
import org.mitre.openid.connect.model.UserInfo;
22+
23+
import java.util.Map;
24+
25+
/**
26+
* Strategy interface for assembling a token introspection result.
27+
*/
28+
public interface IntrospectionResultAssembler {
29+
30+
/**
31+
* Assemble a token introspection result from the given access token and user info.
32+
*
33+
* @param accessToken the access token
34+
* @param userInfo the user info
35+
* @return the token introspection result
36+
*/
37+
Map<String, Object> assembleFrom(OAuth2AccessTokenEntity accessToken, UserInfo userInfo);
38+
39+
/**
40+
* Assemble a token introspection result from the given refresh token and user info.
41+
*
42+
* @param refreshToken the refresh token
43+
* @param userInfo the user info
44+
* @return the token introspection result
45+
*/
46+
Map<String, Object> assembleFrom(OAuth2RefreshTokenEntity refreshToken, UserInfo userInfo);
47+
48+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

openid-connect-server/src/main/java/org/mitre/oauth2/view/TokenIntrospectionView.java

Lines changed: 0 additions & 143 deletions
This file was deleted.

0 commit comments

Comments
 (0)