-
Notifications
You must be signed in to change notification settings - Fork 188
/
SamlAuthenticationEntryPoint.java
52 lines (46 loc) · 2.27 KB
/
SamlAuthenticationEntryPoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* Copyright (c) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.web.security;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import com.evolveum.midpoint.model.api.authentication.MidpointAuthentication;
import com.evolveum.midpoint.model.api.authentication.ModuleAuthentication;
import com.evolveum.midpoint.web.security.module.authentication.Saml2ModuleAuthentication;
import com.evolveum.midpoint.web.security.util.IdentityProvider;
/**
* @author skublik
*/
public class SamlAuthenticationEntryPoint extends WicketLoginUrlAuthenticationEntryPoint {
public SamlAuthenticationEntryPoint(String loginFormUrl) {
super(loginFormUrl);
}
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
List<IdentityProvider> providers = new ArrayList<IdentityProvider>();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof MidpointAuthentication) {
MidpointAuthentication mpAuthentication = (MidpointAuthentication) authentication;
ModuleAuthentication moduleAuthentication = mpAuthentication.getProcessingModuleAuthentication();
if (moduleAuthentication instanceof Saml2ModuleAuthentication) {
providers = ((Saml2ModuleAuthentication) moduleAuthentication).getProviders();
if (providers.size() == 1
&& request.getSession().getAttribute("SPRING_SECURITY_LAST_EXCEPTION") == null) {
response.sendRedirect(providers.get(0).getRedirectLink());
return;
}
}
}
super.commence(request, response, authException);
}
}