|
| 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.context; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.mockito.Mock; |
| 24 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 25 | + |
| 26 | +import org.springframework.messaging.Message; |
| 27 | +import org.springframework.messaging.MessageChannel; |
| 28 | +import org.springframework.messaging.MessageHandler; |
| 29 | +import org.springframework.messaging.simp.SimpMessageHeaderAccessor; |
| 30 | +import org.springframework.messaging.support.MessageBuilder; |
| 31 | +import org.springframework.security.authentication.AnonymousAuthenticationToken; |
| 32 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
| 33 | +import org.springframework.security.core.Authentication; |
| 34 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 35 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
| 36 | +import org.springframework.security.core.context.SecurityContextImpl; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | +import static org.mockito.Mockito.spy; |
| 40 | +import static org.mockito.Mockito.times; |
| 41 | +import static org.mockito.Mockito.verify; |
| 42 | + |
| 43 | +@ExtendWith(MockitoExtension.class) |
| 44 | +public class SecurityContextPropagationChannelInterceptorTests { |
| 45 | + |
| 46 | + @Mock |
| 47 | + MessageChannel channel; |
| 48 | + |
| 49 | + @Mock |
| 50 | + MessageHandler handler; |
| 51 | + |
| 52 | + MessageBuilder<String> messageBuilder; |
| 53 | + |
| 54 | + Authentication authentication; |
| 55 | + |
| 56 | + SecurityContextPropagationChannelInterceptor interceptor; |
| 57 | + |
| 58 | + @BeforeEach |
| 59 | + public void setup() { |
| 60 | + this.authentication = new TestingAuthenticationToken("user", "pass", "ROLE_USER"); |
| 61 | + this.messageBuilder = MessageBuilder.withPayload("payload"); |
| 62 | + this.interceptor = new SecurityContextPropagationChannelInterceptor(); |
| 63 | + } |
| 64 | + |
| 65 | + @AfterEach |
| 66 | + public void cleanup() { |
| 67 | + this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null); |
| 68 | + SecurityContextHolder.clearContext(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void preSendDefaultHeader() { |
| 73 | + SecurityContextHolder.getContext().setAuthentication(this.authentication); |
| 74 | + Message<?> message = this.interceptor.preSend(this.messageBuilder.build(), this.channel); |
| 75 | + assertThat(message.getHeaders()).containsEntry(SimpMessageHeaderAccessor.USER_HEADER, this.authentication); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void preSendCustomHeader() { |
| 80 | + SecurityContextHolder.getContext().setAuthentication(this.authentication); |
| 81 | + String headerName = "header"; |
| 82 | + this.interceptor = new SecurityContextPropagationChannelInterceptor(headerName); |
| 83 | + Message<?> message = this.interceptor.preSend(this.messageBuilder.build(), this.channel); |
| 84 | + assertThat(message.getHeaders()).containsEntry(headerName, this.authentication); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void preSendWhenCustomSecurityContextHolderStrategyThenUserSet() { |
| 89 | + SecurityContextHolderStrategy strategy = spy(SecurityContextHolder.getContextHolderStrategy()); |
| 90 | + strategy.setContext(new SecurityContextImpl(this.authentication)); |
| 91 | + this.interceptor.setSecurityContextHolderStrategy(strategy); |
| 92 | + Message<?> message = this.interceptor.preSend(this.messageBuilder.build(), this.channel); |
| 93 | + this.interceptor.beforeHandle(message, this.channel, this.handler); |
| 94 | + verify(strategy, times(2)).getContext(); |
| 95 | + assertThat(strategy.getContext().getAuthentication()).isSameAs(this.authentication); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void preSendUserNoContext() { |
| 100 | + Message<?> message = this.interceptor.preSend(this.messageBuilder.build(), this.channel); |
| 101 | + assertThat(message.getHeaders()).containsKey(SimpMessageHeaderAccessor.USER_HEADER); |
| 102 | + assertThat(message.getHeaders().get(SimpMessageHeaderAccessor.USER_HEADER)) |
| 103 | + .isInstanceOf(AnonymousAuthenticationToken.class); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void beforeHandleUserSet() { |
| 108 | + this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication); |
| 109 | + this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler); |
| 110 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void postReceiveUserSet() { |
| 115 | + this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication); |
| 116 | + this.interceptor.postReceive(this.messageBuilder.build(), this.channel); |
| 117 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void authenticationIsPropagatedFromPreSendToPostReceive() { |
| 122 | + SecurityContextHolder.getContext().setAuthentication(this.authentication); |
| 123 | + Message<?> message = this.interceptor.preSend(this.messageBuilder.build(), this.channel); |
| 124 | + assertThat(message.getHeaders().get(SimpMessageHeaderAccessor.USER_HEADER)).isSameAs(this.authentication); |
| 125 | + this.interceptor.postReceive(message, this.channel); |
| 126 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void beforeHandleUserNotSet() { |
| 131 | + this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler); |
| 132 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void afterMessageHandledUserNotSet() { |
| 137 | + this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null); |
| 138 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void afterMessageHandled() { |
| 143 | + SecurityContextHolder.getContext().setAuthentication(this.authentication); |
| 144 | + this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication); |
| 145 | + this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null); |
| 146 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void restoresOriginalContext() { |
| 151 | + TestingAuthenticationToken original = new TestingAuthenticationToken("original", "original", "ROLE_USER"); |
| 152 | + SecurityContextHolder.getContext().setAuthentication(original); |
| 153 | + this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication); |
| 154 | + this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler); |
| 155 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication); |
| 156 | + this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null); |
| 157 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(original); |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments