-
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.
- Loading branch information
Showing
7 changed files
with
93 additions
and
47 deletions.
There are no files selected for viewing
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
72 changes: 72 additions & 0 deletions
72
...src/main/java/club/klabis/config/authserver/socialloginsupport/CustomOidcUserService.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,72 @@ | ||
package club.klabis.config.authserver.socialloginsupport; | ||
|
||
import club.klabis.config.authserver.KlabisOidcUser; | ||
import club.klabis.domain.appusers.ApplicationUser; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest; | ||
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserService; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistration; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.oidc.OidcIdToken; | ||
import org.springframework.security.oauth2.core.oidc.OidcUserInfo; | ||
import org.springframework.security.oauth2.core.oidc.StandardClaimNames; | ||
import org.springframework.security.oauth2.core.oidc.user.OidcUser; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.*; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
class CustomOidcUserService extends OidcUserService { | ||
private final Map<String, SocialLoginOidcUserToKlabisOidcUserMapper> mappers; | ||
|
||
public CustomOidcUserService(List<SocialLoginOidcUserToKlabisOidcUserMapper> mappers) { | ||
this.mappers = mappers.stream().collect(Collectors.toMap(SocialLoginOidcUserToKlabisOidcUserMapper::getOAuthClientId, Function.identity())); | ||
} | ||
|
||
private Optional<SocialLoginOidcUserToKlabisOidcUserMapper> getMapperForRegistrationId(ClientRegistration registration) { | ||
return mappers.values().stream().filter(it -> registration.getRegistrationId().equals(it.getOAuthClientId())).findAny(); | ||
} | ||
|
||
@Override | ||
public KlabisOidcUser loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException { | ||
OidcUser oidcUser = super.loadUser(userRequest); | ||
|
||
SocialLoginOidcUserToKlabisOidcUserMapper mapper = getMapperForRegistrationId(userRequest.getClientRegistration()) | ||
.orElseThrow(() -> new RuntimeException("No OIDC mapper defined for registrationId %s".formatted(userRequest.getClientRegistration().getRegistrationId()))); | ||
|
||
return mapper.findApplicationUserForToken(userRequest.getIdToken()) | ||
.map(applicationUser -> createAuthentication(oidcUser.getIdToken(), oidcUser.getUserInfo(), applicationUser, List.of())) | ||
.orElseThrow(() -> new OAuth2AuthenticationException("User with subject %s (%s) not found!".formatted(oidcUser.getSubject(), mapper.getOAuthClientId()))); | ||
} | ||
|
||
KlabisOidcUser createAuthentication(OidcIdToken idToken, OidcUserInfo userInfo, ApplicationUser user, List<String> roles) { | ||
Set<GrantedAuthority> authorities = roles.stream() | ||
.map(roleName -> new SimpleGrantedAuthority(roleName)) | ||
.collect(Collectors.toSet()); | ||
|
||
Map<String, Object> klabisClaims = new HashMap<>(); | ||
//claims.putAll(idToken.getClaims()); | ||
klabisClaims.put(StandardClaimNames.SUB, user.getUsername()); | ||
klabisClaims.put("memberId", user.getMemberId().orElse(null)); | ||
// claims.put(StandardClaimNames.GIVEN_NAME, user.getFirstName()); | ||
// claims.put(StandardClaimNames.MIDDLE_NAME, user.getMiddleName()); | ||
// claims.put(StandardClaimNames.FAMILY_NAME, user.getLastName()); | ||
// claims.put(StandardClaimNames.LOCALE, user.getLocale()); | ||
// claims.put(StandardClaimNames.PICTURE, user.getAvatarUrl()); | ||
|
||
OidcIdToken customIdToken = new OidcIdToken( | ||
idToken.getTokenValue(), idToken.getIssuedAt(), idToken.getExpiresAt(), klabisClaims | ||
); | ||
|
||
KlabisOidcUser oidcUser = new KlabisOidcUser(authorities, customIdToken, userInfo); | ||
// oidcUser.setId(user.getId()); | ||
// oidcUser.setUsername(user.getUsername()); | ||
// oidcUser.setCreatedAt(user.getCreatedAt()); | ||
// oidcUser.setActive(user.isActive()); | ||
return oidcUser; | ||
}; | ||
|
||
} |
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
42 changes: 3 additions & 39 deletions
42
...labis/config/authserver/socialloginsupport/SocialLoginOidcUserToKlabisOidcUserMapper.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 |
---|---|---|
@@ -1,52 +1,16 @@ | ||
package club.klabis.config.authserver.socialloginsupport; | ||
|
||
import club.klabis.config.authserver.KlabisOidcUser; | ||
import club.klabis.domain.appusers.ApplicationUser; | ||
import club.klabis.domain.appusers.ApplicationUsersRepository; | ||
import club.klabis.domain.members.Member; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.core.oidc.OidcIdToken; | ||
import org.springframework.security.oauth2.core.oidc.OidcUserInfo; | ||
import org.springframework.security.oauth2.core.oidc.StandardClaimNames; | ||
|
||
import java.util.*; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.Optional; | ||
|
||
public interface SocialLoginOidcUserToKlabisOidcUserMapper { | ||
/** | ||
* Returns registration ID for which is mapper supposed to be used | ||
*/ | ||
String getRegistration(); | ||
String getOAuthClientId(); | ||
|
||
Function<String, Optional<ApplicationUser>> findMemberFunction(ApplicationUsersRepository memberService); | ||
|
||
default KlabisOidcUser map(OidcIdToken idToken, OidcUserInfo userInfo, ApplicationUser user, List<String> roles) { | ||
Set<GrantedAuthority> authorities = roles.stream() | ||
.map(roleName -> new SimpleGrantedAuthority(roleName)) | ||
.collect(Collectors.toSet()); | ||
|
||
Map<String, Object> klabisClaims = new HashMap<>(); | ||
//claims.putAll(idToken.getClaims()); | ||
klabisClaims.put(StandardClaimNames.SUB, user.getUsername()); | ||
klabisClaims.put("memberId", user.getMemberId().orElse(null)); | ||
// claims.put(StandardClaimNames.GIVEN_NAME, user.getFirstName()); | ||
// claims.put(StandardClaimNames.MIDDLE_NAME, user.getMiddleName()); | ||
// claims.put(StandardClaimNames.FAMILY_NAME, user.getLastName()); | ||
// claims.put(StandardClaimNames.LOCALE, user.getLocale()); | ||
// claims.put(StandardClaimNames.PICTURE, user.getAvatarUrl()); | ||
|
||
OidcIdToken customIdToken = new OidcIdToken( | ||
idToken.getTokenValue(), idToken.getIssuedAt(), idToken.getExpiresAt(), klabisClaims | ||
); | ||
|
||
KlabisOidcUser oidcUser = new KlabisOidcUser(authorities, customIdToken, userInfo); | ||
// oidcUser.setId(user.getId()); | ||
// oidcUser.setUsername(user.getUsername()); | ||
// oidcUser.setCreatedAt(user.getCreatedAt()); | ||
// oidcUser.setActive(user.isActive()); | ||
return oidcUser; | ||
}; | ||
Optional<ApplicationUser> findApplicationUserForToken(OidcIdToken token); | ||
|
||
} |