1

My Spring Boot authorization service is able to create JWT tokens for users with correct credentials. It shall also be possible to get a token via social login (Facebook in this case). The Facebook authentication already works and I find myself redirected to the following endpoint after I get the users's Facebook data.

I could create the JWT token completely from scratch, but that is not what I want. I want to use the already configured (with key pair and so on) TokenServices from my authentication server.

The only way I found was via TokenEndpoint. The problem is that I need the user's password, which I don't have and shouldn't have at this point.

How can I generate the token from what I already configured?

This endpoint is where I end up after Facebook's redirect:

@GetMapping("/loginSuccess")
fun getLoginInfo(authentication: OAuth2AuthenticationToken): ResponseEntity<OAuth2AccessToken> {

    val client = authorizedClientService.loadAuthorizedClient<OAuth2AuthorizedClient>(authentication.authorizedClientRegistrationId, authentication.name)

    val userInfoEndpointUri = client.clientRegistration.providerDetails.userInfoEndpoint.uri

    if (!StringUtils.isEmpty(userInfoEndpointUri)) {
        val restTemplate = RestTemplate()
        val headers = HttpHeaders()
        headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + client.accessToken.tokenValue)

        val entity = HttpEntity("", headers)
        val response = restTemplate.exchange(userInfoEndpointUri, HttpMethod.GET, entity, Map::class.java)

        // At this point I have the email address of the user and I am able to
        // map it to my own User Entity  
        // This is where I would like to create a token and return it  
        // However, the following generation process requires the user's password
        return authService.generateToken((response.body as Map<*, *>)["email"] as String)
    }

    throw AuthenticationException("Error")
}

I would like to use getAccessToken(OAuth2Authentication authentication) in JwtTokenStore but its implementation returns null:

@Override
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
   // We don't want to accidentally issue a token, and we have no way to 
   // reconstruct the refresh token
   return null;
}
finisinfinitatis
  • 861
  • 11
  • 23

0 Answers0