Skip to content

Allow the ability to configure AuthoritiesMapper in Reactive OAuth2Login #8324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
choweiyuan opened this issue Apr 3, 2020 · 4 comments
Closed
Assignees
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) type: enhancement A general enhancement
Milestone

Comments

@choweiyuan
Copy link

choweiyuan commented Apr 3, 2020

Summary

I'd like the ability to modify Reactive OAuth2Login's authoritiesMapper.

Actual Behavior

Can't find a suitable API to configure this. There's an equivalent for the Servlet HttpSecurity but not for Reactive equivalent.

Digging into the source code further seem to suggest that OAuth2LoginAuthenticationProvider (Servlet) have setAuthoritiesMapper, and OidcAuthorizationCodeReactiveAuthenticationManager (Reactive) does not have setAuthoritiesMapper

Expected Behavior

I expect I can do something similar for Reactive OAuth2Login

Configuration

Servlet HttpSecurity

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http
      .authorizeRequests(a -> a
                                .antMatchers("/", "/error", "/webjars/**").permitAll()
                                .anyRequest().authenticated()
      )
      .exceptionHandling(e -> e
                                .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
      )
      .logout(l -> l
                     .logoutSuccessUrl("/").permitAll()
      )
      .oauth2Login().userInfoEndpoint().userAuthoritiesMapper(new GrantedAuthoritiesMapper() {
      @Override
      public Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) {
        return null;
      }
    });
    // @formatter:on
  }

Reactive

  @Bean
  protected SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
    return http
             .authorizeExchange(a -> a
                                       .pathMatchers("/static/**", "/", "/error", "/webjars/**", "/login/**").permitAll()
                                       .anyExchange().authenticated()
             )
             .exceptionHandling(e -> e
                                       .authenticationEntryPoint(new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED))
             )
             .logout().logoutUrl("/")
             .and()
             .oauth2Login().userInfoEndpoint().userAuthoritiesMapper(new GrantedAuthoritiesMapper() {
      @Override
      public Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) {
        return null;
      }
    }) // DOES NOT COMPILE
             .and().build();
    // @formatter:on
  }

Version

5.2.1.RELEASE

Sample

Will provide soon if it gives further clarity

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 3, 2020
@jgrandja jgrandja added in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) status: ideal-for-contribution An issue that we actively are looking for someone to help us with type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 3, 2020
@jgrandja
Copy link
Contributor

jgrandja commented Apr 3, 2020

@choweiyuan Indeed, OAuth2LoginReactiveAuthenticationManager and OidcAuthorizationCodeReactiveAuthenticationManager have not exposed a setAuthoritiesMapper(GrantedAuthoritiesMapper). We should allow for this configuration the same way the Servlet implementations do.

Would you be interested in submitting a PR for this?

@jgrandja
Copy link
Contributor

jgrandja commented Apr 3, 2020

@choweiyuan You can also consider a Delegation-based strategy with OAuth2UserService. The provided link demonstrates a sample for Servlet but the same strategy can be applied on the Reactive side as well.

@rwinch rwinch assigned jgrandja and unassigned jgrandja Apr 3, 2020
@antonin-arquey
Copy link
Contributor

I'm interested in submitting a PR for this if it is ok for you @jgrandja

Just one question, would the default ReactiveAuthenticationManager created by the OAuth2LoginSpec try to get a bean of type GrantedAuthoritiesMapper and use it ?

Or would the user have to manually configure the authentication manager if they want to provide a custom authority mapper ?

@jgrandja
Copy link
Contributor

jgrandja commented Apr 8, 2020

Thanks for the offer @antonin-arquey! The issue is yours.

would the default ReactiveAuthenticationManager created by the OAuth2LoginSpec try to get a bean of type GrantedAuthoritiesMapper and use it ?

Yes, please go with this option.

antonin-arquey added a commit to antonin-arquey/spring-security that referenced this issue Apr 9, 2020
Allow the configuration of a custom GrantedAuthorityMapper for reactive OAuth2Login

- Add setter in OidcAuthorizationCodeReactiveAuthenticationManager
  and OAuth2LoginReactiveAuthenticationManager

- Use an available GrantedAuthorityMapper bean to configure the default ReactiveAuthenticationManager

Fixes spring-projectsgh-8324
@jgrandja jgrandja removed the status: ideal-for-contribution An issue that we actively are looking for someone to help us with label Apr 17, 2020
@jgrandja jgrandja added this to the 5.4.x milestone Apr 17, 2020
@jgrandja jgrandja self-assigned this Apr 17, 2020
@jgrandja jgrandja modified the milestones: 5.4.x, 5.4.0.M1 Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) type: enhancement A general enhancement
Projects
None yet
4 participants