Make OAuth2User extends OAuth2AuthenticatedPrincipal

Fixes gh-7378
This commit is contained in:
Eddú Meléndez 2019-09-05 19:04:39 -05:00 committed by Josh Cummings
parent aa533c2565
commit 91bf1c782a
5 changed files with 31 additions and 44 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -48,6 +48,7 @@ import static org.springframework.security.oauth2.core.TestOAuth2AccessTokens.no
* Tests for {@link CustomUserTypesOAuth2UserService}. * Tests for {@link CustomUserTypesOAuth2UserService}.
* *
* @author Joe Grandja * @author Joe Grandja
* @author Eddú Meléndez
*/ */
public class CustomUserTypesOAuth2UserServiceTests { public class CustomUserTypesOAuth2UserServiceTests {
private ClientRegistration.Builder clientRegistrationBuilder; private ClientRegistration.Builder clientRegistrationBuilder;
@ -134,10 +135,10 @@ public class CustomUserTypesOAuth2UserServiceTests {
assertThat(user.getName()).isEqualTo("first last"); assertThat(user.getName()).isEqualTo("first last");
assertThat(user.getAttributes().size()).isEqualTo(4); assertThat(user.getAttributes().size()).isEqualTo(4);
assertThat(user.getAttributes().get("id")).isEqualTo("12345"); assertThat((String) user.getAttribute("id")).isEqualTo("12345");
assertThat(user.getAttributes().get("name")).isEqualTo("first last"); assertThat((String) user.getAttribute("name")).isEqualTo("first last");
assertThat(user.getAttributes().get("login")).isEqualTo("user1"); assertThat((String) user.getAttribute("login")).isEqualTo("user1");
assertThat(user.getAttributes().get("email")).isEqualTo("user1@example.com"); assertThat((String) user.getAttribute("email")).isEqualTo("user1@example.com");
assertThat(user.getAuthorities().size()).isEqualTo(1); assertThat(user.getAuthorities().size()).isEqualTo(1);
assertThat(user.getAuthorities().iterator().next().getAuthority()).isEqualTo("ROLE_USER"); assertThat(user.getAuthorities().iterator().next().getAuthority()).isEqualTo("ROLE_USER");

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -61,6 +61,7 @@ import static org.springframework.security.oauth2.core.TestOAuth2AccessTokens.sc
* Tests for {@link DefaultOAuth2UserService}. * Tests for {@link DefaultOAuth2UserService}.
* *
* @author Joe Grandja * @author Joe Grandja
* @author Eddú Meléndez
*/ */
public class DefaultOAuth2UserServiceTests { public class DefaultOAuth2UserServiceTests {
private ClientRegistration.Builder clientRegistrationBuilder; private ClientRegistration.Builder clientRegistrationBuilder;
@ -146,12 +147,12 @@ public class DefaultOAuth2UserServiceTests {
assertThat(user.getName()).isEqualTo("user1"); assertThat(user.getName()).isEqualTo("user1");
assertThat(user.getAttributes().size()).isEqualTo(6); assertThat(user.getAttributes().size()).isEqualTo(6);
assertThat(user.getAttributes().get("user-name")).isEqualTo("user1"); assertThat((String) user.getAttribute("user-name")).isEqualTo("user1");
assertThat(user.getAttributes().get("first-name")).isEqualTo("first"); assertThat((String) user.getAttribute("first-name")).isEqualTo("first");
assertThat(user.getAttributes().get("last-name")).isEqualTo("last"); assertThat((String) user.getAttribute("last-name")).isEqualTo("last");
assertThat(user.getAttributes().get("middle-name")).isEqualTo("middle"); assertThat((String) user.getAttribute("middle-name")).isEqualTo("middle");
assertThat(user.getAttributes().get("address")).isEqualTo("address"); assertThat((String) user.getAttribute("address")).isEqualTo("address");
assertThat(user.getAttributes().get("email")).isEqualTo("user1@example.com"); assertThat((String) user.getAttribute("email")).isEqualTo("user1@example.com");
assertThat(user.getAuthorities().size()).isEqualTo(1); assertThat(user.getAuthorities().size()).isEqualTo(1);
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class); assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -61,6 +61,7 @@ import static org.springframework.security.oauth2.core.TestOAuth2AccessTokens.sc
/** /**
* @author Rob Winch * @author Rob Winch
* @author Eddú Meléndez
* @since 5.1 * @since 5.1
*/ */
public class DefaultReactiveOAuth2UserServiceTests { public class DefaultReactiveOAuth2UserServiceTests {
@ -137,12 +138,12 @@ public class DefaultReactiveOAuth2UserServiceTests {
assertThat(user.getName()).isEqualTo("user1"); assertThat(user.getName()).isEqualTo("user1");
assertThat(user.getAttributes().size()).isEqualTo(6); assertThat(user.getAttributes().size()).isEqualTo(6);
assertThat(user.getAttributes().get("id")).isEqualTo("user1"); assertThat((String) user.getAttribute("id")).isEqualTo("user1");
assertThat(user.getAttributes().get("first-name")).isEqualTo("first"); assertThat((String) user.getAttribute("first-name")).isEqualTo("first");
assertThat(user.getAttributes().get("last-name")).isEqualTo("last"); assertThat((String) user.getAttribute("last-name")).isEqualTo("last");
assertThat(user.getAttributes().get("middle-name")).isEqualTo("middle"); assertThat((String) user.getAttribute("middle-name")).isEqualTo("middle");
assertThat(user.getAttributes().get("address")).isEqualTo("address"); assertThat((String) user.getAttribute("address")).isEqualTo("address");
assertThat(user.getAttributes().get("email")).isEqualTo("user1@example.com"); assertThat((String) user.getAttribute("email")).isEqualTo("user1@example.com");
assertThat(user.getAuthorities().size()).isEqualTo(1); assertThat(user.getAuthorities().size()).isEqualTo(1);
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class); assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,6 +42,7 @@ import java.util.LinkedHashSet;
* and returning it from {@link #getName()}. * and returning it from {@link #getName()}.
* *
* @author Joe Grandja * @author Joe Grandja
* @author Eddú Meléndez
* @see OAuth2User * @see OAuth2User
* @since 5.0 * @since 5.0
*/ */
@ -72,7 +73,7 @@ public class DefaultOAuth2User implements OAuth2User, Serializable {
@Override @Override
public String getName() { public String getName() {
return this.getAttributes().get(this.nameAttributeKey).toString(); return this.getAttribute(this.nameAttributeKey).toString();
} }
@Override @Override

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,12 +15,8 @@
*/ */
package org.springframework.security.oauth2.core.user; package org.springframework.security.oauth2.core.user;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
import java.util.Collection;
import java.util.Map;
/** /**
* A representation of a user {@code Principal} * A representation of a user {@code Principal}
@ -37,29 +33,16 @@ import java.util.Map;
* Please consult the provider's API documentation for the set of supported user attribute names. * Please consult the provider's API documentation for the set of supported user attribute names.
* *
* <p> * <p>
* Implementation instances of this interface represent an {@link AuthenticatedPrincipal} * Implementation instances of this interface represent an {@link OAuth2AuthenticatedPrincipal}
* which is associated to an {@link Authentication} object * which is associated to an {@link Authentication} object
* and may be accessed via {@link Authentication#getPrincipal()}. * and may be accessed via {@link Authentication#getPrincipal()}.
* *
* @author Joe Grandja * @author Joe Grandja
* @author Eddú Meléndez
* @since 5.0 * @since 5.0
* @see DefaultOAuth2User * @see DefaultOAuth2User
* @see AuthenticatedPrincipal * @see OAuth2AuthenticatedPrincipal
*/ */
public interface OAuth2User extends AuthenticatedPrincipal { public interface OAuth2User extends OAuth2AuthenticatedPrincipal {
/**
* Returns the authorities granted to the user.
*
* @return a {@code Collection} of {@link GrantedAuthority}(s)
*/
Collection<? extends GrantedAuthority> getAuthorities();
/**
* Returns the attributes about the user.
*
* @return a {@code Map} of attributes about the user
*/
Map<String, Object> getAttributes();
} }