-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
52 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
server/src/main/java/access/security/AuthorizationRequestCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package access.security; | ||
|
||
import access.model.Invitation; | ||
import access.repository.InvitationRepository; | ||
import jakarta.servlet.http.HttpSession; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; | ||
import org.springframework.security.web.savedrequest.DefaultSavedRequest; | ||
import org.springframework.web.context.request.RequestAttributes; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
|
||
public class AuthorizationRequestCustomizer implements Consumer<OAuth2AuthorizationRequest.Builder> { | ||
|
||
private final InvitationRepository invitationRepository; | ||
private final String eduidEntityId; | ||
|
||
public AuthorizationRequestCustomizer(InvitationRepository invitationRepository, String eduidEntityId) { | ||
this.invitationRepository = invitationRepository; | ||
this.eduidEntityId = eduidEntityId; | ||
} | ||
|
||
@Override | ||
public void accept(OAuth2AuthorizationRequest.Builder builder) { | ||
builder.additionalParameters(params -> { | ||
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes(); | ||
HttpSession session = ((ServletRequestAttributes) requestAttributes) | ||
.getRequest().getSession(false); | ||
if (session == null) { | ||
return; | ||
} | ||
DefaultSavedRequest savedRequest = (DefaultSavedRequest) session.getAttribute("SPRING_SECURITY_SAVED_REQUEST"); | ||
String[] force = savedRequest.getParameterValues("force"); | ||
if (force != null && force.length == 1) { | ||
params.put("prompt", "login"); | ||
} | ||
String[] hash = savedRequest.getParameterValues("hash"); | ||
if (hash != null && hash.length == 1) { | ||
Optional<Invitation> optionalInvitation = invitationRepository.findByHash(hash[0]); | ||
optionalInvitation.ifPresent(invitation -> { | ||
if (invitation.isEduIDOnly()) { | ||
params.put("login_hint", eduidEntityId); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters