-
Notifications
You must be signed in to change notification settings - Fork 6k
Add BearerTokenAuthenticationConverter #14791
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will you please change BearerTokenAuthenticationFilter
to accept an AuthenticationConverter
? Also, OAuth2ResourceServerConfigurer
should configure this in the same way as BearerTokenResolver
. Will you please add that?
Finally, please add tests both for the new authentication converter and corresponding tests in OAuth2ResourceServerConfigurerTests
.
...ramework/security/oauth2/server/resource/web/AbstractBearerTokenAuthenticationConverter.java
Outdated
Show resolved
Hide resolved
Hi @jzheaux! Thanks for your feedback! I would like some advice on the best way to do this. I can add
But there are several problems.
Even if i declare method
And use it only when a custom
Not the most beautiful method, but it partially solves the problem. Next you will need to do something with
But I'm not sure about this solution. In addition, there may be a problem with |
e91cbc1
to
3317b0d
Compare
Hi @jzheaux! I have added
Now it works with authenticationConverter or with bearerTokenResolver, but not with both. If a
In
Tests work as is. |
Hi @jzheaux ! Maybe it’s worth adding |
@CrazyParanoid, thanks for your patience as I have been out of town. We certainly want to remain passive and don't want to change behavior unnecessarily. Also, though, we don't typically add filter components that cannot be used by existing filters. I'll take a look at the PR this week and see if I can find a way to simplify things. Thank you for all your research details. |
@franticticktick I realize it's been a while since you opened this PR. I've reviewed it and posted a PR to your repo that you can merge if you agree to the changes. We can see from there what other changes might be needed. |
e4fbf43
to
e39e608
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates, @franticticktick. This is coming together nicely. I've left some feedback inline.
return this; | ||
} | ||
|
||
public OAuth2ResourceServerConfigurer<H> authenticationConverter(AuthenticationConverter authenticationConverter) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though, bearerTokenResolver
doesn't have a JavaDoc, please add one for authenticationConverter
@@ -194,9 +201,16 @@ public OAuth2ResourceServerConfigurer<H> authenticationManagerResolver( | |||
return this; | |||
} | |||
|
|||
@Deprecated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a JavaDoc here that at least has the following:
/**
* @deprecated please use {@link #authenticationConverter} instead
*/
this.entryPoints.put(requestMatcher, authenticationEntryPoint); | ||
this.deniedHandlers.put(requestMatcher, this.accessDeniedHandler); | ||
this.ignoreCsrfRequestMatchers.add(requestMatcher); | ||
BeanDefinitionBuilder filterBuilder = BeanDefinitionBuilder | ||
.rootBeanDefinition(BearerTokenAuthenticationFilter.class); | ||
BeanMetadataElement authenticationManagerResolver = getAuthenticationManagerResolver(oauth2ResourceServer); | ||
filterBuilder.addConstructorArgValue(authenticationManagerResolver); | ||
filterBuilder.addPropertyValue(BEARER_TOKEN_RESOLVER, bearerTokenResolver); | ||
filterBuilder.addPropertyValue(AUTHENTICATION_CONVERTER, authenticationConverter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically in XML support, two properties that set the same underlying value cannot be specified together.
Will you please add a check that errors if both authentication-converter-ref
and bearer-token-resolver-ref
are specified? The error message would say something like "you cannot use bearer-token-ref
and authentication-converter-ref
in the same oauth2-resource-server
element".
In the Java DSL, the rule is "whatever is the last method called"; however, which one is the "last attribute" is not always clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added such a check. Also, I removed RootBeanDefinition
from getAuthenticationConverter
and getBearerTokenResolver
methods. We need to handle the case when bearerTokenResolver
and authenticationConverter
are null. This will mean that the user left the default configuration and we can set authenticationConverter
to RootBeanDefinition
.
BeanMetadataElement bearerTokenResolver = getBearerTokenResolver(oauth2ResourceServer);
BeanMetadataElement authenticationConverter = getAuthenticationConverter(oauth2ResourceServer);
if (bearerTokenResolver != null && authenticationConverter != null) {
throw new BeanDefinitionStoreException(
"You cannot use bearer-token-ref and authentication-converter-ref in the same oauth2-resource-server element");
}
if (bearerTokenResolver == null && authenticationConverter == null) {
authenticationConverter = new RootBeanDefinition(BearerTokenAuthenticationConverter.class);
}
After these checks we are guaranteed to have either bearerTokenResolver
or authenticationConverter
left. And we can create a requestMatcher
and add PropertyValue to the filterBuilder
.
@@ -64,10 +65,14 @@ final class OAuth2ResourceServerBeanDefinitionParser implements BeanDefinitionPa | |||
|
|||
static final String BEARER_TOKEN_RESOLVER_REF = "bearer-token-resolver-ref"; | |||
|
|||
static final String AUTHENTICATION_CONVERTER_REF = "authentication-converter-ref"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will also need to add this to spring-security-6.5.rnc
and then run ./gradlew :spring-security-config:rncToXsd
to update the equivalent XSD. After that, there are some tests that will check that you have documented the attribute.
@@ -760,10 +762,44 @@ public void getBearerTokenResolverWhenResolverBeanAndAnotherOnTheDslThenTheDslOn | |||
} | |||
|
|||
@Test | |||
public void getBearerTokenResolverWhenNoResolverSpecifiedThenTheDefaultIsUsed() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this test stay? I prefer not to remove tests until the code it is testing is removed. This simplifies proving backward compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work. We'll never get DefaultBearerTokenResolver
, since BearerTokenAuthenticationConverter
now does the main work. At the same time, we have BearerTokenResolverAuthenticationConverterAdapter
, which replaces any custom BearerTokenResolver
, of course, it can have DefaultBearerTokenResolver
, but only if the developer defines it himself. In other words, instead of DefaultBearerTokenResolver
, we now have BearerTokenAuthenticationConverter
.
* {@link BearerTokenAuthenticationToken} | ||
* | ||
* @author Max Batischev | ||
* @since 6.3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update this to be 6.5
* Set the {@link AuthenticationConverter} to use. Defaults to | ||
* {@link BearerTokenAuthenticationConverter}. | ||
* @param authenticationConverter the {@code AuthenticationConverter} to use | ||
* @since 6.3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change this to 6.5
@@ -287,13 +304,184 @@ public void constructorWhenNullAuthenticationManagerResolverThenThrowsException( | |||
// @formatter:on | |||
} | |||
|
|||
@Test | |||
public void doFilterWhenBearerTokenPresentAndConverterSetThenAuthenticates() throws ServletException, IOException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the additional test cleanup! Will you please place any tests or cleanup that aren't part of adding BearerTokenAuthenticationConverter
into a previous commit?
Hi @jzheaux , i need some more time to tidy up this PR. Also, I have a few questions, I'll write a new comment a bit later. |
Closes spring-projectsgh-14750 Signed-off-by: Max Batischev <mblancer@mail.ru>
Signed-off-by: Max Batischev <mblancer@mail.ru>
e39e608
to
ee09666
Compare
Hey @jzheaux , thanks for the warmth. Could you review my latest changes please? |
Closes gh-14750