Auth0 Ruby SDK
repository·master·Indexed 42 days ago
https://github.com/auth0/ruby-auth0A Ruby interface for interacting with the Auth0 Management API to programmatically manage users, connections, clients, and other resources. The SDK provides the Auth0::Client for unified access to Authentication and Management APIs, as well as a standalone Auth0::Management client for token-based access.
What's inside Auth0
- The Management API client in this library is generated programmatically using Fern. If you are contributing to the library, do not modify the generated Management API methods directly in the source code; instead, changes should be made in the Fern configuration.
Manage Network ACLs
masterNetwork Access Control Lists (ACLs) allow you to manage access control entries for your client. You can list, create, get, update, set, or delete these entries.
Common Parameters for ACL operations:
description: String. A description of the ACL.active: Boolean. Whether the ACL is actively being used.priority: Integer. The evaluation order relative to other rules.rule:Auth0::Types::NetworkACLRule. The rule definition containingactionandscope(e.g.,scope: "management").
Use keyword arguments exclusively in v6
masterv6 has moved away from positional arguments and mixed option hashes. All methods now use keyword arguments exclusively.
Example Migration:
# v5 (Positional/Mixed) client.create_user('ConnName', email: 'user@example.com') client.user('id', fields: 'email') # v6 (All Keyword Arguments) client.users.create(connection: 'ConnName', email: 'user@example.com') client.users.get(id: 'id', fields: 'email')# v6 pattern client.users.create( connection: 'Username-Password-Authentication', email: 'user@example.com', password: 'Pass123!' )Update Management API method calls to the sub-client pattern
masterVersion 6 introduces a sub-client pattern for all Management API resources. Instead of calling methods directly on the
clientobject, you must access the resource sub-client first.Common transformations include:
- List/Get:
client.users(opts)$\rightarrow$client.users.list(**opts);client.user(id)$\rightarrow$client.users.get(id: id) - Create/Update/Delete:
client.create_user(...)$\rightarrow$client.users.create(...);client.patch_user(id, body)$\rightarrow$client.users.update(id: id, **body) - Sub-resources:
client.get_user_roles(user_id)$\rightarrow$client.users.roles.list(id: user_id)
- List/Get:
Manage Client Credentials
masterClient credentials can be used to configure Private Key JWT, mTLS authentication methods, or JWT-secured Authorization requests.
Important: Creating a credential does not automatically enable it. To use a credential, you must update the client configuration:
- For Private Key JWT or mTLS: Set the
client_authentication_methodsproperty on the client. - For JWT-secured Authorization requests: Set the
signed_request_objectproperty on the client.
- For Private Key JWT or mTLS: Set the
Use resource-specific sub-clients for Management API
masterIn v6, management methods are no longer top-level methods on the client. They are accessed through resource-specific sub-clients (e.g.,
client.users,client.roles,client.organizations).Comparison:
Action v5 (Top-level) v6 (Sub-client) List users client.users(per_page: 10)client.users.list(per_page: 10)Get user client.user('id')client.users.get(id: 'id')Create user client.create_user(conn, opts)client.users.create(connection: conn, **opts)# v6 pattern client.users.list(per_page: 10) client.users.get(id: 'auth0|123') client.users.create(connection: 'Username-Password-Authentication', email: 'a@b.com')Understand the Auth0 Ruby SDK Architecture
masterThe SDK provides a unified interface by combining two distinct API clients into a single entry point:
- Management API (
Auth0::Management): Auto-generated via Fern. These files reside inlib/auth0/and should not be edited manually as they are subject to regeneration. - Authentication API (
Auth0::Api::AuthenticationEndpoints): Hand-written endpoints ported from the original SDK. These are protected from regeneration. - Unified Client (
Auth0::Client): The primary interface defined inlib/auth0/auth_client.rb. It combines the two APIs using mixins for authentication logic and delegation for management logic.
- Management API (
Use Auth0::Client for unified Authentication and Management
masterIn v6,
Auth0::Clientacts as a wrapper that bridges the Authentication API and the Management API. It provides a unified interface where authentication methods are mixed in directly, and management methods are delegated to an internalAuth0::Managementinstance. This internal instance is lazily created and automatically refreshed when the token changes.Client Credentials with Automatic Token Management
When providing
client_idandclient_secret, the client automatically fetches a token via the client credentials grant, caches it, and re-fetches it upon expiry.v5 Pattern:
client = Auth0Client.new( client_id: 'YOUR_CLIENT_ID', client_secret: 'YOUR_CLIENT_SECRET', domain: 'tenant.auth0.com', api_version: 2 ) users = client.users(per_page: 10)v6 Pattern:
client = Auth0::Client.new( client_id: 'YOUR_CLIENT_ID', client_secret: 'YOUR_CLIENT_SECRET', domain: 'tenant.auth0.com' ) users = client.users.list(per_page: 10)require 'auth0' client = Auth0::Client.new( client_id: 'YOUR_CLIENT_ID', client_secret: 'YOUR_CLIENT_SECRET', domain: 'tenant.auth0.com' ) users = client.users.list(per_page: 10)Use the Management API via Auth0::Client
masterThe
Auth0::Clientprovides a high-level interface to the Management API. When initialized with aclient_secret, it can automatically manage tokens. You can access specific resource groups likeusers,roles,connections, andorganizationsdirectly from the client.Common operations include:
client.users.list(q: '...', per_page: 50)client.users.create(...)client.roles.list(...)client.organizations.list(...)
You can also access the underlying
Auth0::Managementinstance directly viaclient.management.# Example: Listing users with specific fields @users = auth0_client.users.list( q: "email:*auth0*", fields: "email,user_id,name", include_fields: true, page: 0, per_page: 50 ) # Example: Accessing management directly management = client.management management.users.list(per_page: 5)Authentication API compatibility in v6
masterThe Authentication API is not affected by the v6 major version change. Any code written for the Authentication API in v5 will work in v6 without modification. This includes methods such as:
signupauthorization_urlloginobtain_user_tokenschange_passwordlogout_urlvalidate_id_token
Run tests and linting
masterUse the following commands to run the test suites and linting tools:
- Run all tests: Executes the full test suite.
- Run unit tests: Executes only the unit tests.
- Run RuboCop: Runs the Ruby style guide linter.
# Run all tests bundle exec rake test # Run only unit tests bundle exec rake spec # Run rubocop bundle exec rubocopSet up the local development environment
masterTo develop with this SDK, ensure you have the correct Ruby version installed (refer to the
.ruby-versionfile in the repository).First, install the necessary dependencies using Bundler:
bundle install