Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TokenProvider customizable #669

Open
5 tasks done
salbracco24 opened this issue Oct 1, 2024 · 2 comments
Open
5 tasks done

Make TokenProvider customizable #669

salbracco24 opened this issue Oct 1, 2024 · 2 comments
Labels
feature request A feature has been asked for or suggested by the community

Comments

@salbracco24
Copy link

salbracco24 commented Oct 1, 2024

Checklist

  • I have looked into the Readme and Examples, and have not found a suitable solution or answer.
  • I have looked into the API documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • I agree to the terms within the Auth0 Code of Conduct.

Describe the problem you'd like to have solved

I want to provide custom logic to retrieve the token used to access the Auth0 Management API. Yes, I can do this by calling "setApiToken()" but that's cumbersome. I would like to implement my own token provider by implementing TokenProvider, a public interface.

Describe the ideal solution

Add a method to the builder or add a constructor for the ManagementApi class (in the com.auth0.client.mgmt package), so that TokenProvider can be changed.

You can even just make the constructor on line 35 public, but I'd prefer a new constructor, as I don't need to specify the HTTP client.

You can literally fix this issue in 10 minutes or less.

Alternatives and current workarounds

No workarounds, except reflection, which is a hack.

Additional context

Please fix this quickly, thanks.

@salbracco24 salbracco24 added the feature request A feature has been asked for or suggested by the community label Oct 1, 2024
@unkali
Copy link

unkali commented Nov 29, 2024

Would like to add that my team is also waiting for this change. We are using reflection for the time being

@salbracco24
Copy link
Author

Nice, I used Method Handles, which is better than reflection, @unkali:

  /**
   * Used to access a private constructor for the Auth0 {@link ManagementAPI}, so that we can use our custom
   * {@link TokenProvider}. <br> Can be removed if
   * <a href="https://github.com/auth0/auth0-java/issues/669">Auth0 Java Repo - Issue #669</a> is addressed.
   */
  private static final MethodHandle MANAGEMENT_API_CONSTRUCTOR_HANDLE;

  static {
    try {
      MethodHandles.Lookup lookup = MethodHandles.lookup();
      MethodHandles.Lookup targetLookup = MethodHandles.privateLookupIn(ManagementAPI.class, lookup);
      MethodType methodType =
        MethodType.methodType(void.class, String.class, TokenProvider.class, Auth0HttpClient.class);
      MANAGEMENT_API_CONSTRUCTOR_HANDLE = targetLookup.findConstructor(ManagementAPI.class, methodType);
    } catch (NoSuchMethodException | IllegalAccessException e) {
      throw new IllegalStateException("Failed to initialize MANAGEMENT_API_CONSTRUCTOR_HANDLE", e);
    }
  }

and then you call it like this:

try {
  return (ManagementAPI) MANAGEMENT_API_CONSTRUCTOR_HANDLE.invoke(domain, tokenProvider, DefaultHttpClient.newBuilder().build());
} catch (Throwable e) {
  throw new IllegalStateException("Failed to construct the Auth0 ManagementAPI", e);
}

But hopefully, Auth0 fixes this soon 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request A feature has been asked for or suggested by the community
Projects
None yet
Development

No branches or pull requests

2 participants