|
| 1 | +/* |
| 2 | + * Copyright 2014-2024 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.session.security; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | +import java.util.concurrent.ConcurrentHashMap; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import reactor.core.publisher.Flux; |
| 25 | +import reactor.core.publisher.Mono; |
| 26 | +import reactor.test.StepVerifier; |
| 27 | + |
| 28 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
| 29 | +import org.springframework.security.core.context.SecurityContextImpl; |
| 30 | +import org.springframework.security.core.session.ReactiveSessionInformation; |
| 31 | +import org.springframework.session.MapSession; |
| 32 | +import org.springframework.session.ReactiveFindByIndexNameSessionRepository; |
| 33 | +import org.springframework.session.ReactiveMapSessionRepository; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +class SpringSessionBackedReactiveSessionRegistryTests { |
| 38 | + |
| 39 | + static MapSession johnSession1 = new MapSession(); |
| 40 | + static MapSession johnSession2 = new MapSession(); |
| 41 | + static MapSession johnSession3 = new MapSession(); |
| 42 | + |
| 43 | + SpringSessionBackedReactiveSessionRegistry<MapSession> sessionRegistry; |
| 44 | + |
| 45 | + ReactiveFindByIndexNameSessionRepository<MapSession> indexedSessionRepository = new StubIndexedSessionRepository(); |
| 46 | + |
| 47 | + ReactiveMapSessionRepository sessionRepository = new ReactiveMapSessionRepository(new ConcurrentHashMap<>()); |
| 48 | + |
| 49 | + static { |
| 50 | + johnSession1.setAttribute(ReactiveFindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, "johndoe"); |
| 51 | + johnSession2.setAttribute(ReactiveFindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, "johndoe"); |
| 52 | + johnSession3.setAttribute(ReactiveFindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, "johndoe"); |
| 53 | + } |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + void setup() { |
| 57 | + this.sessionRegistry = new SpringSessionBackedReactiveSessionRegistry<>(this.sessionRepository, |
| 58 | + this.indexedSessionRepository); |
| 59 | + this.sessionRepository.save(johnSession1).block(); |
| 60 | + this.sessionRepository.save(johnSession2).block(); |
| 61 | + this.sessionRepository.save(johnSession3).block(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void saveSessionInformationThenDoNothing() { |
| 66 | + StepVerifier.create(this.sessionRegistry.saveSessionInformation(null)).expectComplete().verify(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void removeSessionInformationThenDoNothing() { |
| 71 | + StepVerifier.create(this.sessionRegistry.removeSessionInformation(null)).expectComplete().verify(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void updateLastAccessTimeThenDoNothing() { |
| 76 | + StepVerifier.create(this.sessionRegistry.updateLastAccessTime(null)).expectComplete().verify(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void getSessionInformationWhenPrincipalIndexNamePresentThenPrincipalResolved() { |
| 81 | + MapSession session = this.sessionRepository.createSession().block(); |
| 82 | + session.setAttribute(ReactiveFindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, "johndoe"); |
| 83 | + this.sessionRepository.save(session).block(); |
| 84 | + StepVerifier.create(this.sessionRegistry.getSessionInformation(session.getId())) |
| 85 | + .assertNext((sessionInformation) -> { |
| 86 | + assertThat(sessionInformation.getSessionId()).isEqualTo(session.getId()); |
| 87 | + assertThat(sessionInformation.getLastAccessTime()).isEqualTo(session.getLastAccessedTime()); |
| 88 | + assertThat(sessionInformation.getPrincipal()).isEqualTo("johndoe"); |
| 89 | + }) |
| 90 | + .verifyComplete(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void getSessionInformationWhenSecurityContextAttributePresentThenPrincipalResolved() { |
| 95 | + MapSession session = this.sessionRepository.createSession().block(); |
| 96 | + TestingAuthenticationToken authentication = new TestingAuthenticationToken("johndoe", "n/a"); |
| 97 | + SecurityContextImpl securityContext = new SecurityContextImpl(); |
| 98 | + securityContext.setAuthentication(authentication); |
| 99 | + session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext); |
| 100 | + this.sessionRepository.save(session).block(); |
| 101 | + StepVerifier.create(this.sessionRegistry.getSessionInformation(session.getId())) |
| 102 | + .assertNext((sessionInformation) -> { |
| 103 | + assertThat(sessionInformation.getSessionId()).isEqualTo(session.getId()); |
| 104 | + assertThat(sessionInformation.getLastAccessTime()).isEqualTo(session.getLastAccessedTime()); |
| 105 | + assertThat(sessionInformation.getPrincipal()).isEqualTo("johndoe"); |
| 106 | + }) |
| 107 | + .verifyComplete(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void getSessionInformationWhenNoResolvablePrincipalThenPrincipalBlank() { |
| 112 | + MapSession session = this.sessionRepository.createSession().block(); |
| 113 | + this.sessionRepository.save(session).block(); |
| 114 | + StepVerifier.create(this.sessionRegistry.getSessionInformation(session.getId())) |
| 115 | + .assertNext((sessionInformation) -> { |
| 116 | + assertThat(sessionInformation.getSessionId()).isEqualTo(session.getId()); |
| 117 | + assertThat(sessionInformation.getLastAccessTime()).isEqualTo(session.getLastAccessedTime()); |
| 118 | + assertThat(sessionInformation.getPrincipal()).isEqualTo(""); |
| 119 | + }) |
| 120 | + .verifyComplete(); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void getSessionInformationWhenInvalidateThenRemovedFromSessionRepository() { |
| 125 | + MapSession session = this.sessionRepository.createSession().block(); |
| 126 | + this.sessionRepository.save(session).block(); |
| 127 | + Mono<Void> publisher = this.sessionRegistry.getSessionInformation(session.getId()) |
| 128 | + .flatMap(ReactiveSessionInformation::invalidate); |
| 129 | + StepVerifier.create(publisher).verifyComplete(); |
| 130 | + StepVerifier.create(this.sessionRepository.findById(session.getId())).expectComplete().verify(); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void getAllSessionsWhenSessionsExistsThenReturned() { |
| 135 | + Flux<ReactiveSessionInformation> sessions = this.sessionRegistry.getAllSessions("johndoe"); |
| 136 | + StepVerifier.create(sessions) |
| 137 | + .assertNext((sessionInformation) -> assertThat(sessionInformation.getPrincipal()).isEqualTo("johndoe")) |
| 138 | + .assertNext((sessionInformation) -> assertThat(sessionInformation.getPrincipal()).isEqualTo("johndoe")) |
| 139 | + .assertNext((sessionInformation) -> assertThat(sessionInformation.getPrincipal()).isEqualTo("johndoe")) |
| 140 | + .verifyComplete(); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void getAllSessionsWhenInvalidateThenSessionsRemovedFromRepository() { |
| 145 | + this.sessionRegistry.getAllSessions("johndoe").flatMap(ReactiveSessionInformation::invalidate).blockLast(); |
| 146 | + StepVerifier.create(this.sessionRepository.findById(johnSession1.getId())).expectComplete().verify(); |
| 147 | + StepVerifier.create(this.sessionRepository.findById(johnSession2.getId())).expectComplete().verify(); |
| 148 | + StepVerifier.create(this.sessionRepository.findById(johnSession3.getId())).expectComplete().verify(); |
| 149 | + } |
| 150 | + |
| 151 | + static class StubIndexedSessionRepository implements ReactiveFindByIndexNameSessionRepository<MapSession> { |
| 152 | + |
| 153 | + Map<String, MapSession> johnSessions = Map.of(johnSession1.getId(), johnSession1, johnSession2.getId(), |
| 154 | + johnSession2, johnSession3.getId(), johnSession3); |
| 155 | + |
| 156 | + @Override |
| 157 | + public Mono<Map<String, MapSession>> findByIndexNameAndIndexValue(String indexName, String indexValue) { |
| 158 | + if ("johndoe".equals(indexValue)) { |
| 159 | + return Mono.just(this.johnSessions); |
| 160 | + } |
| 161 | + return Mono.empty(); |
| 162 | + } |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments