@Override public Mono<Void> removeAuthorizedClient(String clientRegistrationId, Authentication principal, ServerWebExchange exchange) { Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty"); Assert.notNull(exchange, "exchange cannot be null"); return exchange.getSession() .doOnSuccess(session -> { Map<String, OAuth2AuthorizedClient> authorizedClients = getAuthorizedClients(session); authorizedClients.remove(clientRegistrationId); if (authorizedClients.isEmpty()) { session.getAttributes().remove(this.sessionAttributeName); } else { session.getAttributes().put(this.sessionAttributeName, authorizedClients); } }) .then(Mono.empty()); }
@Test public void removeAuthorizedClientWhenClient1SavedAndClient2RemovedThenClient1NotRemoved() { OAuth2AuthorizedClient authorizedClient1 = new OAuth2AuthorizedClient( this.registration1, this.principalName1, mock(OAuth2AccessToken.class)); this.authorizedClientRepository.saveAuthorizedClient(authorizedClient1, null, this.exchange).block(); // Remove registrationId2 (never added so is not removed either) this.authorizedClientRepository.removeAuthorizedClient( this.registrationId2, null, this.exchange); OAuth2AuthorizedClient loadedAuthorizedClient1 = this.authorizedClientRepository.loadAuthorizedClient( this.registrationId1, null, this.exchange).block(); assertThat(loadedAuthorizedClient1).isNotNull(); assertThat(loadedAuthorizedClient1).isSameAs(authorizedClient1); }
@Test public void loadAuthorizedClientWhenSavedThenReturnAuthorizedClient() { OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient( this.registration1, this.principalName1, mock(OAuth2AccessToken.class)); this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, null, this.exchange).block(); OAuth2AuthorizedClient loadedAuthorizedClient = this.authorizedClientRepository.loadAuthorizedClient(this.registrationId1, null, this.exchange).block(); assertThat(loadedAuthorizedClient).isEqualTo(authorizedClient); }
@Test public void removeAuthorizedClientWhenRequestIsNullThenThrowIllegalArgumentException() { assertThatThrownBy(() -> this.authorizedClientRepository.removeAuthorizedClient( this.registrationId1, null, null)).isInstanceOf(IllegalArgumentException.class); }
@Test public void loadAuthorizedClientWhenClientRegistrationIdIsNullThenThrowIllegalArgumentException() { assertThatThrownBy(() -> this.authorizedClientRepository.loadAuthorizedClient(null, null, this.exchange).block()) .isInstanceOf(IllegalArgumentException.class); }
@Test public void saveAuthorizedClientWhenAuthorizedClientIsNullThenThrowIllegalArgumentException() { assertThatThrownBy(() -> this.authorizedClientRepository.saveAuthorizedClient(null, null, this.exchange).block()) .isInstanceOf(IllegalArgumentException.class); }
@Test public void saveAuthorizedClientWhenSavedThenSavedToSession() { OAuth2AuthorizedClient expected = new OAuth2AuthorizedClient( this.registration2, this.principalName1, mock(OAuth2AccessToken.class)); this.authorizedClientRepository.saveAuthorizedClient(expected, null, this.exchange).block(); OAuth2AuthorizedClient result = this.authorizedClientRepository .loadAuthorizedClient(this.registrationId2, null, this.exchange).block(); assertThat(result).isEqualTo(expected); }
@Test public void removeAuthorizedClientWhenClientRegistrationIdIsNullThenThrowIllegalArgumentException() { assertThatThrownBy(() -> this.authorizedClientRepository.removeAuthorizedClient( null, null, this.exchange)).isInstanceOf(IllegalArgumentException.class); }
@Test public void loadAuthorizedClientWhenRequestIsNullThenThrowIllegalArgumentException() { assertThatThrownBy(() -> this.authorizedClientRepository.loadAuthorizedClient(this.registrationId1, null, null).block()) .isInstanceOf(IllegalArgumentException.class); }
@Test public void saveAuthorizedClientWhenRequestIsNullThenThrowIllegalArgumentException() { OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient( this.registration2, this.principalName1, mock(OAuth2AccessToken.class)); assertThatThrownBy(() -> this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, null, null).block()) .isInstanceOf(IllegalArgumentException.class); }
@Test public void removeAuthorizedClientWhenSavedThenRemoved() { OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient( this.registration2, this.principalName1, mock(OAuth2AccessToken.class)); this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, null, this.exchange).block(); OAuth2AuthorizedClient loadedAuthorizedClient = this.authorizedClientRepository.loadAuthorizedClient( this.registrationId2, null, this.exchange).block(); assertThat(loadedAuthorizedClient).isSameAs(authorizedClient); this.authorizedClientRepository.removeAuthorizedClient( this.registrationId2, null, this.exchange).block(); loadedAuthorizedClient = this.authorizedClientRepository.loadAuthorizedClient( this.registrationId2, null, this.exchange).block(); assertThat(loadedAuthorizedClient).isNull(); }
@Override public Mono<Void> saveAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal, ServerWebExchange exchange) { Assert.notNull(authorizedClient, "authorizedClient cannot be null"); Assert.notNull(exchange, "exchange cannot be null"); return exchange.getSession() .doOnSuccess(session -> { Map<String, OAuth2AuthorizedClient> authorizedClients = getAuthorizedClients(session); authorizedClients.put(authorizedClient.getClientRegistration().getRegistrationId(), authorizedClient); session.getAttributes().put(this.sessionAttributeName, authorizedClients); }) .then(Mono.empty()); }
@Test public void removeAuthorizedClientWhenPrincipalNameIsNullThenExceptionNotThrown() { this.authorizedClientRepository.removeAuthorizedClient(this.registrationId1, null, this.exchange); }
@Test public void loadAuthorizedClientWhenPrincipalNameIsNullThenExceptionNotThrown() { this.authorizedClientRepository.loadAuthorizedClient(this.registrationId1, null, this.exchange).block(); }
@Test public void saveAuthorizedClientWhenAuthenticationIsNullThenExceptionNotThrown() { OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient( this.registration2, this.principalName1, mock(OAuth2AccessToken.class)); this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, null, this.exchange).block(); }
@Test public void removeAuthorizedClientWhenClient1Client2SavedAndClient1RemovedThenClient2NotRemoved() { OAuth2AuthorizedClient authorizedClient1 = new OAuth2AuthorizedClient( this.registration1, this.principalName1, mock(OAuth2AccessToken.class)); this.authorizedClientRepository.saveAuthorizedClient(authorizedClient1, null, this.exchange).block(); OAuth2AuthorizedClient authorizedClient2 = new OAuth2AuthorizedClient( this.registration2, this.principalName1, mock(OAuth2AccessToken.class)); this.authorizedClientRepository.saveAuthorizedClient(authorizedClient2, null, this.exchange).block(); this.authorizedClientRepository.removeAuthorizedClient( this.registrationId1, null, this.exchange).block(); OAuth2AuthorizedClient loadedAuthorizedClient2 = this.authorizedClientRepository.loadAuthorizedClient( this.registrationId2, null, this.exchange).block(); assertThat(loadedAuthorizedClient2).isNotNull(); assertThat(loadedAuthorizedClient2).isSameAs(authorizedClient2); } }
@Override public Mono<Void> removeAuthorizedClient(String clientRegistrationId, Authentication principal, ServerWebExchange exchange) { Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty"); Assert.notNull(exchange, "exchange cannot be null"); return exchange.getSession() .doOnSuccess(session -> { Map<String, OAuth2AuthorizedClient> authorizedClients = getAuthorizedClients(session); authorizedClients.remove(clientRegistrationId); if (authorizedClients.isEmpty()) { session.getAttributes().remove(this.sessionAttributeName); } else { session.getAttributes().put(this.sessionAttributeName, authorizedClients); } }) .then(Mono.empty()); }
@Test public void removeAuthorizedClientWhenNotSavedThenSessionNotCreated() { this.authorizedClientRepository.removeAuthorizedClient( this.registrationId2, null, this.exchange); assertThat(this.exchange.getSession().block().isStarted()).isFalse(); }
@Test public void loadAuthorizedClientWhenClientRegistrationNotFoundThenReturnNull() { OAuth2AuthorizedClient authorizedClient = this.authorizedClientRepository.loadAuthorizedClient("registration-not-found", null, this.exchange).block(); assertThat(authorizedClient).isNull(); }
@Test public void removeAuthorizedClientWhenSavedThenRemovedFromSession() { OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient( this.registration1, this.principalName1, mock(OAuth2AccessToken.class)); this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, null, this.exchange).block(); OAuth2AuthorizedClient loadedAuthorizedClient = this.authorizedClientRepository.loadAuthorizedClient( this.registrationId1, null, this.exchange).block(); assertThat(loadedAuthorizedClient).isSameAs(authorizedClient); this.authorizedClientRepository.removeAuthorizedClient( this.registrationId1, null, this.exchange).block(); WebSession session = this.exchange.getSession().block(); assertThat(session).isNotNull(); assertThat(session.getAttributes()).isEmpty(); }