Skip to content

Commit db9f593

Browse files
committed
Merge branch '6.2.x' into 6.3.x
2 parents dd5edeb + 5a1d261 commit db9f593

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/server/WebSessionServerOAuth2AuthorizedClientRepository.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -94,14 +94,10 @@ public Mono<Void> removeAuthorizedClient(String clientRegistrationId, Authentica
9494
// @formatter:on
9595
}
9696

97-
@SuppressWarnings("unchecked")
9897
private Map<String, OAuth2AuthorizedClient> getAuthorizedClients(WebSession session) {
99-
Map<String, OAuth2AuthorizedClient> authorizedClients = (session != null)
100-
? (Map<String, OAuth2AuthorizedClient>) session.getAttribute(this.sessionAttributeName) : null;
101-
if (authorizedClients == null) {
102-
authorizedClients = new HashMap<>();
103-
}
104-
return authorizedClients;
98+
Assert.notNull(session, "session cannot be null");
99+
Map<String, OAuth2AuthorizedClient> authorizedClients = session.getAttribute(this.sessionAttributeName);
100+
return (authorizedClients != null) ? authorizedClients : new HashMap<>();
105101
}
106102

107103
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/web/server/WebSessionServerOAuth2AuthorizedClientRepositoryTests.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -17,17 +17,20 @@
1717
package org.springframework.security.oauth2.client.web.server;
1818

1919
import org.junit.jupiter.api.Test;
20+
import reactor.core.publisher.Mono;
2021

2122
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
2223
import org.springframework.mock.web.server.MockServerWebExchange;
2324
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
2425
import org.springframework.security.oauth2.client.registration.ClientRegistration;
2526
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
2627
import org.springframework.security.oauth2.core.OAuth2AccessToken;
28+
import org.springframework.web.server.ServerWebExchange;
2729
import org.springframework.web.server.WebSession;
2830

2931
import static org.assertj.core.api.Assertions.assertThat;
3032
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
33+
import static org.mockito.BDDMockito.given;
3134
import static org.mockito.Mockito.mock;
3235

3336
/**
@@ -202,4 +205,28 @@ public void removeAuthorizedClientWhenClient1Client2SavedAndClient1RemovedThenCl
202205
assertThat(loadedAuthorizedClient2).isSameAs(authorizedClient2);
203206
}
204207

208+
@Test
209+
public void saveAuthorizedClientWhenSessionIsNullThenThrowIllegalArgumentException() {
210+
ServerWebExchange exchange = mock(ServerWebExchange.class);
211+
given(exchange.getSession()).willReturn(Mono.empty());
212+
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration1, this.principalName1,
213+
mock(OAuth2AccessToken.class));
214+
// @formatter:off
215+
assertThatIllegalArgumentException()
216+
.isThrownBy(() -> this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, null, exchange).block())
217+
.withMessage("session cannot be null");
218+
// @formatter:on
219+
}
220+
221+
@Test
222+
public void removeAuthorizedClientWhenSessionIsNullThenThrowIllegalArgumentException() {
223+
ServerWebExchange exchange = mock(ServerWebExchange.class);
224+
given(exchange.getSession()).willReturn(Mono.empty());
225+
// @formatter:off
226+
assertThatIllegalArgumentException()
227+
.isThrownBy(() -> this.authorizedClientRepository.removeAuthorizedClient(this.registrationId1, null, exchange).block())
228+
.withMessage("session cannot be null");
229+
// @formatter:on
230+
}
231+
205232
}

0 commit comments

Comments
 (0)