|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 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.messaging.access.intercept; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | +import java.util.function.Supplier; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.springframework.messaging.Message; |
| 25 | +import org.springframework.messaging.MessageHeaders; |
| 26 | +import org.springframework.messaging.simp.SimpMessageHeaderAccessor; |
| 27 | +import org.springframework.messaging.simp.SimpMessageType; |
| 28 | +import org.springframework.messaging.support.GenericMessage; |
| 29 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
| 30 | +import org.springframework.security.authorization.AuthorizationManager; |
| 31 | +import org.springframework.security.core.Authentication; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.mockito.Mockito.mock; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for {@link MessageMatcherDelegatingAuthorizationManager} |
| 38 | + */ |
| 39 | +public final class MessageMatcherDelegatingAuthorizationManagerTests { |
| 40 | + |
| 41 | + @Test |
| 42 | + void checkWhenPermitAllThenPermits() { |
| 43 | + AuthorizationManager<Message<?>> authorizationManager = builder().anyMessage().permitAll().build(); |
| 44 | + Message<?> message = new GenericMessage<>(new Object()); |
| 45 | + assertThat(authorizationManager.check(mock(Supplier.class), message).isGranted()).isTrue(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void checkWhenAnyMessageHasRoleThenRequires() { |
| 50 | + AuthorizationManager<Message<?>> authorizationManager = builder().anyMessage().hasRole("USER").build(); |
| 51 | + Message<?> message = new GenericMessage<>(new Object()); |
| 52 | + Authentication user = new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
| 53 | + assertThat(authorizationManager.check(() -> user, message).isGranted()).isTrue(); |
| 54 | + Authentication admin = new TestingAuthenticationToken("user", "password", "ROLE_ADMIN"); |
| 55 | + assertThat(authorizationManager.check(() -> admin, message).isGranted()).isFalse(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void checkWhenSimpDestinationMatchesThenUses() { |
| 60 | + AuthorizationManager<Message<?>> authorizationManager = builder().simpDestMatchers("destination").permitAll() |
| 61 | + .anyMessage().denyAll().build(); |
| 62 | + MessageHeaders headers = new MessageHeaders( |
| 63 | + Map.of(SimpMessageHeaderAccessor.DESTINATION_HEADER, "destination")); |
| 64 | + Message<?> message = new GenericMessage<>(new Object(), headers); |
| 65 | + assertThat(authorizationManager.check(mock(Supplier.class), message).isGranted()).isTrue(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void checkWhenNullDestinationHeaderMatchesThenUses() { |
| 70 | + AuthorizationManager<Message<?>> authorizationManager = builder().nullDestMatcher().permitAll().anyMessage() |
| 71 | + .denyAll().build(); |
| 72 | + Message<?> message = new GenericMessage<>(new Object()); |
| 73 | + assertThat(authorizationManager.check(mock(Supplier.class), message).isGranted()).isTrue(); |
| 74 | + MessageHeaders headers = new MessageHeaders( |
| 75 | + Map.of(SimpMessageHeaderAccessor.DESTINATION_HEADER, "destination")); |
| 76 | + message = new GenericMessage<>(new Object(), headers); |
| 77 | + assertThat(authorizationManager.check(mock(Supplier.class), message).isGranted()).isFalse(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void checkWhenSimpTypeMatchesThenUses() { |
| 82 | + AuthorizationManager<Message<?>> authorizationManager = builder().simpTypeMatchers(SimpMessageType.CONNECT) |
| 83 | + .permitAll().anyMessage().denyAll().build(); |
| 84 | + MessageHeaders headers = new MessageHeaders( |
| 85 | + Map.of(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.CONNECT)); |
| 86 | + Message<?> message = new GenericMessage<>(new Object(), headers); |
| 87 | + assertThat(authorizationManager.check(mock(Supplier.class), message).isGranted()).isTrue(); |
| 88 | + } |
| 89 | + |
| 90 | + private MessageMatcherDelegatingAuthorizationManager.Builder builder() { |
| 91 | + return MessageMatcherDelegatingAuthorizationManager.builder(); |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments