|
| 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