Skip to content

Commit 91bf1c7

Browse files
eddumelendezjzheaux
authored andcommitted
Make OAuth2User extends OAuth2AuthenticatedPrincipal
Fixes gh-7378
1 parent aa533c2 commit 91bf1c7

File tree

5 files changed

+31
-44
lines changed

5 files changed

+31
-44
lines changed

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/CustomUserTypesOAuth2UserServiceTests.java

+6-5
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-2019 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.
@@ -48,6 +48,7 @@
4848
* Tests for {@link CustomUserTypesOAuth2UserService}.
4949
*
5050
* @author Joe Grandja
51+
* @author Eddú Meléndez
5152
*/
5253
public class CustomUserTypesOAuth2UserServiceTests {
5354
private ClientRegistration.Builder clientRegistrationBuilder;
@@ -134,10 +135,10 @@ public void loadUserWhenUserInfoSuccessResponseThenReturnUser() {
134135

135136
assertThat(user.getName()).isEqualTo("first last");
136137
assertThat(user.getAttributes().size()).isEqualTo(4);
137-
assertThat(user.getAttributes().get("id")).isEqualTo("12345");
138-
assertThat(user.getAttributes().get("name")).isEqualTo("first last");
139-
assertThat(user.getAttributes().get("login")).isEqualTo("user1");
140-
assertThat(user.getAttributes().get("email")).isEqualTo("[email protected]");
138+
assertThat((String) user.getAttribute("id")).isEqualTo("12345");
139+
assertThat((String) user.getAttribute("name")).isEqualTo("first last");
140+
assertThat((String) user.getAttribute("login")).isEqualTo("user1");
141+
assertThat((String) user.getAttribute("email")).isEqualTo("[email protected]");
141142

142143
assertThat(user.getAuthorities().size()).isEqualTo(1);
143144
assertThat(user.getAuthorities().iterator().next().getAuthority()).isEqualTo("ROLE_USER");

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultOAuth2UserServiceTests.java

+8-7
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-2019 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.
@@ -61,6 +61,7 @@
6161
* Tests for {@link DefaultOAuth2UserService}.
6262
*
6363
* @author Joe Grandja
64+
* @author Eddú Meléndez
6465
*/
6566
public class DefaultOAuth2UserServiceTests {
6667
private ClientRegistration.Builder clientRegistrationBuilder;
@@ -146,12 +147,12 @@ public void loadUserWhenUserInfoSuccessResponseThenReturnUser() {
146147

147148
assertThat(user.getName()).isEqualTo("user1");
148149
assertThat(user.getAttributes().size()).isEqualTo(6);
149-
assertThat(user.getAttributes().get("user-name")).isEqualTo("user1");
150-
assertThat(user.getAttributes().get("first-name")).isEqualTo("first");
151-
assertThat(user.getAttributes().get("last-name")).isEqualTo("last");
152-
assertThat(user.getAttributes().get("middle-name")).isEqualTo("middle");
153-
assertThat(user.getAttributes().get("address")).isEqualTo("address");
154-
assertThat(user.getAttributes().get("email")).isEqualTo("[email protected]");
150+
assertThat((String) user.getAttribute("user-name")).isEqualTo("user1");
151+
assertThat((String) user.getAttribute("first-name")).isEqualTo("first");
152+
assertThat((String) user.getAttribute("last-name")).isEqualTo("last");
153+
assertThat((String) user.getAttribute("middle-name")).isEqualTo("middle");
154+
assertThat((String) user.getAttribute("address")).isEqualTo("address");
155+
assertThat((String) user.getAttribute("email")).isEqualTo("[email protected]");
155156

156157
assertThat(user.getAuthorities().size()).isEqualTo(1);
157158
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class);

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultReactiveOAuth2UserServiceTests.java

+8-7
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-2019 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.
@@ -61,6 +61,7 @@
6161

6262
/**
6363
* @author Rob Winch
64+
* @author Eddú Meléndez
6465
* @since 5.1
6566
*/
6667
public class DefaultReactiveOAuth2UserServiceTests {
@@ -137,12 +138,12 @@ public void loadUserWhenUserInfoSuccessResponseThenReturnUser() throws Exception
137138

138139
assertThat(user.getName()).isEqualTo("user1");
139140
assertThat(user.getAttributes().size()).isEqualTo(6);
140-
assertThat(user.getAttributes().get("id")).isEqualTo("user1");
141-
assertThat(user.getAttributes().get("first-name")).isEqualTo("first");
142-
assertThat(user.getAttributes().get("last-name")).isEqualTo("last");
143-
assertThat(user.getAttributes().get("middle-name")).isEqualTo("middle");
144-
assertThat(user.getAttributes().get("address")).isEqualTo("address");
145-
assertThat(user.getAttributes().get("email")).isEqualTo("[email protected]");
141+
assertThat((String) user.getAttribute("id")).isEqualTo("user1");
142+
assertThat((String) user.getAttribute("first-name")).isEqualTo("first");
143+
assertThat((String) user.getAttribute("last-name")).isEqualTo("last");
144+
assertThat((String) user.getAttribute("middle-name")).isEqualTo("middle");
145+
assertThat((String) user.getAttribute("address")).isEqualTo("address");
146+
assertThat((String) user.getAttribute("email")).isEqualTo("[email protected]");
146147

147148
assertThat(user.getAuthorities().size()).isEqualTo(1);
148149
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class);

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/DefaultOAuth2User.java

+3-2
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-2019 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.
@@ -42,6 +42,7 @@
4242
* and returning it from {@link #getName()}.
4343
*
4444
* @author Joe Grandja
45+
* @author Eddú Meléndez
4546
* @see OAuth2User
4647
* @since 5.0
4748
*/
@@ -72,7 +73,7 @@ public DefaultOAuth2User(Collection<? extends GrantedAuthority> authorities, Map
7273

7374
@Override
7475
public String getName() {
75-
return this.getAttributes().get(this.nameAttributeKey).toString();
76+
return this.getAttribute(this.nameAttributeKey).toString();
7677
}
7778

7879
@Override
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -15,12 +15,8 @@
1515
*/
1616
package org.springframework.security.oauth2.core.user;
1717

18-
import org.springframework.security.core.AuthenticatedPrincipal;
1918
import org.springframework.security.core.Authentication;
20-
import org.springframework.security.core.GrantedAuthority;
21-
22-
import java.util.Collection;
23-
import java.util.Map;
19+
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
2420

2521
/**
2622
* A representation of a user {@code Principal}
@@ -37,29 +33,16 @@
3733
* Please consult the provider's API documentation for the set of supported user attribute names.
3834
*
3935
* <p>
40-
* Implementation instances of this interface represent an {@link AuthenticatedPrincipal}
36+
* Implementation instances of this interface represent an {@link OAuth2AuthenticatedPrincipal}
4137
* which is associated to an {@link Authentication} object
4238
* and may be accessed via {@link Authentication#getPrincipal()}.
4339
*
4440
* @author Joe Grandja
41+
* @author Eddú Meléndez
4542
* @since 5.0
4643
* @see DefaultOAuth2User
47-
* @see AuthenticatedPrincipal
44+
* @see OAuth2AuthenticatedPrincipal
4845
*/
49-
public interface OAuth2User extends AuthenticatedPrincipal {
50-
51-
/**
52-
* Returns the authorities granted to the user.
53-
*
54-
* @return a {@code Collection} of {@link GrantedAuthority}(s)
55-
*/
56-
Collection<? extends GrantedAuthority> getAuthorities();
57-
58-
/**
59-
* Returns the attributes about the user.
60-
*
61-
* @return a {@code Map} of attributes about the user
62-
*/
63-
Map<String, Object> getAttributes();
46+
public interface OAuth2User extends OAuth2AuthenticatedPrincipal {
6447

6548
}

0 commit comments

Comments
 (0)