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

feat: implement authenticateRequest and verifyToken JWKS helper methods #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

2ynn
Copy link
Contributor

@2ynn 2ynn commented Dec 10, 2024

This PR implements authenticateRequest and verifyToken helper methods.

Notes

  • the src/clerk/BackendAPI/Helpers/ folder is .genignored
  • unlike in the clerk-sdk-python implementation, the secretKey must be passed manually to the AuthenticateRequestOptions and the bearerAuth value passed during sdk instantiation cannot be reused for convenience.
    (due to $sdkConfiguration being a private member of the Jwks SubSDK class).
  • unlike in other implementations (Python, C#, Java) the iat claim is only checked when the nbf claim is not provided

Limitations

  • the added helper functions are only applicable for Backend APIs, afterSignInUrl/afterSignUpUrl options are not implemented
  • multi-domain setup (isSatellite, proxyUrl, signInUrl, signUpUrl) is not implemented
  • caching is not covered by this PR and skipJwksCache option is not made available

Tests

To run all tests (vendor/bin/pint) the following environment variables should be set:

  • CLERK_SESSION_TOKEN: The session token to be tested.
  • CLERK_SECRET_KEY: The Clerk secret key from the API Keys page in the Clerk Dashboard (needed for fetching Clerk's Jwks)
  • CLERK_JWT_KEY: The PEM public key from Clerk Dashboard (needed for networkless verification only)

Example Usage

  1. Remote JWKS (using secretKey)
use GuzzleHttp\Psr7\Request;
use Clerk\Backend\Helpers\Jwks\AuthenticateRequestOptions;
use Clerk\Backend\Helpers\Jwks\AuthenticateRequest;
use Clerk\Backend\Helpers\Jwks\RequestState;

class UserAuthentication
{
    public static function isSignedIn(Request $request): bool
    {
        $options = new AuthenticateRequestOptions(
            secretKey: getenv("CLERK_SECRET_KEY"),
            authorizedParties: ["https://example.com"]
        );

        $requestState = AuthenticateRequest::authenticateRequest($request, $options);

        return $requestState.isSignedIn();
    }
}
  1. Networkless (using local PEM formatted jwtKey)
<?php

use GuzzleHttp\Psr7\Request;
use Clerk\Backend\Helpers\Jwks\AuthenticateRequestOptions;
use Clerk\Backend\Helpers\Jwks\AuthenticateRequest;
use Clerk\Backend\Helpers\Jwks\RequestState;

class UserAuthentication
{
    public static function isSignedIn(Request $request): bool
    {
        $options = new AuthenticateRequestOptions(
            jwtKey: getenv('CLERK_JWT_KEY'),
            authorizedParties: ['https://example.com']
        );

        $requestState = AuthenticateRequest::authenticateRequest($request, $options);

        return $requestState->isSignedIn();
    }
}

@2ynn 2ynn force-pushed the feat/jwks-helpers branch 2 times, most recently from 120c73e to 0a1bfb6 Compare December 11, 2024 15:14
@2ynn 2ynn marked this pull request as ready for review December 11, 2024 15:14
@2ynn 2ynn force-pushed the feat/jwks-helpers branch from 0a1bfb6 to b2cd6c4 Compare December 11, 2024 15:15
@jescalan jescalan requested a review from octoper December 20, 2024 16:56
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