spring-projects / spring-projects/spring-security

DefaultOAuth2AuthorizationRequestResolver should be extensible

Open
#13,124 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: oauth2 type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Expected Behavior

DefaultOAuth2AuthorizationRequestResolver should provide a way to customize authorizationRequestMatcher and provide a customizable way to extract the registrationId on custom scenarios where we may need to store registrationId on headers or cookies but also want to leverage the default implementation since current class is final we can't even extend it.

private Function<HttpServletRequest, String> registrationIdResolver;

public DefaultOAuth2AuthorizationRequestResolver(ClientRegistrationRepository clientRegistrationRepository, String authorizationRequestBaseUri) {
    this(clientRegistrationRepository, new AntPathRequestMatcher(authorizationRequestBaseUri + "/{" + REGISTRATION_ID_URI_VARIABLE_NAME + "}")));
}

public DefaultOAuth2AuthorizationRequestResolver(ClientRegistrationRepository clientRegistrationRepository, AntPathRequestMatcher authorizationRequestMatcher) {
    this.clientRegistrationRepository = clientRegistrationRepository;
    this.authorizationRequestMatcher = authorizationRequestMatcher;
    this.registrationIdResolver = request -> {
        return authorizationRequestMatcher.matcher(request)
                .getVariables()
                .get(REGISTRATION_ID_URI_VARIABLE_NAME);
    };
}

public void setRegistrationIdResolver(Function<HttpServletRequest, String> registrationIdResolver) {
    this.registrationIdResolver = registrationIdResolver;
}

private String resolveRegistrationId(HttpServletRequest request) {
    if (!this.authorizationRequestMatcher.matches(request)) {
        return null;
    }

    return this.registrationIdResolver.apply(request);
}

Current Behavior

Current behavior does not enable providing a custom matcher nor an alternative way to extract the registrationId value from the request.
With the suggested changes we can have it customizable by possibly: 1. matching with different URIs, 2. being able to extract registrationId from request cookies, headers, etc.

Context

Currently implementing multi-tenant login support and need to recover from request context which registrationId to use. From the way that DefaultOAuth2AuthorizationRequestResolver is implemented, I had to basically copy & paste its source code to add the desired behavior since it is final and no way is provided to add custom behavior to it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DefaultOAuth2AuthorizationRequestResolver and trace its constructors, authorizationRequestMatcher handling, and registrationId resolution. Review the surrounding Spring Security OAuth2 authorization-request tests, then define and verify an extensibility path that supports custom matching and registrationId extraction without requiring users to copy the class.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
authentication, security
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.