Search in sources :

Example 1 with AuthenticatedPrincipal

use of org.springframework.security.core.AuthenticatedPrincipal in project midpoint by Evolveum.

the class MidpointSaml2LogoutRequestResolver method resolve.

@Override
public Saml2LogoutRequest resolve(HttpServletRequest httpServletRequest, Authentication authentication) {
    Saml2AuthenticationToken token = null;
    if (authentication instanceof MidpointAuthentication) {
        ModuleAuthentication authModule = ((MidpointAuthentication) authentication).getProcessingModuleAuthentication();
        if (authModule instanceof Saml2ModuleAuthenticationImpl) {
            if (authModule.getAuthentication() instanceof Saml2AuthenticationToken) {
                token = (Saml2AuthenticationToken) authModule.getAuthentication();
            } else if ((authModule.getAuthentication() instanceof PreAuthenticatedAuthenticationToken || authModule.getAuthentication() instanceof AnonymousAuthenticationToken) && authModule.getAuthentication().getDetails() instanceof Saml2AuthenticationToken) {
                token = (Saml2AuthenticationToken) authModule.getAuthentication().getDetails();
            }
        }
    } else if (authentication instanceof AnonymousAuthenticationToken && authentication.getDetails() instanceof Saml2AuthenticationToken) {
        token = (Saml2AuthenticationToken) authentication.getDetails();
    }
    if (token != null) {
        AuthenticatedPrincipal principal = token.getDetails() instanceof AuthenticatedPrincipal ? (AuthenticatedPrincipal) token.getDetails() : null;
        if (!(principal instanceof Saml2AuthenticatedPrincipal)) {
            String name = token.getRelyingPartyRegistration().getEntityId();
            String relyingPartyRegistrationId = token.getRelyingPartyRegistration().getRegistrationId();
            principal = new Saml2AuthenticatedPrincipal() {

                @Override
                public String getName() {
                    return name;
                }

                @Override
                public String getRelyingPartyRegistrationId() {
                    return relyingPartyRegistrationId;
                }
            };
        }
        return resolver.resolve(httpServletRequest, new Saml2Authentication(principal, token.getSaml2Response(), null));
    }
    return resolver.resolve(httpServletRequest, authentication);
}
Also used : ModuleAuthentication(com.evolveum.midpoint.authentication.api.config.ModuleAuthentication) Saml2Authentication(org.springframework.security.saml2.provider.service.authentication.Saml2Authentication) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) Saml2ModuleAuthenticationImpl(com.evolveum.midpoint.authentication.impl.module.authentication.Saml2ModuleAuthenticationImpl) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) MidpointAuthentication(com.evolveum.midpoint.authentication.api.config.MidpointAuthentication) Saml2AuthenticationToken(org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationToken) Saml2AuthenticatedPrincipal(org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal) Saml2AuthenticatedPrincipal(org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal) AuthenticatedPrincipal(org.springframework.security.core.AuthenticatedPrincipal)

Example 2 with AuthenticatedPrincipal

use of org.springframework.security.core.AuthenticatedPrincipal in project spring-security by spring-projects.

the class AbstractAuthenticationTokenTests method testGetNameWhenPrincipalIsAuthenticatedPrincipal.

@Test
public void testGetNameWhenPrincipalIsAuthenticatedPrincipal() {
    String principalName = "test";
    AuthenticatedPrincipal principal = mock(AuthenticatedPrincipal.class);
    given(principal.getName()).willReturn(principalName);
    MockAuthenticationImpl token = new MockAuthenticationImpl(principal, "Password", this.authorities);
    assertThat(token.getName()).isEqualTo(principalName);
    verify(principal, times(1)).getName();
}
Also used : AuthenticatedPrincipal(org.springframework.security.core.AuthenticatedPrincipal) Test(org.junit.jupiter.api.Test)

Example 3 with AuthenticatedPrincipal

use of org.springframework.security.core.AuthenticatedPrincipal in project spring-security by spring-projects.

the class SecurityContextHolderAwareRequestWrapperTests method testGetRemoteUserStringWithAuthenticatedPrincipal.

@Test
public void testGetRemoteUserStringWithAuthenticatedPrincipal() {
    String username = "authPrincipalUsername";
    AuthenticatedPrincipal principal = mock(AuthenticatedPrincipal.class);
    given(principal.getName()).willReturn(username);
    Authentication auth = new TestingAuthenticationToken(principal, "user");
    SecurityContextHolder.getContext().setAuthentication(auth);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/");
    SecurityContextHolderAwareRequestWrapper wrapper = new SecurityContextHolderAwareRequestWrapper(request, "");
    assertThat(wrapper.getRemoteUser()).isEqualTo(username);
    verify(principal, times(1)).getName();
}
Also used : Authentication(org.springframework.security.core.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) AuthenticatedPrincipal(org.springframework.security.core.AuthenticatedPrincipal) Test(org.junit.jupiter.api.Test)

Aggregations

AuthenticatedPrincipal (org.springframework.security.core.AuthenticatedPrincipal)3 Test (org.junit.jupiter.api.Test)2 MidpointAuthentication (com.evolveum.midpoint.authentication.api.config.MidpointAuthentication)1 ModuleAuthentication (com.evolveum.midpoint.authentication.api.config.ModuleAuthentication)1 Saml2ModuleAuthenticationImpl (com.evolveum.midpoint.authentication.impl.module.authentication.Saml2ModuleAuthenticationImpl)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)1 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 Saml2AuthenticatedPrincipal (org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal)1 Saml2Authentication (org.springframework.security.saml2.provider.service.authentication.Saml2Authentication)1 Saml2AuthenticationToken (org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationToken)1 PreAuthenticatedAuthenticationToken (org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken)1