Skip to content

Commit

Permalink
use GrailsUserDetailsService get authorities. (#25)
Browse files Browse the repository at this point in the history
* use GrailsUserDetailsService get authorities.

* Update SpringSecurityOauth2BaseService.groovy

Remove commented code

---------

Co-authored-by: chenmin25 <[email protected]>
Co-authored-by: Puneet Behl <[email protected]>
  • Loading branch information
3 people committed Jan 19, 2024
1 parent 1975437 commit f0db049
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,29 @@ import grails.plugin.springsecurity.oauth2.token.OAuth2SpringToken
import grails.plugin.springsecurity.oauth2.util.OAuth2ProviderConfiguration
import grails.plugin.springsecurity.userdetails.GormUserDetailsService
import grails.plugin.springsecurity.userdetails.GrailsUser
import grails.plugin.springsecurity.userdetails.GrailsUserDetailsService
import groovy.util.logging.Slf4j
import org.apache.commons.lang.exception.ExceptionUtils
import org.springframework.security.authentication.AuthenticationManager
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.AuthenticationException
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails

@Transactional
@Slf4j
class SpringSecurityOauth2BaseService {

/**
* Map for storing the different OAuth2Provider
*/
/**
* Map for storing the different OAuth2Provider
*/
Map<String, OAuth2AbstractProviderService> providerServiceMap = new HashMap<>()
private Map<String, OAuth2ProviderConfiguration> _providerConfigurationMap = new HashMap<>()

GrailsApplication grailsApplication
AuthenticationManager authenticationManager

GrailsUserDetailsService userDetailsService

OAuth2SpringToken createAuthToken(String providerName, OAuth2AccessToken scribeToken) {
def providerService = getProviderService(providerName)
OAuth2SpringToken oAuthToken = providerService.createSpringAuthToken(scribeToken)
Expand Down Expand Up @@ -108,11 +111,8 @@ class SpringSecurityOauth2BaseService {
boolean passwordExpired = passwordExpiredPropertyName ? user."${passwordExpiredPropertyName}" : false

// authorities

String authoritiesPropertyName = conf.userLookup.authoritiesPropertyName
String authorityPropertyName = conf.authority.nameField
Collection<?> userAuthorities = user."${authoritiesPropertyName}"
def authorities = userAuthorities.collect { new SimpleGrantedAuthority(it."${authorityPropertyName}") }
UserDetails userDetails = userDetailsService.loadUserByUsername(username,true)
def authorities= userDetails.authorities

oAuthToken.principal = new GrailsUser(username, password, enabled, !accountExpired, !passwordExpired,
!accountLocked, authorities ?: [GormUserDetailsService.NO_ROLE], user.id)
Expand Down

1 comment on commit f0db049

@tircnf
Copy link

@tircnf tircnf commented on f0db049 Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm 9 months late to the party, but it looks like most of this method can disappear, and the oAuthToken.principal should just be set to the userDetails you got back from the service.

OAuth2SpringToken updateOAuthToken(OAuth2SpringToken oAuthToken, user) {
    

    // authorities
    UserDetails userDetails = userDetailsService.loadUserByUsername(username,true)
    

    oAuthToken.principal = userDetails
    oAuthToken.authorities = userDetails.authorities
    oAuthToken.authenticated = true

    return oAuthToken
}

Please sign in to comment.