~/docs
Resonate
Resonate
1.x 1.5.0, current
Resonate
Toggle sidebar
Resonate
webpatser/resonate 1.5.0

Resonate 1.x

Token Auth

Subscribe to private and presence channels with a JWT or any pluggable token instead of the /broadcasting/auth round-trip.

webpatser/resonate-token-auth v1.1.0. Resonate authenticates private and presence subscriptions the Pusher way: the browser POSTs to /broadcasting/auth and carries the returned HMAC in pusher:subscribe. That is awkward for mobile clients without a session cookie, server-to-server bots, and apps whose identity lives in an OAuth or JWT issuer. This plugin lets a connection present a token directly to Resonate.

How it works

When a pusher:subscribe arrives with a token field instead of auth, the plugin:

  1. validates the token with the configured TokenAuthenticator (JWT by default);
  2. checks that the token is bound to the application it was presented on;
  3. asks the ChannelAuthorizer whether the claims may subscribe to this channel;
  4. for presence channels, builds the channel_data from the token's user_id and optional user_info;
  5. computes the HMAC signature /broadcasting/auth would have returned, using the app's secret;
  6. hands the rewritten payload to the standard subscribe path.

Because the plugin signs with the secret the verifier checks against, the rest of Resonate runs unchanged. A pusher:subscribe that already carries auth is relayed untouched, so cookie-based browser clients and token clients share one server.

Two flows

Per-subscribe (stateless), with the token on every subscribe:

{"event": "pusher:subscribe", "data": {"channel": "presence-chat.42", "token": "..."}}

Pre-authenticated, where you authenticate once and subscribe without a token:

{"event": "app:authenticate", "data": {"token": "..."}}

The plugin replies with app:authenticated and caches the validated claims on the connection. A per-subscribe token always overrides the cached claims. Cached claims are re-checked against exp plus leeway on every subscribe. After they expire, new subscribes are rejected with reason expired_token.

Application binding

Resonate can serve several applications from one process while the JWT settings are one global block. Every token therefore names the application it was minted for, in an app_id claim by default:

{"sub": "42", "app_id": "your-app-id", "exp": 1800000000}

The value is matched against the connection's Resonate application id (app_id in config/reverb.php, not the app key). A mismatch is always rejected with a pusher:error and a TokenRejected event with reason application_mismatch.

To migrate existing issuers without downtime:

  1. Set RESONATE_TOKEN_AUTH_REQUIRE_APP_CLAIM=false and deploy. Tokens without the claim keep working. Tokens naming the wrong application are still rejected.
  2. Add 'app_id' => config('reverb.apps.apps.0.app_id') to whatever mints your tokens and let the old ones expire.
  3. Set the switch back to true.

Install

composer require webpatser/resonate-token-auth
php artisan vendor:publish --tag=resonate-token-auth-config

Register the plugin in config/reverb.php:

'servers' => [
'reverb' => [
// ...
'plugins' => [
\Webpatser\ResonateTokenAuth\TokenAuthPlugin::class,
],
],
],

Restart Resonate. Requirements: PHP 8.6+ and Resonate 1.4+.

Configuration

RESONATE_TOKEN_AUTH_ALG=HS256
RESONATE_TOKEN_AUTH_SECRET=your-shared-jwt-secret
# RESONATE_TOKEN_AUTH_PUBLIC_KEY (PEM) # for RS*/ES* algorithms
# RESONATE_TOKEN_AUTH_ISSUER=https://your.app
# RESONATE_TOKEN_AUTH_AUDIENCE=resonate
# RESONATE_TOKEN_AUTH_REQUIRE_APP_CLAIM=false # only while migrating issuers
Key Default Purpose
algorithm HS256 HS256, HS384, HS512 (shared secret) or RS256, RS384, RS512, ES256, ES384 (public key).
secret null Shared secret for HS* algorithms.
public_key null PEM-encoded public key for RS* and ES* algorithms.
issuer null When set, the token's iss claim must match.
audience null When set, the token's aud claim must match (string or list).
leeway 30 Seconds of clock skew tolerated on exp, nbf and iat, and when re-checking cached claims.
require_app_claim true Whether a token with no application-binding claim is accepted. A mismatching claim is rejected either way.
pin_identity false When true, a per-subscribe token whose subject differs from the pre-authenticated identity is rejected (identity_mismatch).
claims.user_id sub Which JWT claim is the user id.
claims.user_info user_info Which claim holds optional presence user info.
claims.channels channels Which claim, if present, lists allowed channels (glob patterns).
claims.app_id app_id Which claim carries the application binding.

Claim rules

Claim Rule
exp Required. A token without one is rejected.
channels Optional. If present it must be a list of strings. A non-list is rejected.
channels: [] An allow-list that permits nothing: every private and presence subscribe is denied. Only an absent channels claim means no token-level restriction.
app_id Required unless require_app_claim is false. Must equal the connection's application id.

Issue a token

A Laravel helper that mints a JWT this plugin accepts, using firebase/php-jwt:

use Firebase\JWT\JWT;
 
return JWT::encode([
'iss' => config('app.url'),
'aud' => 'resonate',
'sub' => (string) $user->id,
'app_id' => config('reverb.apps.apps.0.app_id'),
'user_info' => ['name' => $user->name],
'channels' => ['presence-chat.*', 'private-user.'.$user->id],
'iat' => time(),
'exp' => time() + 300,
], config('resonate-token-auth.secret'), 'HS256');

Another token format

To accept Sanctum tokens, opaque tokens against an introspection endpoint or anything else, implement TokenAuthenticator and bind it:

// AppServiceProvider::register()
$this->app->bind(
\Webpatser\ResonateTokenAuth\Contracts\TokenAuthenticator::class,
\App\Resonate\SanctumTokenAuthenticator::class,
);

ChannelAuthorizer works the same way for tenant rules, role checks or external lookups. TokenClaims::channels() returns null when the token carried no allow-list and a list<string> when it did, so an authorizer that reads [] as no restriction fails open. A custom TokenAuthenticator must pass exp and the application-binding claim through into TokenClaims::$raw.

Security notes

  • Tokens are bearer tokens. Serve them over TLS and keep exp short.
  • A valid, unexpired JWT cannot be revoked by this plugin. If revocation matters, bind a custom TokenAuthenticator that checks a deny list.
  • The first valid app:authenticate binds the cached claims. Later ones are ignored, so a long-lived connection cannot change identity.
  • Public channels are untouched. Only private-* and presence-* subscribes are authorized.