-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Add webhook decoder #80
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
Open
Koosha-Owji
wants to merge
6
commits into
kinde-oss:master
Choose a base branch
from
Koosha-Owji:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fff16c2
feat: Add webhook decoder
Koosha-Owji 8e0c472
chore: fix coderabbit comment
Koosha-Owji a23182d
chore: Namespace JWKS cache per JWKS URL
Koosha-Owji 3bd281a
chore: delete extra space
Koosha-Owji e494111
chore: Guard JWKS fetches against untrusted domains
Koosha-Owji dfb5356
chore: fail-close JWKS domain validation and seed webhook JWKS host
Koosha-Owji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| namespace Kinde\KindeSDK\Webhooks; | ||
|
|
||
| use Kinde\KindeSDK\Sdk\Storage\Storage; | ||
| use Kinde\KindeSDK\Sdk\Utils\Utils; | ||
|
|
||
| /** | ||
| * Decode and validate webhook JWTs using the SDK's JWKS handling. | ||
| */ | ||
| final class WebhookDecoder | ||
| { | ||
| /** | ||
| * Decode a webhook JWT and return its payload as an array. | ||
| * | ||
| * Returns null when the token is missing, domain is missing, signature | ||
| * validation fails, or the payload cannot be decoded. | ||
| * | ||
| * @param string|null $token The webhook JWT. | ||
| * @param string|null $domain The Kinde domain (e.g. https://your-subdomain.kinde.com). | ||
| * | ||
| * @return array|null | ||
| */ | ||
| public static function decodeWebhook(?string $token, ?string $domain): ?array | ||
| { | ||
| if (empty($token) || empty($domain)) { | ||
| return null; | ||
| } | ||
|
|
||
| // Basic domain validation: require HTTPS scheme and host, strip path/query. | ||
| $normalizedDomain = rtrim($domain, '/'); | ||
| $parts = parse_url($normalizedDomain); | ||
| if ($parts === false || empty($parts['scheme']) || empty($parts['host']) || strtolower($parts['scheme']) !== 'https') { | ||
| return null; | ||
| } | ||
| $normalizedDomain = $parts['scheme'] . '://' . $parts['host'] . (isset($parts['port']) ? ':' . $parts['port'] : ''); | ||
| $jwksUrl = $normalizedDomain . '/.well-known/jwks.json'; | ||
|
|
||
| try { | ||
| // Seed the trusted JWKS URL so validation can enforce host matching | ||
| Storage::getInstance()->setJwksUrl($jwksUrl); | ||
| $payload = Utils::parseJWT($token, $jwksUrl); | ||
Koosha-Owji marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return is_array($payload) ? $payload : null; | ||
| } catch (\Throwable $e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| <?php | ||
|
|
||
| namespace Kinde\KindeSDK\Webhooks; | ||
|
|
||
| /** | ||
| * Webhook event type constants mirroring webhook-main. | ||
| */ | ||
| final class WebhookEventType | ||
| { | ||
| public const ORGANIZATION_CREATED = 'organization.created'; | ||
| public const ORGANIZATION_UPDATED = 'organization.updated'; | ||
| public const USER_CREATED = 'user.created'; | ||
| public const USER_UPDATED = 'user.updated'; | ||
| public const USER_DELETED = 'user.deleted'; | ||
| public const USER_AUTHENTICATION_FAILED = 'user.authentication_failed'; | ||
| public const USER_AUTHENTICATED = 'user.authenticated'; | ||
| public const ORGANIZATION_DELETED = 'organization.deleted'; | ||
| public const ROLE_CREATED = 'role.created'; | ||
| public const ROLE_UPDATED = 'role.updated'; | ||
| public const ROLE_DELETED = 'role.deleted'; | ||
| public const PERMISSION_CREATED = 'permission.created'; | ||
| public const PERMISSION_UPDATED = 'permission.updated'; | ||
| public const PERMISSION_DELETED = 'permission.deleted'; | ||
| public const SUBSCRIBER_CREATED = 'subscriber.created'; | ||
| public const ACCESS_REQUEST_CREATED = 'access_request.created'; | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
| public static function all(): array | ||
| { | ||
| return [ | ||
| self::ORGANIZATION_CREATED, | ||
| self::ORGANIZATION_UPDATED, | ||
| self::USER_CREATED, | ||
| self::USER_UPDATED, | ||
| self::USER_DELETED, | ||
| self::USER_AUTHENTICATION_FAILED, | ||
| self::USER_AUTHENTICATED, | ||
| self::ORGANIZATION_DELETED, | ||
| self::ROLE_CREATED, | ||
| self::ROLE_UPDATED, | ||
| self::ROLE_DELETED, | ||
| self::PERMISSION_CREATED, | ||
| self::PERMISSION_UPDATED, | ||
| self::PERMISSION_DELETED, | ||
| self::SUBSCRIBER_CREATED, | ||
| self::ACCESS_REQUEST_CREATED, | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| namespace Kinde\KindeSDK\Webhooks; | ||
|
|
||
| /** | ||
| * Origin of the webhook event. | ||
| */ | ||
| final class WebhookSource | ||
| { | ||
| public const ADMIN = 'admin'; | ||
| public const API = 'api'; | ||
| public const USER = 'user'; | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
| public static function all(): array | ||
| { | ||
| return [ | ||
| self::ADMIN, | ||
| self::API, | ||
| self::USER, | ||
| ]; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.