From 91bf1c782a32f343232d8fc38145930a3c7de55e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Thu, 5 Sep 2019 19:04:39 -0500 Subject: [PATCH] Make OAuth2User extends OAuth2AuthenticatedPrincipal Fixes gh-7378 --- ...CustomUserTypesOAuth2UserServiceTests.java | 11 +++---- .../DefaultOAuth2UserServiceTests.java | 15 +++++----- ...DefaultReactiveOAuth2UserServiceTests.java | 15 +++++----- .../oauth2/core/user/DefaultOAuth2User.java | 5 ++-- .../security/oauth2/core/user/OAuth2User.java | 29 ++++--------------- 5 files changed, 31 insertions(+), 44 deletions(-) diff --git a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/CustomUserTypesOAuth2UserServiceTests.java b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/CustomUserTypesOAuth2UserServiceTests.java index 3f1a9bfaad..3305a1ebd9 100644 --- a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/CustomUserTypesOAuth2UserServiceTests.java +++ b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/CustomUserTypesOAuth2UserServiceTests.java @@ -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"); * 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}. * * @author Joe Grandja + * @author Eddú Meléndez */ public class CustomUserTypesOAuth2UserServiceTests { private ClientRegistration.Builder clientRegistrationBuilder; @@ -134,10 +135,10 @@ public class CustomUserTypesOAuth2UserServiceTests { assertThat(user.getName()).isEqualTo("first last"); assertThat(user.getAttributes().size()).isEqualTo(4); - assertThat(user.getAttributes().get("id")).isEqualTo("12345"); - assertThat(user.getAttributes().get("name")).isEqualTo("first last"); - assertThat(user.getAttributes().get("login")).isEqualTo("user1"); - assertThat(user.getAttributes().get("email")).isEqualTo("user1@example.com"); + assertThat((String) user.getAttribute("id")).isEqualTo("12345"); + assertThat((String) user.getAttribute("name")).isEqualTo("first last"); + assertThat((String) user.getAttribute("login")).isEqualTo("user1"); + assertThat((String) user.getAttribute("email")).isEqualTo("user1@example.com"); assertThat(user.getAuthorities().size()).isEqualTo(1); assertThat(user.getAuthorities().iterator().next().getAuthority()).isEqualTo("ROLE_USER"); diff --git a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultOAuth2UserServiceTests.java b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultOAuth2UserServiceTests.java index f42e167b83..f5b4202a39 100644 --- a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultOAuth2UserServiceTests.java +++ b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultOAuth2UserServiceTests.java @@ -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"); * 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}. * * @author Joe Grandja + * @author Eddú Meléndez */ public class DefaultOAuth2UserServiceTests { private ClientRegistration.Builder clientRegistrationBuilder; @@ -146,12 +147,12 @@ public class DefaultOAuth2UserServiceTests { assertThat(user.getName()).isEqualTo("user1"); assertThat(user.getAttributes().size()).isEqualTo(6); - assertThat(user.getAttributes().get("user-name")).isEqualTo("user1"); - assertThat(user.getAttributes().get("first-name")).isEqualTo("first"); - assertThat(user.getAttributes().get("last-name")).isEqualTo("last"); - assertThat(user.getAttributes().get("middle-name")).isEqualTo("middle"); - assertThat(user.getAttributes().get("address")).isEqualTo("address"); - assertThat(user.getAttributes().get("email")).isEqualTo("user1@example.com"); + assertThat((String) user.getAttribute("user-name")).isEqualTo("user1"); + assertThat((String) user.getAttribute("first-name")).isEqualTo("first"); + assertThat((String) user.getAttribute("last-name")).isEqualTo("last"); + assertThat((String) user.getAttribute("middle-name")).isEqualTo("middle"); + assertThat((String) user.getAttribute("address")).isEqualTo("address"); + assertThat((String) user.getAttribute("email")).isEqualTo("user1@example.com"); assertThat(user.getAuthorities().size()).isEqualTo(1); assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class); diff --git a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultReactiveOAuth2UserServiceTests.java b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultReactiveOAuth2UserServiceTests.java index 8d1725b37c..7b3d43b67d 100644 --- a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultReactiveOAuth2UserServiceTests.java +++ b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultReactiveOAuth2UserServiceTests.java @@ -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"); * 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 Eddú Meléndez * @since 5.1 */ public class DefaultReactiveOAuth2UserServiceTests { @@ -137,12 +138,12 @@ public class DefaultReactiveOAuth2UserServiceTests { assertThat(user.getName()).isEqualTo("user1"); assertThat(user.getAttributes().size()).isEqualTo(6); - assertThat(user.getAttributes().get("id")).isEqualTo("user1"); - assertThat(user.getAttributes().get("first-name")).isEqualTo("first"); - assertThat(user.getAttributes().get("last-name")).isEqualTo("last"); - assertThat(user.getAttributes().get("middle-name")).isEqualTo("middle"); - assertThat(user.getAttributes().get("address")).isEqualTo("address"); - assertThat(user.getAttributes().get("email")).isEqualTo("user1@example.com"); + assertThat((String) user.getAttribute("id")).isEqualTo("user1"); + assertThat((String) user.getAttribute("first-name")).isEqualTo("first"); + assertThat((String) user.getAttribute("last-name")).isEqualTo("last"); + assertThat((String) user.getAttribute("middle-name")).isEqualTo("middle"); + assertThat((String) user.getAttribute("address")).isEqualTo("address"); + assertThat((String) user.getAttribute("email")).isEqualTo("user1@example.com"); assertThat(user.getAuthorities().size()).isEqualTo(1); assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class); diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/DefaultOAuth2User.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/DefaultOAuth2User.java index 28e115b870..7657d6b1b7 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/DefaultOAuth2User.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/DefaultOAuth2User.java @@ -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"); * 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()}. * * @author Joe Grandja + * @author Eddú Meléndez * @see OAuth2User * @since 5.0 */ @@ -72,7 +73,7 @@ public class DefaultOAuth2User implements OAuth2User, Serializable { @Override public String getName() { - return this.getAttributes().get(this.nameAttributeKey).toString(); + return this.getAttribute(this.nameAttributeKey).toString(); } @Override diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/OAuth2User.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/OAuth2User.java index e31498cf61..f25db96c01 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/OAuth2User.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/OAuth2User.java @@ -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"); * you may not use this file except in compliance with the License. @@ -15,12 +15,8 @@ */ package org.springframework.security.oauth2.core.user; -import org.springframework.security.core.AuthenticatedPrincipal; import org.springframework.security.core.Authentication; -import org.springframework.security.core.GrantedAuthority; - -import java.util.Collection; -import java.util.Map; +import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal; /** * 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. * *

- * 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 * and may be accessed via {@link Authentication#getPrincipal()}. * * @author Joe Grandja + * @author Eddú Meléndez * @since 5.0 * @see DefaultOAuth2User - * @see AuthenticatedPrincipal + * @see OAuth2AuthenticatedPrincipal */ -public interface OAuth2User extends AuthenticatedPrincipal { - - /** - * Returns the authorities granted to the user. - * - * @return a {@code Collection} of {@link GrantedAuthority}(s) - */ - Collection getAuthorities(); - - /** - * Returns the attributes about the user. - * - * @return a {@code Map} of attributes about the user - */ - Map getAttributes(); +public interface OAuth2User extends OAuth2AuthenticatedPrincipal { }