-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#4941 - Mapping OIDC groups to INCEpTION's internal roles #4982
base: main
Are you sure you want to change the base?
Changes from all commits
8038bc1
375efa2
3664f5c
1fa5702
c19f375
86f2266
a036546
370bba9
644e435
f86f656
7787bc1
d45480a
c505b1a
3cb8a01
f2b41dc
bdae968
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Licensed to the Technische Universität Darmstadt under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The Technische Universität Darmstadt | ||
* licenses this file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package de.tudarmstadt.ukp.inception.security.oauth; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
|
||
import de.tudarmstadt.ukp.clarin.webanno.security.model.Role; | ||
import de.tudarmstadt.ukp.clarin.webanno.security.model.User; | ||
|
||
@Configuration | ||
@ConfigurationProperties(prefix = "security.oauth.roles") | ||
public class OAuth2Utils { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Utils classes in INCEpTION are typically final classes containing only static methods. Configuration classes are called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @reckart how would you recommend to proceed? Creating the interface and implementation class for the properties, injecting the PropertiesImpl class in OAuth2AdapterImpl's constructor and moving the mapping logic in a private method of that class? |
||
|
||
private static boolean OAUTH2_ROLES_ENABLED; | ||
private static String OAUTH2_ROLES_CLAIM; | ||
private static String OAUTH2_ADMIN_ROLE; | ||
private static String OAUTH2_USER_ROLE; | ||
private static String OAUTH2_PROJECT_CREATOR_ROLE; | ||
private static String OAUTH2_REMOTE_ROLE; | ||
|
||
@Value("${security.oauth.roles.enabled:false}") | ||
public void setOAuth2RolesEnabled(boolean oAuth2RolesEnabled) | ||
{ | ||
OAUTH2_ROLES_ENABLED = oAuth2RolesEnabled; | ||
} | ||
|
||
@Value("${security.oauth.roles.claim:groups}") | ||
public void setOAuth2RolesClaim(String oAuth2RolesClaim) | ||
{ | ||
OAUTH2_ROLES_CLAIM = oAuth2RolesClaim; | ||
} | ||
|
||
@Value("${security.oauth.roles.admin:}") | ||
public void setAdminRole(String adminRole) | ||
{ | ||
OAUTH2_ADMIN_ROLE = adminRole; | ||
} | ||
|
||
@Value("${security.oauth.roles.user:}") | ||
public void setUserRole(String userRole) | ||
{ | ||
OAUTH2_USER_ROLE = userRole; | ||
} | ||
|
||
@Value("${security.oauth.roles.project-creator:}") | ||
public void setProjectCreatorRole(String projectCreatorRole) | ||
{ | ||
OAUTH2_PROJECT_CREATOR_ROLE = projectCreatorRole; | ||
} | ||
|
||
@Value("${security.oauth.roles.remote:}") | ||
public void setRemoteRole(String remoteRole) | ||
{ | ||
OAUTH2_REMOTE_ROLE = remoteRole; | ||
} | ||
|
||
|
||
public static Set<Role> getOAuth2UserRoles(User aUser, OAuth2User user) | ||
throws AccessDeniedException | ||
{ | ||
Set<Role> roles = new HashSet<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
|
||
if (!OAUTH2_ROLES_ENABLED) { | ||
roles.add(Role.ROLE_USER); | ||
return roles; | ||
} | ||
|
||
List<String> oauth2groups = user.getAttribute(OAUTH2_ROLES_CLAIM); | ||
|
||
if (oauth2groups == null || oauth2groups.isEmpty()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just return the empty list here. |
||
throw new AccessDeniedException("OAuth2 roles mapping is enabled, but user [" | ||
+ aUser.getUsername() + "] doesn't have any roles, or the corresponding claim is empty"); | ||
} | ||
|
||
oauth2groups.forEach(group -> matchOauth2groupToRole(group, roles)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Functional programming (without side-effects) would be easier to understand. Can we use |
||
|
||
if (roles.isEmpty()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if this check is necessary at all since the application should only set users in that have ROLE_USER. If it is necessary, then it should IMHO be at a higher level, not at the level where the groups are extracted from the claim. |
||
throw new AccessDeniedException("User [" | ||
+ aUser.getUsername() + "] doesn't belong to any role"); | ||
} | ||
|
||
return roles; | ||
} | ||
|
||
private static void matchOauth2groupToRole(String oauth2group, Set<Role> userRoles) { | ||
|
||
if (StringUtils.equals(oauth2group, OAUTH2_ADMIN_ROLE)) { | ||
userRoles.add(Role.ROLE_ADMIN); | ||
} | ||
|
||
if (StringUtils.equals(oauth2group, OAUTH2_USER_ROLE)) { | ||
userRoles.add(Role.ROLE_USER); | ||
} | ||
|
||
if (StringUtils.equals(oauth2group, OAUTH2_PROJECT_CREATOR_ROLE)) { | ||
userRoles.add(Role.ROLE_PROJECT_CREATOR); | ||
} | ||
|
||
if (StringUtils.equals(oauth2group, OAUTH2_REMOTE_ROLE)) { | ||
userRoles.add(Role.ROLE_REMOTE); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Licensed to the Technische Universität Darmstadt under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The Technische Universität Darmstadt | ||
* licenses this file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package de.tudarmstadt.ukp.inception.security.oauth; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.annotation.DirtiesContext.ClassMode; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import de.tudarmstadt.ukp.clarin.webanno.security.model.Role; | ||
import de.tudarmstadt.ukp.clarin.webanno.security.model.User; | ||
|
||
|
||
@ExtendWith(SpringExtension.class) | ||
@ContextConfiguration(classes = OAuth2Utils.class) | ||
@DirtiesContext(classMode = ClassMode.AFTER_CLASS) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of making this a spring integration test, I'd tend towards it being a plain junit test that sets up the necessary beans as POJOs. It seems a bit of overkill to start up an entire spring context for this. |
||
@TestPropertySource( | ||
properties = """ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better use |
||
security.oauth.roles.enabled=true | ||
security.oauth.roles.claim=groups | ||
security.oauth.roles.admin=/INCEPTION_ADMIN | ||
security.oauth.roles.user=/INCEPTION_USER | ||
security.oauth.roles.project-creator=/INCEPTION_PROJECT_CREATOR | ||
security.oauth.roles.remote=/INCEPTION_REMOTE | ||
""") | ||
public class OAuth2UtilsTest | ||
{ | ||
|
||
String USERNAME = "ThatGuy"; | ||
String OAUTH2_GROUP_USER = "/INCEPTION_USER"; | ||
String OAUTH2_GROUP_PROJECT_CREATOR = "/INCEPTION_PROJECT_CREATOR"; | ||
String OAUTH2_GROUP_ADMIN = "/INCEPTION_ADMIN"; | ||
String OAUTH2_GROUP_REMOTE = "/INCEPTION_REMOTE"; | ||
|
||
User testUser; | ||
OAuth2User testRetievedOAuth2User; | ||
|
||
@BeforeEach | ||
void setup() { | ||
testUser = new User(); | ||
testUser.setUsername(USERNAME); | ||
|
||
testRetievedOAuth2User = mock(OAuth2User.class); | ||
} | ||
|
||
@Test | ||
void thatAdminRoleIsGivenIfMatchingGroupFound() | ||
{ | ||
ArrayList<String> userOAuth2Groups = new ArrayList<>(); | ||
userOAuth2Groups.add(OAUTH2_GROUP_ADMIN); | ||
when(testRetievedOAuth2User.getAttribute(anyString())).thenReturn(userOAuth2Groups); | ||
|
||
Set<Role> userRoles = OAuth2Utils.getOAuth2UserRoles(testUser, testRetievedOAuth2User); | ||
|
||
assertTrue(userRoles.contains(Role.ROLE_ADMIN)); | ||
} | ||
|
||
@Test | ||
void thatUserRoleIsGivenIfMatchingGroupFound() | ||
{ | ||
ArrayList<String> userOAuth2Groups = new ArrayList<>(); | ||
userOAuth2Groups.add(OAUTH2_GROUP_USER); | ||
when(testRetievedOAuth2User.getAttribute(anyString())).thenReturn(userOAuth2Groups); | ||
|
||
Set<Role> userRoles = OAuth2Utils.getOAuth2UserRoles(testUser, testRetievedOAuth2User); | ||
|
||
assertTrue(userRoles.contains(Role.ROLE_USER)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use AssertJ asserts instead of Unit asserts. |
||
} | ||
|
||
@Test | ||
void thatProjectCreatorRoleIsGivenIfMatchingGroupFound() | ||
{ | ||
ArrayList<String> userOAuth2Groups = new ArrayList<>(); | ||
userOAuth2Groups.add(OAUTH2_GROUP_PROJECT_CREATOR); | ||
when(testRetievedOAuth2User.getAttribute(anyString())).thenReturn(userOAuth2Groups); | ||
|
||
Set<Role> userRoles = OAuth2Utils.getOAuth2UserRoles(testUser, testRetievedOAuth2User); | ||
assertTrue(userRoles.contains(Role.ROLE_PROJECT_CREATOR)); | ||
} | ||
|
||
@Test | ||
void thatRemoteRoleIsGivenIfMatchingGroupFound() | ||
{ | ||
ArrayList<String> userOAuth2Groups = new ArrayList<>(); | ||
userOAuth2Groups.add(OAUTH2_GROUP_REMOTE); | ||
when(testRetievedOAuth2User.getAttribute(anyString())).thenReturn(userOAuth2Groups); | ||
|
||
Set<Role> userRoles = OAuth2Utils.getOAuth2UserRoles(testUser, testRetievedOAuth2User); | ||
|
||
assertTrue(userRoles.contains(Role.ROLE_REMOTE)); | ||
} | ||
|
||
@Test | ||
void thatUnauthorizedExceptionIsThrownIfNoRoleIsMapped() { | ||
|
||
ArrayList<String> userOAuth2Groups = new ArrayList<>(); | ||
when(testRetievedOAuth2User.getAttribute(anyString())).thenReturn(userOAuth2Groups); | ||
|
||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
OAuth2Utils.getOAuth2UserRoles(testUser, testRetievedOAuth2User); | ||
} catch (AccessDeniedException ade) { | ||
System.out.println(ade.getClass().getSimpleName() + " was thrown"); | ||
return; | ||
} | ||
|
||
fail("Expected Exception wasn't catched"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Why) did you have to add these annotations here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably a side effect of a refactor, I didn't changed that class - will revert that