javaspring-securitypac4j

How to upgrade from version 2.31 to version 5.3.1 of pac4j-http and fix the arguments to org.pac4j.core.credentials.authenticator.Authenticator?


I am trying to upgrade pac4j-http:2.31 to 5.3.1 and need help dealing with a breaking change in the argument list of the validate() function in the Authenticator interface.

Earlier I was using Authenticator(org.pac4j.core.credentials.authenticator.Authenticator) which had method public void validate(TokenCredentials credentials, WebContext context) and now it changed to public void validate(Credentials credentials, WebContext context, SessionStore sessionStore)

Earlier code below:

private static class TestAuthenticator implements Authenticator<TokenCredentials>
  {
    @Override
    public void validate(TokenCredentials credentials, WebContext context)
    {
      if (TEST_TOKEN.equals(credentials.getToken()))
      {
        credentials.setUserProfile(mockUserProfile());
      }
    }
  }

So my question is, how to upgrade to this latest version of 5.3.1. where the validate method of Authenticator class has Credential argument instead of TokenCredential? How can we validate token now since the validate method arguments changed?


Solution

  • Since Credentials is a super-class of TokenCredentials, it seems like you'd be able to do:

    private static class TestAuthenticator implements Authenticator {
      @Override
      public void validate(Credentials credentials, WebContext context, SessionStore sessionStore) {
        TokenCredentials tokenCredentials = (TokenCredentials) credentials;
        if (TEST_TOKEN.equals(tokenCredentials.getToken())) {
          tokenCredentials.setUserProfile(mockUserProfile());
        }
      }
    }
    

    Apologies, though, if I'm missing something. It seems like the generic type was removed from Authenticator, meaning that a cast is now required.