Auth0 PHP SDK

repository·main·Indexed 19 days ago

https://github.com/auth0/auth0-php

The Auth0 PHP SDK provides tools for integrating Auth0 Authentication and Management APIs into PHP applications. It supports stateful web applications using sessions and stateless APIs using access tokens. The SDK includes namespaces for authentication, management, configuration, token validation (JWT), and various storage implementations such as CookieStore and SessionStore. It requires PHP 8.2+ and supports PSR-7, PSR-17, and PSR-18 standards.

Tokens
25.8K
Snippets
70
Records
117
Agent score
64%

What's inside auth0-php

  1. Use the Auth0 PHP SDK API namespaces

    main

    The Auth0\SDK\API\ namespace serves as the entry point for interacting with Auth0 services via the PHP SDK. It is divided into two primary functional areas:

    1. Authentication: Used for handling user authentication flows, such as logging in, logging out, and managing sessions.
    2. Management: Used for interacting with the Auth0 Management API to perform administrative tasks like managing users, connections, clients, and tenant configurations.

    Refer to the specific documentation for Authentication or Management to learn how to instantiate and use these sub-namespaces.

  2. Available storage implementations in Auth0\SDK\Store\

    main

    The Auth0\SDK\Store\ namespace provides various implementations for storing data required by the SDK (such as state or nonces). Depending on your application architecture and persistence requirements, you can choose from the following store types:

    • CookieStore: Stores data in browser cookies.
    • MemoryStore: Stores data in memory (volatile, suitable for single-request lifecycles or testing).
    • Psr6Store: Implements the PSR-6 Cache Interface for standardized caching integration.
    • Psr14Store: Implements the PSR-14 Event Dispatcher interface (for event-driven storage patterns).
    • SessionStore: Stores data within the PHP session.
  3. Explore Auth0\SDK\Event\ event types

    main

    The Auth0\SDK\Event\ namespace contains event objects that are dispatched during the lifecycle of an Auth0 request. These events allow you to hook into specific points in the request/response cycle.

    Currently, the available event types are:

    • HttpRequestBuilt: Dispatched when an HTTP request has been constructed and is ready to be sent.
    • HttpResponseBuilt: Dispatched when an HTTP response has been received and constructed.

    You can use a Psr14Store to manage and listen for these events.

  4. Configure the Auth0 PHP SDK

    main
    The Auth0\SDK\Configuration\ namespace contains the core classes used to define how the SDK behaves and how it manages state. To customize the SDK's behavior (such as setting domain, client IDs, or custom request headers), you must interact with the SdkConfiguration class. To manage how the SDK tracks user sessions or authentication state, you interact with SdkState.
  5. Handle exceptions in the Auth0 PHP SDK

    main

    The Auth0 PHP SDK uses a specific hierarchy of exceptions to signal different types of failures. When integrating the SDK, you should catch these exceptions to handle errors gracefully. The available exception types include:

    • Auth0Exception: The base exception class for all SDK-related errors.
    • AuthenticationException: Errors occurring during the authentication process.
    • ConfigurationException: Errors related to incorrect or missing SDK configuration.
    • InvalidTokenException: Errors when a provided token is malformed or invalid.
    • NetworkException: Errors related to connectivity or communication with Auth0 servers.
    • ArgumentException: Errors caused by passing invalid arguments to SDK methods.
    • PaginatorException: Errors occurring during paginated API requests.
    • StateException: Errors related to the internal state of the SDK or session.
  6. Understand the Auth0\SDK\Token\ namespace components

    main

    The Auth0\SDK\Token\ namespace provides the core tools for handling JSON Web Tokens (JWTs) within the Auth0 PHP SDK. It is composed of three primary functional areas:

    • Parser: Used to decode and extract information from a JWT without necessarily validating its signature.
    • Validator: Used to verify the integrity and authenticity of a token by checking its signature and standard claims.
    • Verifier: Provides the mechanism to define and execute the validation logic against specific requirements.
  7. Use Authentication and Management Factories

    main

    In SDK v8.0, you can access the Authentication and Management API sub-classes via factory methods on the Auth0 instance. These methods return pre-configured singletons that share the base configuration, eliminating the need to reconfigure them independently.

    use Auth0//SDK//Auth0;
    
    // Configure just once:
    $auth0 = new Auth0([
        'domain' => 'your-tenant.auth0.com',
        'clientId' => 'application_client_id',
        'clientSecret' => 'application_client_secret',
        'redirectUri' => 'https://yourapplication.com/auth/callback',
        'tokenAlgorithm' => 'HS256'
    ]);
    
    // Returns an instance already configured for you.
    $authentication = $auth0->authentication();
    $management = $auth0->management();
    
    // Or, a fluent example:
    $response = $auth0->management()->users()->getAll();
  8. Configure HTTP Networking with PSR-7, PSR-17, and PSR-18

    main

    The SDK handles network requests to the Auth0 API using a combination of three PSR standards. To use the SDK, you must provide implementations for these interfaces:

    1. PSR-18 (HTTP Client): Used to actually send the network requests and receive responses. The SDK uses your provided PSR-18 Client to issue requests.
    2. PSR-17 (HTTP Factories): Used to create the message objects. The SDK uses your provided PSR-17 Factory to generate the requests and responses.
    3. PSR-7 (HTTP Messages): The standard for the message objects themselves. The SDK uses PSR-7 to represent the HTTP requests and responses being sent to and from the Auth0 API.

    Workflow: The PSR-18 Client uses the PSR-17 Factory to create PSR-7 Messages which are then sent over the wire.

  9. Use custom JWKS paths and kid checks

    main
    Version 5.3.0 introduced the ability to configure custom JSON Web Key Set (JWKS) paths and perform kid (Key ID) checks within the ext{JWKFetcher} component, providing more control over how JWTs are validated against your Auth0 domain.
  10. Use SdkState to manage authentication state

    main
    The SdkState class is responsible for managing the state of the SDK, which typically involves handling session data, tokens, and authentication status. This abstraction allows the SDK to maintain continuity across requests. Refer to the SdkState documentation for details on how to implement or customize state management.