micronaut-projects / micronaut-security

The official Micronaut security solution
Apache License 2.0
171 stars 128 forks source link

Custom OpenIdAuthenticationMapper not working 3.5.x #1043

Open Kushan2021 opened 2 years ago

Kushan2021 commented 2 years ago

Expected Behavior

Micronaut version 3.4.1

Custom class created by using implement OpenIdAuthenticationMapper and it calls createAuthenticationResponse.

   `@Singleton
    @Named("keycloak")
   public class CustomOpenIdAuthenticationMapper implements OpenIdAuthenticationMapper {

    @Override
    public AuthenticationResponse createAuthenticationResponse(String providerName,  OpenIdTokenResponse 
    tokenResponse,OpenIdClaims openIdClaims, @Nullable State state) {   

   HashMap<String, Object> attrs = new HashMap<>();
   attrs.put(OpenIdAuthenticationMapper.OPENID_TOKEN_KEY, tokenResponse.getIdToken());
   attrs.put(OauthAuthenticationMapper.ACCESS_TOKEN_KEY, tokenResponse.getAccessToken());
       attrs.put(OauthAuthenticationMapper.REFRESH_TOKEN_KEY, tokenResponse.getRefreshToken());
       attrs.put(OauthAuthenticationMapper.PROVIDER_KEY, providerName);
       attrs.put("email", openIdClaims.getEmail());

       return AuthenticationResponse.success(openIdClaims.getEmail(), attrs);   
  }
}`

Actual Behaviour

Micronaut version 3.5.0

Custome class createAuthenticationResponse method not triggered.

Environment Information

Version

3.5.0

sdelamo commented 2 years ago

Do you have a sample app?

sdelamo commented 2 years ago

Have you tried adding @Replaces(DefaultOpenIdAuthenticationMapper.class)if you want to replace the global mapper? If not, you will need to add a @Named qualifier.

https://micronaut-projects.github.io/micronaut-security/latest/guide/#openid-user-details`

Kushan2021 commented 2 years ago

Yes I already used @Named qualifier, but forgot to add on sample code.

This is my application.yml.

  `micronaut:     
          security:
              oauth2:
                   enabled: true
                   clients:
                       keycloak: 
                            enabled: true
                            grant-type: password
                            client-id: <<my client id>> 
                            client-secret: <<my client secret>>  
                            openid:
                                issuer: <<my openid issuer>> 
                                configuration-path: /.well-known/openid-configuration
                                jwks-uri: << my jwks uri >>
                                token:
                                     auth-method: client-secret-post`
Kushan2021 commented 2 years ago

Application works fine after upgraded into version 3.5.0 and downgraded this dependency.

    `<dependency>
        <groupId>io.micronaut.security</groupId>
        <artifactId>micronaut-security-oauth2</artifactId>
        <version>3.4.1</version>
        <scope>compile</scope>
    </dependency>`
Kushan2021 commented 2 years ago

Once I downgrade in version 3.4.1 io.micronaut.security.authentication.Authenticator variable authenticationProviders contains OpenIdPasswordAuthenticationProvider. But new version authenticationProviders contains empty array.