Skip to content

Conversation

ttstarck
Copy link

@ttstarck ttstarck commented Sep 17, 2025

Changes

Code Changes

Make Auth0::Client#get_token public.

Why

Currently if I want to generate an API token for an API using the Auth0::Client, I must do the following:

 # This makes a request to `/oauth/token`
client = Auth0::Client.new(api_identifier: "my-api-identifier", client_id: "my-client-id", client_secret: "my-client-secret", domain: "my-domain.auth0.com")

# This makes a duplicate request to `/oauth/token`
api_token = client.api_token("my-api-identifier")

# i.e. make an http request to this API with the api token as the Authorization bearer.
do_something(api_token)

However Auth0::Client.new will actually already request this api token on initialize, however this token is not available via any public interface. It is only stored as an instance variable in @token.

This commit makes Auth0::Client#get_token public (formerly private).

Now when in need of an API token for the API, we can do the following:

# This makes a request to `/oauth/token`
client = Auth0::Client.new(api_identifier: "my-api-identifier", client_id: "my-client-id", client_secret: "my-client-secret", domain: "my-domain.auth0.com")

 # This does not make another request to `/oauth/token` unless the existing token has expired.
api_token = client.get_token

# i.e make an http request to this API using the API token as the Authorization bearer.
do_something(api_token)

Why not make @token an attr_reader?

#get_token handles generating a new api token if the existing one has expired, saving me from having to handle that logic myself.

References

None

Testing

There already are tests for this method #get_token, I have just changed the tests to no longer use send as the method is public now.

  • This change adds unit test coverage
  • This change adds integration test coverage
  • This change has been tested on the latest version of Ruby

Checklist

Currently if I want to generate an API token for an API using the Auth0::Client, I must do the following:
```ruby
client = Auth0::Client.new(api_identifier: "my-api-identifier", **options)
api_token = client.api_token("my-api-identifier")
do_something(api_token) # Make a request to this API with the api token as the Authorization bearer.
```

However `Auth0::Client.new` will actually already request this api token on initialize, however this token is not available via any public interface.

This commit moves the `Auth0::Client#get_token` to a public method (formerly private).

Now when in need of any API token for an API, we can do the following:
```ruby
client = Auth0::Client.new(api_identifier: "my-api-identifier", **options)
api_token = client.get_token # No need for redundant request to `/oauth/token`.
do_something(api_token) # i.e make an http request to this API using the API token as the Authorization bearer.
```
@ttstarck ttstarck marked this pull request as ready for review September 17, 2025 19:33
@ttstarck ttstarck requested a review from a team as a code owner September 17, 2025 19:33
@ttstarck ttstarck changed the title Make get_token a public method on the Auth0::Client Make Auth0::Client#get_token public Sep 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant