forked from spring-projects/spring-security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for nested username attribute in DefaultOAuth2User
Closes spring-projectsgh-14186 Signed-off-by: ahmd-nabil <ahm3dnabil99@gmail.com>
- Loading branch information
1 parent
63e726e
commit 91e2203
Showing
10 changed files
with
301 additions
and
53 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...va/org/springframework/security/oauth2/client/jackson2/DefaultOAuth2UserDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2002-2023 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.oauth2.client.jackson2; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
|
||
/** | ||
* A JsonDeserializer for {@link DefaultOAuth2User}. | ||
* | ||
* @author Ahmed Nabil | ||
* @since 6.3 | ||
* @see DefaultOAuth2User | ||
* @see DefaultOAuth2UserMixin | ||
*/ | ||
public class DefaultOAuth2UserDeserializer extends JsonDeserializer<DefaultOAuth2User> { | ||
|
||
@Override | ||
public DefaultOAuth2User deserialize(JsonParser parser, DeserializationContext context) | ||
throws IOException, JacksonException { | ||
ObjectMapper mapper = (ObjectMapper) parser.getCodec(); | ||
JsonNode defaultOAuth2UserNode = mapper.readTree(parser); | ||
Collection<? extends GrantedAuthority> authorities = JsonNodeUtils.findValue(defaultOAuth2UserNode, | ||
"authorities", JsonNodeUtils.GRANTED_AUTHORITY_COLLECTION, mapper); | ||
Map<String, Object> attributes = JsonNodeUtils.findValue(defaultOAuth2UserNode, "attributes", | ||
JsonNodeUtils.STRING_OBJECT_MAP, mapper); | ||
String name = JsonNodeUtils.findStringValue(defaultOAuth2UserNode, "name"); | ||
if (name != null) { | ||
return new DefaultOAuth2User(attributes, authorities, name); | ||
} | ||
String nameAttributeKey = JsonNodeUtils.findStringValue(defaultOAuth2UserNode, "nameAttributeKey"); | ||
return new DefaultOAuth2User(authorities, attributes, nameAttributeKey); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...java/org/springframework/security/oauth2/client/jackson2/DefaultOidcUserDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2002-2023 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.oauth2.client.jackson2; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.core.oidc.OidcIdToken; | ||
import org.springframework.security.oauth2.core.oidc.OidcUserInfo; | ||
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; | ||
|
||
/** | ||
* A JsonDeserializer for {@link DefaultOidcUser}. | ||
* | ||
* @author Ahmed Nabil | ||
* @since 6.3 | ||
* @see DefaultOidcUser | ||
* @see DefaultOidcUserMixin | ||
*/ | ||
public class DefaultOidcUserDeserializer extends JsonDeserializer<DefaultOidcUser> { | ||
|
||
@Override | ||
public DefaultOidcUser deserialize(JsonParser parser, DeserializationContext context) | ||
throws IOException, JacksonException { | ||
ObjectMapper mapper = (ObjectMapper) parser.getCodec(); | ||
JsonNode defaultOidcUserNode = mapper.readTree(parser); | ||
Collection<? extends GrantedAuthority> authorities = JsonNodeUtils.findValue(defaultOidcUserNode, "authorities", | ||
JsonNodeUtils.GRANTED_AUTHORITY_COLLECTION, mapper); | ||
OidcIdToken idToken = JsonNodeUtils.findValueByPath(defaultOidcUserNode, "idToken", OidcIdToken.class, mapper); | ||
OidcUserInfo userInfo = JsonNodeUtils.findValueByPath(defaultOidcUserNode, "userInfo", OidcUserInfo.class, | ||
mapper); | ||
String nameAttributeKey = JsonNodeUtils.findValueByPath(defaultOidcUserNode, "nameAttributeKey", String.class, | ||
mapper); | ||
String name = JsonNodeUtils.findValueByPath(defaultOidcUserNode, "name", String.class, mapper); | ||
return (name != null) ? new DefaultOidcUser(idToken, userInfo, authorities, name) | ||
: new DefaultOidcUser(authorities, idToken, userInfo, nameAttributeKey); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.