Skip to content

Commit 1695d03

Browse files
dukbongsjohnr
authored andcommitted
Assert WebSession is not null
Issue gh-14975
1 parent e7ea409 commit 1695d03

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

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

Lines changed: 4 additions & 7 deletions
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.
@@ -96,12 +96,9 @@ public Mono<Void> removeAuthorizedClient(String clientRegistrationId, Authentica
9696

9797
@SuppressWarnings("unchecked")
9898
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;
99+
Assert.notNull(session, "session cannot be null");
100+
Map<String, OAuth2AuthorizedClient> authorizedClients = session.getAttribute(this.sessionAttributeName);
101+
return (authorizedClients != null) ? authorizedClients : new HashMap<>();
105102
}
106103

107104
}

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

Lines changed: 23 additions & 1 deletion
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.
@@ -25,10 +25,12 @@
2525
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
2626
import org.springframework.security.oauth2.core.OAuth2AccessToken;
2727
import org.springframework.web.server.WebSession;
28+
import reactor.core.publisher.Mono;
2829

2930
import static org.assertj.core.api.Assertions.assertThat;
3031
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3132
import static org.mockito.Mockito.mock;
33+
import static org.mockito.Mockito.when;
3234

3335
/**
3436
* @author Rob Winch
@@ -201,5 +203,25 @@ public void removeAuthorizedClientWhenClient1Client2SavedAndClient1RemovedThenCl
201203
assertThat(loadedAuthorizedClient2).isNotNull();
202204
assertThat(loadedAuthorizedClient2).isSameAs(authorizedClient2);
203205
}
206+
207+
@Test
208+
public void saveAuthorizedClientWhenSessionIsNullThenThrowIllegalArgumentException() {
209+
MockServerWebExchange mockedExchange = mock(MockServerWebExchange.class);
210+
when(mockedExchange.getSession()).thenReturn(Mono.empty());
211+
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration1, this.principalName1,
212+
mock(OAuth2AccessToken.class));
213+
assertThatIllegalArgumentException().isThrownBy(
214+
() -> authorizedClientRepository.saveAuthorizedClient(authorizedClient, null, mockedExchange).block())
215+
.withMessage("session cannot be null");
216+
}
217+
218+
@Test
219+
public void removeAuthorizedClientWhenSessionIsNullThenThrowIllegalArgumentException() {
220+
MockServerWebExchange mockedExchange = mock(MockServerWebExchange.class);
221+
when(mockedExchange.getSession()).thenReturn(Mono.empty());
222+
assertThatIllegalArgumentException().isThrownBy(
223+
() -> authorizedClientRepository.removeAuthorizedClient(this.registrationId1, null, mockedExchange).block())
224+
.withMessage("session cannot be null");
225+
}
204226

205227
}

0 commit comments

Comments
 (0)