
Security News
Meet Socket at Black Hat Europe and BSides London 2025
Socket is heading to London! Stop by our booth or schedule a meeting to see what we've been working on.
@auth0/auth0-cloudflare-agents-api
Advanced tools
A PartyServer mixin for adding Auth0 API authentication to your PartyServer applications.
A PartyServer mixin for adding OAuth 2.0 JWT Bearer Token authentication to your PartyServer applications, with Auth0 support.
It should work with:
This package provides a mixin that adds authentication functionality to a PartyServer server using JSON Web Token (JWT) Profile for OAuth 2.0 Access Tokens. It allows you to secure your PartyServer applications by validating access tokens from requests and connections, with built-in support for Auth0.
npm install @auth0/auth0-cloudflare-agent-api
# or
yarn add @auth0/auth0-cloudflare-agent-api
# or
pnpm add @auth0/auth0-cloudflare-agent-api
import { Server } from "partyserver";
import { WithAuth } from "@auth0/auth0-cloudflare-agent-api";
// Define your environment type
type MyEnv = {
AUTH0_DOMAIN: string;
AUTH0_AUDIENCE: string;
// ... other environment variables
};
// Create your server class with authentication
class MyAuthenticatedServer extends WithAuth(Server<MyEnv>) {
// Your server implementation
}
// Pass options as parameters to the mixin function
class MyAuthenticatedServer extends WithAuth(Server, {
// Optional: make authentication optional
authRequired: false,
// Optional: provide a debug function
debug: (message, ctx) => console.log(message, ctx),
}) {
// Your server implementation
}
// Start the server
const server = new MyAuthenticatedServer({
env: {
AUTH0_DOMAIN: "your-tenant.auth0.com",
AUTH0_AUDIENCE: "your-api-audience",
// ... other environment variables
},
});
server.start();
Once you've added the mixin, you can access token information and claims:
class MyAuthenticatedServer extends WithAuth(Server<MyEnv>) {
//optionally override onAuthenticatedRequest
onAuthenticatedRequest(req: Request) {
// Get the JWT claims from the token
const claims = this.getClaims();
if (claims?.sub !== expectedUserId) {
return new Response("You are not welcome", { status: 401 });
}
}
onRequest(req: Request) {
// Get the token set from the request
const tokenSet = this.getCredentials();
// Get the Access Token claims from the token
const claims = this.getClaims();
// Now you can use the claims to identify the user
console.log(`User ID: ${claims?.sub}`);
// You can also require specific scopes for certain operations
try {
await this.requireAuth({ scopes: "read:data" });
// The user has the required scope
} catch (error) {
if (error instanceof UnauthorizedError) {
return error.toResponse();
}
// Handle other errors
return new Response("Unknown error", { status: 500 });
}
// Continue processing the request...
return new Response("Hello authenticated user!");
}
//optionally override onAuthenticatedConnect
onAuthenticatedConnect(connection: Connection, ctx: ConnectionContext) {
// Get the JWT claims from the token
const claims = this.getClaims();
if (claims.sub !== expectedUserId) {
connection.close(1008, "I don't like you");
}
}
onConnect(connection: Connection, ctx: ConnectionContext) {
// Get the token set from the connection
const tokenSet = this.getCredentials();
// Get the JWT claims from the token
const claims = this.getClaims();
// Use the claims in your connection handling logic
console.log(`Connected user: ${claims?.sub}`);
// You can also require specific scopes for certain operations
try {
await this.requireAuth({ scopes: ["read:data", "write:data"] });
// The user has both required scopes
} catch (error) {
if (error instanceof UnauthorizedError) {
return error.terminateConnection(connection);
}
// Handle other errors
throw error;
}
}
onMessage(connection: Connection, message: unknown) {
// Get the token set from the connection
const tokenSet = this.getCredentials();
// Get the JWT claims from the token
const claims = this.getClaims();
// Use the claims in your message handling logic
console.log(`Message from user: ${claims?.sub}`);
// Process the message...
}
}
When a client makes a request or connection:
access_token query parameterIf validation succeeds:
If validation fails:
The WithAuth mixin requires the following environment variables:
AUTH0_DOMAIN: Your Auth0 tenant domain (e.g., "your-tenant.auth0.com")AUTH0_AUDIENCE: The audience for the JWT, typically your API identifierYou can also configure the mixin with options:
WithAuth(Server, {
// Make authentication optional (default: true)
authRequired: false,
// Optional debug function
debug: (message, context) => console.log(message, context),
});
WithAuth(BaseClass, options?)A mixin factory function that adds authentication functionality to a PartyServer class.
Parameters:
BaseClass: The base class to extend from. This should be a class that extends Server.options: Optional configuration object:
authRequired: Boolean indicating whether authentication is required (default: true)debug: Function for debugging (default: noop)Returns:
getCredentials(): TokenSet | undefinedGets the token set associated with the current context.
Returns:
TokenSet object containing:
access_token: The JWT bearer tokenid_token: Optional ID token (from x-id-token header)refresh_token: Optional refresh token (from x-refresh-token header)getClaims(): Token | undefinedGets the decoded JWT claims from the access token.
Returns:
requireAuth(options?: { scopes?: string | string[] }): Promise<TokenSet>Requires authentication with optional scope checking.
Parameters:
options: Optional configuration object:
scopes: String or array of strings representing required scopesReturns:
Throws:
UnauthorizedError: If no valid token is presentInvalidTokenError: If the token is invalidInsufficientScopeError: If the token doesn't have the required scopesThe mixin accepts tokens in the following formats:
Authorization: Bearer <token>?access_token=<token>The WithOwnership mixin adds ownership capabilities to a PartyServer that already has authentication provided by the WithAuth mixin. This is particularly useful for scenarios where you need to restrict access to resources based on ownership, such as private chats or user-specific data.
// Then add ownership with WithOwnership
class MyServer extends WithOwnership(WithAuth(Server<MyEnv>), {
// Optional: provide a debug function
debug: (message, ctx) => console.log(message, ctx),
}) {
// Your server implementation
// Optionally override authorization methods
async onAuthorizedConnect(connection, ctx) {
console.log("Owner connected:", this.getClaims()?.sub);
// Handle authorized connection
}
async onAuthorizedRequest(req) {
console.log("Owner made a request:", this.getClaims()?.sub);
// Handle authorized request
}
}
setOwner(owner: string, overwrite: boolean = false): Promise<void>Sets the owner of the object. By default, it will throw an error if the owner is already set to a different user unless overwrite is set to true.
Parameters:
owner: The user ID (sub from JWT claims) to set as the owneroverwrite: Optional boolean to allow overwriting an existing ownerExample:
// When initializing a new chat or resource
async onCreate() {
const claims = this.getClaims();
if (claims?.sub) {
await this.setOwner(claims.sub);
}
}
getOwner(): Promise<string | undefined>Gets the current owner of the object.
Returns:
Example:
async checkOwnership() {
const owner = await this.getOwner();
console.log(`This resource is owned by: ${owner}`);
}
When a client makes a request or connection:
WithAuth mixinIf the ownership check succeeds:
onAuthorizedConnect or onAuthorizedRequest method is calledIf the ownership check fails:
The WithOwnership mixin is designed to work with Cloudflare DurableObjects for storing ownership data. The mixin uses the DurableObject's storage API to persist ownership information.
Note: If you're not using DurableObjects, you'll need to override the setOwner and getOwner methods to implement your own storage mechanism.
We appreciate feedback and contribution to this repo! Before you get started, please see the following:
To provide feedback or report a bug, please raise an issue on our issue tracker.
Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0?
This project is licensed under the Apache 2.0 license. See the LICENSE file for more info.
FAQs
A PartyServer mixin for adding Auth0 API authentication to your PartyServer applications.
The npm package @auth0/auth0-cloudflare-agents-api receives a total of 21 weekly downloads. As such, @auth0/auth0-cloudflare-agents-api popularity was classified as not popular.
We found that @auth0/auth0-cloudflare-agents-api demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 48 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket is heading to London! Stop by our booth or schedule a meeting to see what we've been working on.

Security News
OWASP’s 2025 Top 10 introduces Software Supply Chain Failures as a new category, reflecting rising concern over dependency and build system risks.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.