Skip to content

Commit

Permalink
Make OAuth2User extends OAuth2AuthenticatedPrincipal
Browse files Browse the repository at this point in the history
Fixes gh-7378
  • Loading branch information
eddumelendez authored and jzheaux committed Sep 9, 2019
1 parent aa533c2 commit 91bf1c7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -48,6 +48,7 @@
* Tests for {@link CustomUserTypesOAuth2UserService}.
*
* @author Joe Grandja
* @author Eddú Meléndez
*/
public class CustomUserTypesOAuth2UserServiceTests {
private ClientRegistration.Builder clientRegistrationBuilder;
Expand Down Expand Up @@ -134,10 +135,10 @@ public void loadUserWhenUserInfoSuccessResponseThenReturnUser() {

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("[email protected]");
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("[email protected]");

assertThat(user.getAuthorities().size()).isEqualTo(1);
assertThat(user.getAuthorities().iterator().next().getAuthority()).isEqualTo("ROLE_USER");
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -61,6 +61,7 @@
* Tests for {@link DefaultOAuth2UserService}.
*
* @author Joe Grandja
* @author Eddú Meléndez
*/
public class DefaultOAuth2UserServiceTests {
private ClientRegistration.Builder clientRegistrationBuilder;
Expand Down Expand Up @@ -146,12 +147,12 @@ public void loadUserWhenUserInfoSuccessResponseThenReturnUser() {

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("[email protected]");
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("[email protected]");

assertThat(user.getAuthorities().size()).isEqualTo(1);
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -61,6 +61,7 @@

/**
* @author Rob Winch
* @author Eddú Meléndez
* @since 5.1
*/
public class DefaultReactiveOAuth2UserServiceTests {
Expand Down Expand Up @@ -137,12 +138,12 @@ public void loadUserWhenUserInfoSuccessResponseThenReturnUser() throws Exception

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("[email protected]");
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("[email protected]");

assertThat(user.getAuthorities().size()).isEqualTo(1);
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -42,6 +42,7 @@
* and returning it from {@link #getName()}.
*
* @author Joe Grandja
* @author Eddú Meléndez
* @see OAuth2User
* @since 5.0
*/
Expand Down Expand Up @@ -72,7 +73,7 @@ public DefaultOAuth2User(Collection<? extends GrantedAuthority> authorities, Map

@Override
public String getName() {
return this.getAttributes().get(this.nameAttributeKey).toString();
return this.getAttribute(this.nameAttributeKey).toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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}
Expand All @@ -37,29 +33,16 @@
* Please consult the provider's API documentation for the set of supported user attribute names.
*
* <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
* 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<? extends GrantedAuthority> getAuthorities();

/**
* Returns the attributes about the user.
*
* @return a {@code Map} of attributes about the user
*/
Map<String, Object> getAttributes();
public interface OAuth2User extends OAuth2AuthenticatedPrincipal {

}

0 comments on commit 91bf1c7

Please sign in to comment.