Skip to content

Make #buildFormParameters in the oauth2 client auth converters protected #6854

Closed
@ExpDev07

Description

@ExpDev07

I was working with making a custom converter for the "OAuth2AuthorizationCodeGrantRequest" to add support for the PKCE spec. However, I still wanted the base functionality of "OAuth2AuthorizationCodeGrantRequestEntityConverter", and just wanted to add some parameters.

The way to do this, per the official documentation, is to just create a new class implementing "Converter<OAuth2AuthorizationCodeGrantRequest, RequestEntity<?>>" and then keep an instance of "OAuth2AuthorizationCodeGrantRequestEntityConverter" inside that class and do some magic like this:

@Override
public RequestEntity<?> convert(OAuth2AuthorizationCodeGrantRequest request) {
    RequestEntity<?> entity = this.defaultConverter.convert(request);

    // Create PKCE request parameters.
    MultiValueMap<String, String> params = (MultiValueMap<String,String>) entity.getBody();
    params.add("code_verifier", "verifier");

    return new RequestEntity<>(params, entity.getHeaders(), entity.getMethod(), entity.getUrl());
}

This looks very ugly when you can just make buildFormParameters inside "OAuth2AuthorizationCodeGrantRequestEntityConverter" protected, and then we can override it inside a class extending it and modify/add parameters such as this:

@Override
protected MultiValueMap<String, String> buildFormParameters(OAuth2AuthorizationCodeGrantRequest req) {
    // Call to super to get parameters
    MultiValueMap<String, String> params = super.buildFormParameters(req);

    // Modify/edit
    params.put("code_verifier", "verifer");

    // Return added params
    return params;
}

The "convert" method which is already defined in the super-class will then do the rest. Much cleaner and easier.

Thanks for taking the time to read this!

Metadata

Metadata

Assignees

Labels

in: oauth2An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions