Skip to content

Commit fbdecda

Browse files
committed
Add Mapping to Invalid Bearer Token
Fixes gh-7793
1 parent 25d029b commit fbdecda

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

core/src/main/java/org/springframework/security/authentication/DefaultAuthenticationEventPublisher.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -92,6 +92,9 @@ public DefaultAuthenticationEventPublisher(
9292
addMapping(
9393
"org.springframework.security.authentication.cas.ProxyUntrustedException",
9494
AuthenticationFailureProxyUntrustedEvent.class);
95+
addMapping(
96+
"org.springframework.security.oauth2.server.resource.InvalidBearerTokenException",
97+
AuthenticationFailureBadCredentialsEvent.class);
9598
}
9699

97100
public void publishAuthenticationSuccess(Authentication authentication) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2002-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.oauth2.server.resource;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.context.ApplicationEventPublisher;
22+
import org.springframework.security.authentication.DefaultAuthenticationEventPublisher;
23+
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
24+
import org.springframework.security.core.Authentication;
25+
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
26+
27+
import static org.mockito.ArgumentMatchers.isA;
28+
import static org.mockito.Mockito.mock;
29+
import static org.mockito.Mockito.times;
30+
import static org.mockito.Mockito.verify;
31+
import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
32+
33+
/**
34+
* Tests for {@link DefaultAuthenticationEventPublisher}'s bearer token use cases
35+
*
36+
* {@see DefaultAuthenticationEventPublisher}
37+
*/
38+
public class DefaultAuthenticationEventPublisherBearerTokenTests {
39+
DefaultAuthenticationEventPublisher publisher;
40+
41+
@Test
42+
public void publishAuthenticationFailureWhenInvalidBearerTokenExceptionThenMaps() {
43+
ApplicationEventPublisher appPublisher = mock(ApplicationEventPublisher.class);
44+
Authentication authentication = new JwtAuthenticationToken(jwt().build());
45+
Exception cause = new Exception();
46+
this.publisher = new DefaultAuthenticationEventPublisher(appPublisher);
47+
this.publisher.publishAuthenticationFailure(new InvalidBearerTokenException("invalid"), authentication);
48+
this.publisher.publishAuthenticationFailure(new InvalidBearerTokenException("invalid", cause), authentication);
49+
verify(appPublisher, times(2)).publishEvent(
50+
isA(AuthenticationFailureBadCredentialsEvent.class));
51+
}
52+
}

0 commit comments

Comments
 (0)