Search in sources :

Example 16 with OAuth2AuthenticationToken

use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project spring-security by spring-projects.

the class OidcClientInitiatedServerLogoutSuccessHandlerTests method logoutWhenClientRegistrationHasNoEndSessionEndpointThenDefaults.

@Test
public void logoutWhenClientRegistrationHasNoEndSessionEndpointThenDefaults() {
    ClientRegistration registration = TestClientRegistrations.clientRegistration().build();
    ReactiveClientRegistrationRepository repository = new InMemoryReactiveClientRegistrationRepository(registration);
    OidcClientInitiatedServerLogoutSuccessHandler handler = new OidcClientInitiatedServerLogoutSuccessHandler(repository);
    OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(), AuthorityUtils.NO_AUTHORITIES, registration.getRegistrationId());
    given(this.exchange.getPrincipal()).willReturn(Mono.just(token));
    WebFilterExchange f = new WebFilterExchange(this.exchange, this.chain);
    handler.setLogoutSuccessUrl(URI.create("https://default"));
    handler.onLogoutSuccess(f, token).block();
    assertThat(redirectedUrl(this.exchange)).isEqualTo("https://default");
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) ReactiveClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository) InMemoryReactiveClientRegistrationRepository(org.springframework.security.oauth2.client.registration.InMemoryReactiveClientRegistrationRepository) OAuth2AuthenticationToken(org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken) InMemoryReactiveClientRegistrationRepository(org.springframework.security.oauth2.client.registration.InMemoryReactiveClientRegistrationRepository) WebFilterExchange(org.springframework.security.web.server.WebFilterExchange) Test(org.junit.jupiter.api.Test)

Example 17 with OAuth2AuthenticationToken

use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project spring-security by spring-projects.

the class OidcClientInitiatedServerLogoutSuccessHandlerTests method logoutWhenUsingPostLogoutRedirectUriTemplateThenBuildsItForRedirect.

@Test
public void logoutWhenUsingPostLogoutRedirectUriTemplateThenBuildsItForRedirect() throws IOException, ServletException {
    OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(), AuthorityUtils.NO_AUTHORITIES, this.registration.getRegistrationId());
    given(this.exchange.getPrincipal()).willReturn(Mono.just(token));
    MockServerHttpRequest request = MockServerHttpRequest.get("https://rp.example.org/").build();
    given(this.exchange.getRequest()).willReturn(request);
    WebFilterExchange f = new WebFilterExchange(this.exchange, this.chain);
    this.handler.setPostLogoutRedirectUri("{baseUrl}");
    this.handler.onLogoutSuccess(f, token).block();
    assertThat(redirectedUrl(this.exchange)).isEqualTo("https://endpoint?" + "id_token_hint=id-token&" + "post_logout_redirect_uri=https://rp.example.org");
}
Also used : OAuth2AuthenticationToken(org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) WebFilterExchange(org.springframework.security.web.server.WebFilterExchange) Test(org.junit.jupiter.api.Test)

Example 18 with OAuth2AuthenticationToken

use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project spring-security by spring-projects.

the class OidcClientInitiatedServerLogoutSuccessHandlerTests method logoutWhenUsingPostLogoutRedirectUriThenIncludesItInRedirect.

@Test
public void logoutWhenUsingPostLogoutRedirectUriThenIncludesItInRedirect() {
    OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(), AuthorityUtils.NO_AUTHORITIES, this.registration.getRegistrationId());
    given(this.exchange.getPrincipal()).willReturn(Mono.just(token));
    WebFilterExchange f = new WebFilterExchange(this.exchange, this.chain);
    this.handler.setPostLogoutRedirectUri(URI.create("https://postlogout?encodedparam=value"));
    this.handler.onLogoutSuccess(f, token).block();
    assertThat(redirectedUrl(this.exchange)).isEqualTo("https://endpoint?" + "id_token_hint=id-token&" + "post_logout_redirect_uri=https://postlogout?encodedparam%3Dvalue");
}
Also used : OAuth2AuthenticationToken(org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken) WebFilterExchange(org.springframework.security.web.server.WebFilterExchange) Test(org.junit.jupiter.api.Test)

Example 19 with OAuth2AuthenticationToken

use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project dhis2-core by dhis2.

the class AuthenticationLoggerListener method onApplicationEvent.

public void onApplicationEvent(AbstractAuthenticationEvent event) {
    if (!log.isWarnEnabled()) {
        return;
    }
    if (SessionFixationProtectionEvent.class.isAssignableFrom(event.getClass()) || InteractiveAuthenticationSuccessEvent.class.isAssignableFrom(event.getClass())) {
        return;
    }
    String eventClassName = String.format("Authentication event: %s; ", ClassUtils.getShortName(event.getClass()));
    String authName = StringUtils.firstNonEmpty(event.getAuthentication().getName(), "");
    String ipAddress = "";
    String sessionId = "";
    String exceptionMessage = "";
    if (event instanceof AbstractAuthenticationFailureEvent) {
        exceptionMessage = "exception: " + ((AbstractAuthenticationFailureEvent) event).getException().getMessage();
    }
    Object details = event.getAuthentication().getDetails();
    if (details != null && ForwardedIpAwareWebAuthenticationDetails.class.isAssignableFrom(details.getClass())) {
        ForwardedIpAwareWebAuthenticationDetails authDetails = (ForwardedIpAwareWebAuthenticationDetails) details;
        ipAddress = String.format("ip: %s; ", authDetails.getIp());
        sessionId = hashSessionId(authDetails.getSessionId());
    } else if (OAuth2LoginAuthenticationToken.class.isAssignableFrom(event.getAuthentication().getClass())) {
        OAuth2LoginAuthenticationToken authenticationToken = (OAuth2LoginAuthenticationToken) event.getAuthentication();
        DhisOidcUser principal = (DhisOidcUser) authenticationToken.getPrincipal();
        if (principal != null) {
            User user = principal.getUser();
            authName = user.getUsername();
        }
        WebAuthenticationDetails oauthDetails = (WebAuthenticationDetails) authenticationToken.getDetails();
        ipAddress = String.format("ip: %s; ", oauthDetails.getRemoteAddress());
        sessionId = hashSessionId(oauthDetails.getSessionId());
    } else if (OAuth2AuthenticationToken.class.isAssignableFrom(event.getSource().getClass())) {
        OAuth2AuthenticationToken authenticationToken = (OAuth2AuthenticationToken) event.getSource();
        DhisOidcUser principal = (DhisOidcUser) authenticationToken.getPrincipal();
        if (principal != null) {
            User user = principal.getUser();
            authName = user.getUsername();
        }
    }
    String userNamePrefix = Strings.isNullOrEmpty(authName) ? "" : String.format("username: %s; ", authName);
    log.info(TextUtils.removeNonEssentialChars(eventClassName + userNamePrefix + ipAddress + sessionId + exceptionMessage));
}
Also used : InteractiveAuthenticationSuccessEvent(org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent) DhisOidcUser(org.hisp.dhis.security.oidc.DhisOidcUser) DhisOidcUser(org.hisp.dhis.security.oidc.DhisOidcUser) User(org.hisp.dhis.user.User) OAuth2AuthenticationToken(org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken) SessionFixationProtectionEvent(org.springframework.security.web.authentication.session.SessionFixationProtectionEvent) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) OAuth2LoginAuthenticationToken(org.springframework.security.oauth2.client.authentication.OAuth2LoginAuthenticationToken) AbstractAuthenticationFailureEvent(org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent)

Example 20 with OAuth2AuthenticationToken

use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project books by aidanwhiteley.

the class Oauth2AuthenticationUtils method getAuthenticationProvider.

public User.AuthenticationProvider getAuthenticationProvider(OAuth2AuthenticationToken auth) {
    OAuth2AuthorizedClient authorizedClient = this.getAuthorizedClient(auth);
    String clientId = authorizedClient.getClientRegistration().getClientId();
    if (clientId.equals(googleClientClientId)) {
        return GOOGLE;
    } else if (clientId.equals(facebookClientClientId)) {
        return FACEBOOK;
    } else {
        LOGGER.error("Unknown clientId specified of {} so cant determine authentication provider.", clientId);
        throw new IllegalArgumentException("Uknown client id specified");
    }
}
Also used : OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient)

Aggregations

OAuth2AuthenticationToken (org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken)41 Test (org.junit.jupiter.api.Test)34 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)11 Collection (java.util.Collection)6 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)6 DefaultOidcUser (org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser)6 DefaultOAuth2User (org.springframework.security.oauth2.core.user.DefaultOAuth2User)6 RegisteredOAuth2AuthorizedClient (org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient)5 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)5 OAuth2User (org.springframework.security.oauth2.core.user.OAuth2User)5 WebFilterExchange (org.springframework.security.web.server.WebFilterExchange)5 HttpHeaders (org.springframework.http.HttpHeaders)4 URI (java.net.URI)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)3 Mock (org.mockito.Mock)3 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)3 GrantedAuthority (org.springframework.security.core.GrantedAuthority)3 AuthorityUtils (org.springframework.security.core.authority.AuthorityUtils)3