Skip to content

Commit 6129cfa

Browse files
committed
added scope-based authorities granter for introspections services, closes mitreid-connect#835
1 parent 96f4d5e commit 6129cfa

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*******************************************************************************
2+
* Copyright 2015 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+
18+
package org.mitre.oauth2.introspectingfilter.service.impl;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import java.util.Set;
23+
24+
import org.mitre.oauth2.introspectingfilter.service.IntrospectionAuthorityGranter;
25+
import org.springframework.security.core.GrantedAuthority;
26+
import org.springframework.security.core.authority.AuthorityUtils;
27+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
28+
import org.springframework.security.oauth2.common.util.OAuth2Utils;
29+
30+
import com.google.gson.JsonObject;
31+
32+
/**
33+
* @author jricher
34+
*
35+
*/
36+
public class ScopeBasedIntrospectionAuthoritiesGranter implements IntrospectionAuthorityGranter {
37+
38+
private List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_API");
39+
40+
/* (non-Javadoc)
41+
* @see org.mitre.oauth2.introspectingfilter.IntrospectionAuthorityGranter#getAuthorities(net.minidev.json.JSONObject)
42+
*/
43+
@Override
44+
public List<GrantedAuthority> getAuthorities(JsonObject introspectionResponse) {
45+
List<GrantedAuthority> auth = new ArrayList<>(getAuthorities());
46+
47+
if (introspectionResponse.has("scope") && introspectionResponse.get("scope").isJsonPrimitive()) {
48+
String scopeString = introspectionResponse.get("scope").getAsString();
49+
Set<String> scopes = OAuth2Utils.parseParameterList(scopeString);
50+
for (String scope : scopes) {
51+
auth.add(new SimpleGrantedAuthority("OAUTH_SCOPE_" + scope));
52+
}
53+
}
54+
55+
return auth;
56+
}
57+
58+
/**
59+
* @return the authorities
60+
*/
61+
public List<GrantedAuthority> getAuthorities() {
62+
return authorities;
63+
}
64+
65+
/**
66+
* @param authorities the authorities to set
67+
*/
68+
public void setAuthorities(List<GrantedAuthority> authorities) {
69+
this.authorities = authorities;
70+
}
71+
72+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*******************************************************************************
2+
* Copyright 2015 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+
18+
package org.mitre.oauth2.introspectingfilter.service.impl;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.springframework.security.core.GrantedAuthority;
26+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
27+
28+
import com.google.gson.JsonObject;
29+
30+
import static org.junit.Assert.assertTrue;
31+
32+
/**
33+
* @author jricher
34+
*
35+
*/
36+
public class TestScopeBasedIntrospectionAuthoritiesGranter {
37+
38+
private JsonObject introspectionResponse;
39+
40+
private ScopeBasedIntrospectionAuthoritiesGranter granter = new ScopeBasedIntrospectionAuthoritiesGranter();
41+
42+
/**
43+
* @throws java.lang.Exception
44+
*/
45+
@Before
46+
public void setUp() throws Exception {
47+
introspectionResponse = new JsonObject();
48+
}
49+
50+
/**
51+
* Test method for {@link org.mitre.oauth2.introspectingfilter.service.impl.ScopeBasedIntrospectionAuthoritiesGranter#getAuthorities(com.google.gson.JsonObject)}.
52+
*/
53+
@Test
54+
public void testGetAuthoritiesJsonObject_withScopes() {
55+
introspectionResponse.addProperty("scope", "foo bar baz batman");
56+
57+
List<GrantedAuthority> expected = new ArrayList<>();
58+
expected.add(new SimpleGrantedAuthority("ROLE_API"));
59+
expected.add(new SimpleGrantedAuthority("OAUTH_SCOPE_foo"));
60+
expected.add(new SimpleGrantedAuthority("OAUTH_SCOPE_bar"));
61+
expected.add(new SimpleGrantedAuthority("OAUTH_SCOPE_baz"));
62+
expected.add(new SimpleGrantedAuthority("OAUTH_SCOPE_batman"));
63+
64+
List<GrantedAuthority> authorities = granter.getAuthorities(introspectionResponse);
65+
66+
assertTrue(authorities.containsAll(expected));
67+
assertTrue(expected.containsAll(authorities));
68+
}
69+
70+
/**
71+
* Test method for {@link org.mitre.oauth2.introspectingfilter.service.impl.ScopeBasedIntrospectionAuthoritiesGranter#getAuthorities(com.google.gson.JsonObject)}.
72+
*/
73+
@Test
74+
public void testGetAuthoritiesJsonObject_withoutScopes() {
75+
76+
List<GrantedAuthority> expected = new ArrayList<>();
77+
expected.add(new SimpleGrantedAuthority("ROLE_API"));
78+
79+
List<GrantedAuthority> authorities = granter.getAuthorities(introspectionResponse);
80+
81+
assertTrue(authorities.containsAll(expected));
82+
assertTrue(expected.containsAll(authorities));
83+
}
84+
85+
}

0 commit comments

Comments
 (0)