Skip to content
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

Authentication Method related special groups are put in claim set even if a different authentication method is used #9130

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ public boolean allowSetPassword(Context context,
public List<Group> getSpecialGroups(Context context, HttpServletRequest request)
throws SQLException;

/**
* Returns true if the special groups returned by
* {@link org.dspace.authenticate.AuthenticationMethod#getSpecialGroups(Context, HttpServletRequest)}
* should be implicitly be added to the groups related to the current user. By
* default this is true if the authentication method is the actual
* authentication mechanism used by the user.
* @param context A valid DSpace context.
* @param request The request that started this operation, or null if not
* applicable.
* @return true is the special groups must be considered, false
* otherwise
*/
public default boolean areSpecialGroupsApplicable(Context context, HttpServletRequest request) {
return getName().equals(context.getAuthenticationMethod());
}

/**
* Authenticate the given or implicit credentials.
* This is the heart of the authentication method: test the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,15 @@ public List<Group> getSpecialGroups(Context context,
int totalLen = 0;

for (AuthenticationMethod method : getAuthenticationMethodStack()) {
List<Group> gl = method.getSpecialGroups(context, request);
if (gl.size() > 0) {
result.addAll(gl);
totalLen += gl.size();

if (method.areSpecialGroupsApplicable(context, request)) {

List<Group> gl = method.getSpecialGroups(context, request);
if (gl.size() > 0) {
result.addAll(gl);
totalLen += gl.size();
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ public List<Group> getSpecialGroups(Context context, HttpServletRequest request)
return groups;
}

@Override
public boolean areSpecialGroupsApplicable(Context context, HttpServletRequest request) {
return true;
}

@Override
public int authenticate(Context context, String username, String password,
String realm, HttpServletRequest request) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.dspace.app.rest;

import static java.lang.Thread.sleep;
import static org.dspace.app.rest.matcher.GroupMatcher.matchGroupWithName;
import static org.dspace.app.rest.utils.RegexUtils.REGEX_UUID;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
Expand Down Expand Up @@ -1641,6 +1642,71 @@ public void testOrcidLoginURL() throws Exception {
}
}

@Test
public void testAreSpecialGroupsApplicable() throws Exception {
context.turnOffAuthorisationSystem();

GroupBuilder.createGroup(context)
.withName("specialGroupPwd")
.build();
GroupBuilder.createGroup(context)
.withName("specialGroupShib")
.build();

configurationService.setProperty("plugin.sequence.org.dspace.authenticate.AuthenticationMethod", SHIB_AND_PASS);
configurationService.setProperty("authentication-password.login.specialgroup", "specialGroupPwd");
configurationService.setProperty("authentication-shibboleth.role.faculty", "specialGroupShib");
configurationService.setProperty("authentication-shibboleth.default-roles", "faculty");

context.restoreAuthSystemState();

String passwordToken = getAuthToken(eperson.getEmail(), password);

getClient(passwordToken).perform(get("/api/authn/status").param("projection", "full"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", AuthenticationStatusMatcher.matchFullEmbeds()))
.andExpect(jsonPath("$", AuthenticationStatusMatcher.matchLinks()))
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$.okay", is(true)))
.andExpect(jsonPath("$.authenticated", is(true)))
.andExpect(jsonPath("$.authenticationMethod", is("password")))
.andExpect(jsonPath("$.type", is("status")))
.andExpect(jsonPath("$._links.specialGroups.href", startsWith(REST_SERVER_URL)))
.andExpect(jsonPath("$._embedded.specialGroups._embedded.specialGroups",
Matchers.containsInAnyOrder(matchGroupWithName("specialGroupPwd"))));

getClient(passwordToken).perform(get("/api/authn/status/specialGroups").param("projection", "full"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.specialGroups",
Matchers.containsInAnyOrder(matchGroupWithName("specialGroupPwd"))));

String shibToken = getClient().perform(post("/api/authn/login")
.requestAttr("SHIB-MAIL", eperson.getEmail())
.requestAttr("SHIB-SCOPED-AFFILIATION", "faculty;staff"))
.andExpect(status().isOk())
.andReturn().getResponse().getHeader(AUTHORIZATION_HEADER).replace(AUTHORIZATION_TYPE, "");

getClient(shibToken).perform(get("/api/authn/status").param("projection", "full"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", AuthenticationStatusMatcher.matchFullEmbeds()))
.andExpect(jsonPath("$", AuthenticationStatusMatcher.matchLinks()))
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$.okay", is(true)))
.andExpect(jsonPath("$.authenticated", is(true)))
.andExpect(jsonPath("$.authenticationMethod", is("shibboleth")))
.andExpect(jsonPath("$.type", is("status")))
.andExpect(jsonPath("$._links.specialGroups.href", startsWith(REST_SERVER_URL)))
.andExpect(jsonPath("$._embedded.specialGroups._embedded.specialGroups",
Matchers.containsInAnyOrder(matchGroupWithName("specialGroupShib"))));

getClient(shibToken).perform(get("/api/authn/status/specialGroups").param("projection", "full"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.specialGroups",
Matchers.containsInAnyOrder(matchGroupWithName("specialGroupShib"))));
}

// Get a short-lived token based on an active login token
private String getShortLivedToken(String loginToken) throws Exception {
ObjectMapper mapper = new ObjectMapper();
Expand Down