spring-cloud-aws icon indicating copy to clipboard operation
spring-cloud-aws copied to clipboard

OIDC Logout for AWS Cognito

Open rieckpil opened this issue 3 years ago • 13 comments

Type: Feature

Is your feature request related to a problem? Please describe.

AWS Cognito doesn't implement the OpenID Connect RP-Initiated Logout specification (in draft) yet. When using AWS Cognito together with Spring Security for OAuth 2.0 Login (aka. OIDC) every user will still be logged in at the identity provider when they logout at the Spring backend. Spring Security already provides a OidcClientInitiatedLogoutSuccessHandler to logout the end-user also at the identity provider (technically an additional HTTP call to the identity provider when the user decided to logout), but as AWS Cogntio doesn't implement the spec, it's of little help.

For Stratospheric we implemented our own SimpleUrlLogoutSuccessHandler to achieve the full logout. Our (naive) solution looks like the following:

public class CognitoOidcLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {

  private final String logoutUrl;
  private final String clientId;

  public CognitoOidcLogoutSuccessHandler(String logoutUrl, String clientId) {
    this.logoutUrl = logoutUrl;
    this.clientId = clientId;
  }

  @Override
  protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response,
                                      Authentication authentication) {

    UriComponents baseUrl = UriComponentsBuilder
      .fromHttpUrl(UrlUtils.buildFullRequestUrl(request))
      .replacePath(request.getContextPath())
      .replaceQuery(null)
      .fragment(null)
      .build();

    return UriComponentsBuilder
      .fromUri(URI.create(logoutUrl))
      .queryParam("client_id", clientId)
      .queryParam("logout_uri", baseUrl)
      .encode(StandardCharsets.UTF_8)
      .build()
      .toUriString();
  }
}

Describe the solution you'd like

For Spring Cloud AWS + Spring Security + AWS Cognito setup, end-users should be fully logged-out when they log out from the application (invalid Spring Session) and at the identity provider.

Describe alternatives you've considered

Some use cases might not favor a fully-loggout for e.g. SSO with other applications. Hence the fully logout should be an opt-in and not applied automatically.

Additional context

I've already blogged about a possible Spring Security and AWS Cognito OIDC logout to demonstrate a possible solution.

I'm looking forward to provide a PR with a possible solution in case you think it makes sense to add this feature.

rieckpil avatar May 09 '21 14:05 rieckpil

@eddumelendez your voice is very welcome here.

maciejwalkowiak avatar Feb 08 '22 13:02 maciejwalkowiak

this sounds good to me. I would suggest to have CognitoLogoutSuccessHandler and then an auto-configuration that creates the bean.

@rieckpil would you like to contribute to this one?

eddumelendez avatar Feb 11 '22 16:02 eddumelendez

thanks for getting back to this issue 🙏 Yes, I'd like to contribute to this.

rieckpil avatar Feb 11 '22 16:02 rieckpil

it's yours.

eddumelendez avatar Feb 11 '22 16:02 eddumelendez

To avoid double-work - ideally this should be done as a part of Cognito integration in 3.0 (migrated to AWS SDK v2)

maciejwalkowiak avatar Apr 02 '22 08:04 maciejwalkowiak

Thanks for the info 👍 I should get to it in the next 1-2 weeks 🚀

rieckpil avatar Apr 02 '22 08:04 rieckpil

nice thats awesome! Since i haven't touched much Cognito integration, @eddumelendez is a person to bug about it ;-)

Keep in mind that we've reorganised branches a bit - main is now where 3.0 development happens.

maciejwalkowiak avatar Apr 02 '22 08:04 maciejwalkowiak

I'm a bit confused with the re-organization. I don't see any Cognito-relevant code in spring-cloud-aws-autoconfigure while in 2.4.x there's code available.

Do I first have to migrate the 2.4.x Cognito code to the main branch or how would you (@eddumelendez) recommend to integrate my new class?

In short, I need to add a new property to CognitoAuthenticationProperties and add a bean factory for LogoutSuccessHandler to CognitoAuthenticationAutoConfiguration. I have an early WIP (branching of 2.4.x here).

rieckpil avatar Sep 11 '22 08:09 rieckpil

As far as I understand - to get basic Cognito auth we do not anything custom as it has been already covered by Spring Boot and Spring Security. For the purpose of implementing OIDC Logout we need CognitoProperties and perhaps a separate module for Cognito? @eddumelendez your opinion will be appreciated.

maciejwalkowiak avatar Sep 12 '22 09:09 maciejwalkowiak

Do we have any update on this?

dominik-kovacs avatar Feb 19 '23 15:02 dominik-kovacs

@poklakni we are not actively working on this, I'll mark it as open for contributions.

maciejwalkowiak avatar Feb 20 '23 05:02 maciejwalkowiak

@maciejwalkowiak I could work on this if @rieckpil does not already

dominik-kovacs avatar Feb 20 '23 17:02 dominik-kovacs

I stopped working on the WIP (see here) after I didn't get any answer. Feel free to take over 🚀

rieckpil avatar Feb 21 '23 09:02 rieckpil