Skip to content

Commit 6814843

Browse files
kuchtiak-ufalkosarko
authored andcommitted
Issue 1373: obtain special groups from user context when new token is generated (on token refresh) (ufal#1378)
* Issue 1373: obtain special groups from user context when new token is generated (on token refresh) * resolve Copilot comments * resolve Copilot Comments: compute special groups only when when user is authenticated * Remove HttpSession dependency from ClarinShibAuthentication Use request-scoped attributes for shib.authenticated instead of HttpSession/JSESSIONID, aligning with upstream ShibAuthentication. Follow-up to ufal#1373/ufal#1378. * Guard against null special groups in Context.getSpecialGroups A special-group UUID may reference a Group that has since been deleted; GroupService.find returns null in that case. The list was built with an unconditional add, so it could contain null elements, which caused an NPE downstream (e.g. SpecialGroupClaimProvider.getValue maps group.getID() while generating the JWT sg claim on token refresh). Filter nulls once here so every caller is covered. Follow-up to ufal#1373/ufal#1378. --------- Co-authored-by: Ondrej Kosarko <kosarko@ufal.mff.cuni.cz> (cherry picked from commit 4c294b2)
1 parent 867e43d commit 6814843

2 files changed

Lines changed: 23 additions & 25 deletions

File tree

dspace-api/src/main/java/org/dspace/authenticate/clarin/ClarinShibAuthentication.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public int authenticate(Context context, String username, String password,
277277

278278
// Step 4: Log the user in.
279279
context.setCurrentUser(eperson);
280-
request.getSession().setAttribute("shib.authenticated", true);
280+
request.setAttribute("shib.authenticated", true);
281281
AuthenticateServiceFactory.getInstance().getAuthenticationService().initEPerson(context, request, eperson);
282282

283283
log.info(eperson.getEmail() + " has been authenticated via shibboleth.");
@@ -330,42 +330,35 @@ public int authenticate(Context context, String username, String password,
330330
@Override
331331
public List<Group> getSpecialGroups(Context context, HttpServletRequest request) {
332332
try {
333-
// User has not successfuly authenticated via shibboleth.
334-
if (request == null ||
335-
context.getCurrentUser() == null ||
336-
request.getSession().getAttribute("shib.authenticated") == null) {
337-
return Collections.EMPTY_LIST;
333+
// User has not successfully authenticated via shibboleth.
334+
if (request == null || context.getCurrentUser() == null) {
335+
return Collections.emptyList();
338336
}
339337

340-
// If we have already calculated the special groups then return them.
341-
if (request.getSession().getAttribute("shib.specialgroup") != null) {
342-
log.debug("Returning cached special groups.");
343-
List<UUID> sessionGroupIds = (List<UUID>) request.getSession().getAttribute("shib.specialgroup");
344-
List<Group> result = new ArrayList<>();
345-
for (UUID uuid : sessionGroupIds) {
346-
result.add(groupService.find(context, uuid));
347-
}
348-
return result;
338+
List<Group> specialGroups = context.getSpecialGroups();
339+
if (!specialGroups.isEmpty()) {
340+
log.debug("Returning special groups from context.");
341+
return specialGroups;
349342
}
350343

344+
if (request.getAttribute("shib.authenticated") == null) {
345+
log.debug("User has not been authenticated via shibboleth, returning empty list of special groups.");
346+
return Collections.emptyList();
347+
}
351348

352349
List<UUID> groupIds = new ShibGroup(new ShibHeaders(request), context).get();
353-
// Cache the special groups, so we don't have to recalculate them again
354-
// for this session.
355-
request.getSession().setAttribute("shib.specialgroup", groupIds);
356350

357351
List<Group> groups = new ArrayList<>();
358352
for (UUID uuid : groupIds) {
359353
Group foundGroup = groupService.find(context, uuid);
360-
if (Objects.isNull(foundGroup)) {
361-
continue;
354+
if (foundGroup != null) {
355+
groups.add(foundGroup);
362356
}
363-
groups.add(foundGroup);
364357
}
365358
return groups;
366359
} catch (Throwable t) {
367-
log.error("Unable to validate any sepcial groups this user may belong too because of an exception.", t);
368-
return Collections.EMPTY_LIST;
360+
log.error("Unable to validate any special groups this user may belong to because of an exception.", t);
361+
return Collections.emptyList();
369362
}
370363
}
371364

@@ -1291,7 +1284,7 @@ private String getShibURL(HttpServletRequest request) {
12911284
public boolean isUsed(final Context context, final HttpServletRequest request) {
12921285
if (request != null &&
12931286
context.getCurrentUser() != null &&
1294-
request.getSession().getAttribute("shib.authenticated") != null) {
1287+
request.getAttribute("shib.authenticated") != null) {
12951288
return true;
12961289
}
12971290
return false;

dspace-api/src/main/java/org/dspace/core/Context.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,12 @@ public boolean inSpecialGroup(UUID groupID) {
714714
public List<Group> getSpecialGroups() throws SQLException {
715715
List<Group> myGroups = new ArrayList<>();
716716
for (UUID groupId : specialGroups) {
717-
myGroups.add(EPersonServiceFactory.getInstance().getGroupService().find(this, groupId));
717+
Group group = EPersonServiceFactory.getInstance().getGroupService().find(this, groupId);
718+
// A special group UUID may reference a group that has since been deleted; skip nulls
719+
// so callers never receive a list containing null (avoids NPE downstream).
720+
if (group != null) {
721+
myGroups.add(group);
722+
}
718723
}
719724

720725
return myGroups;

0 commit comments

Comments
 (0)