Spring Boot as a resource server (JWK validation) & Angular/Cordova as front end - OAuth2 for social Login (Facebook & Google) support

Viewed 839

I am stuck with the implementation of spring boot as a resource server for multiple authorization servers for validating the access/id tokens provided by authorization servers (such as google, facebook via front end libraries). Here is the architecture I am looking for the below desired flow as a working model.

Desired Architecture Image - Click here

what I implemented so far: I used a library angularx-social-login on angular 8 to get the required tokens google. I was able to pass the token back to the backend resource server to validate the token with google and authorize. Below is the code snippets.

Property File:

google:
  client-id: xyz
  iss: accounts.google.com

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: https://accounts.google.com
          jwk-set-uri: https://accounts.google.com/.well-known/openid-configuration

Security Config Snippet

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}")
private String issuer;

@Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}")
private String jwkSetUri;

@Value("${google.client-id}")
private String clientId;

@Value("${google.iss}")
private String iss;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
        .authorizeRequests()
        .anyRequest().authenticated().and()
        .oauth2ResourceServer()
        .jwt().decoder(jwtDecoder());
}

@Bean
JwtDecoder jwtDecoder() {
    NimbusJwtDecoder jwtDecoder = (NimbusJwtDecoder)
            JwtDecoders.fromOidcIssuerLocation(issuer);        

    OAuth2TokenValidator<Jwt> audienceValidator = new AudienceValidator(clientId);
    OAuth2TokenValidator<Jwt> withIssuer = JwtValidators.createDefaultWithIssuer(iss);
    OAuth2TokenValidator<Jwt> withAudience = new DelegatingOAuth2TokenValidator<> 
    (withIssuer, audienceValidator);

    jwtDecoder.setJwtValidator(withAudience);

    return jwtDecoder;
}

}

The above snippet works for one authorization server (google in this case) but

below are my issues

  1. How do Intercept the code to validate if the user exists in our DB first?
  2. How do I add support for another authorization server like facebook?
0 Answers
Related