Skip to content

Commit 1525fc7

Browse files
Merge pull request #7 from Crowdhandler/improvement/lite-validator-support
Native support for the lite validator and some other minor adjustments
2 parents f821324 + 9a049fc commit 1525fc7

6 files changed

Lines changed: 418 additions & 30 deletions

File tree

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ const result = await gatekeeper.validateRequest();
158158
responseID: string, // Response ID for performance tracking (when promoted)
159159
deployment: string, // Deployment identifier from the API
160160
token: string, // The session token
161-
hash: string | null // Signature hash for validation (when available)
161+
hash: string | null, // Signature hash for validation (when available)
162+
liteValidatorRedirect: boolean, // true = redirect to lite validator
163+
liteValidatorUrl: string // URL for lite validator redirect
162164
}
163165
```
164166

@@ -262,7 +264,16 @@ const instance = crowdhandler.init({
262264
timeout: 5000, // API timeout in milliseconds
263265
trustOnFail: true, // Allow access if API fails
264266
fallbackSlug: '', // Fallback room slug when trustOnFail is false
265-
cookieName: 'crowdhandler' // Custom cookie name (default: 'crowdhandler')
267+
cookieName: 'crowdhandler', // Custom cookie name (default: 'crowdhandler')
268+
liteValidator: false, // Enable lite validator mode (default: false)
269+
roomsConfig: [{ // Array of room configurations for lite validator
270+
domain: string, // e.g. 'https://example.com'
271+
slug: string, // Room identifier
272+
urlPattern?: string, // URL pattern to match
273+
patternType?: 'regex' | 'contains' | 'all',
274+
queueActivatesOn?: number, // Unix timestamp
275+
timeout?: number // Timeout in seconds
276+
}]
266277
}
267278
});
268279
```
@@ -567,6 +578,43 @@ await gatekeeper.recordPerformance({
567578
});
568579
```
569580

581+
### Lite Validator Mode
582+
583+
Lite validator mode provides token refresh without API calls by checking room configuration locally. To enable it:
584+
585+
1. Set `liteValidator: true` in options
586+
2. Fetch and provide your rooms configuration from the CrowdHandler API
587+
588+
```javascript
589+
// First, fetch your rooms configuration
590+
const { client } = init({ publicKey: 'YOUR_PUBLIC_KEY' });
591+
const roomsResponse = await client.rooms().get();
592+
593+
// Then initialize with lite validator enabled
594+
const { gatekeeper } = init({
595+
publicKey: 'YOUR_PUBLIC_KEY',
596+
request: req,
597+
response: res,
598+
options: {
599+
liteValidator: true, // Enable lite validator
600+
roomsConfig: roomsResponse.result // Pass the rooms array from API
601+
}
602+
});
603+
604+
// Handle the lite validator redirect
605+
const result = await gatekeeper.validateRequest();
606+
607+
if (result.liteValidatorRedirect) {
608+
// Redirect to refresh token/session
609+
return gatekeeper.redirect(result.liteValidatorUrl);
610+
}
611+
```
612+
613+
**When lite validator activates:**
614+
- URL matches a room in your config
615+
- Token is missing or >12 hours old
616+
- Redirects to CrowdHandler to refresh session
617+
570618
## Testing
571619

572620
The SDK includes comprehensive testing tools:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "crowdhandler-sdk",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "",
55
"homepage": "https://www.crowdhandler.com",
66
"repository": {

src/common/types.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import { z } from "zod";
22

3+
// Lite Validator types
4+
export const RoomConfig = z.object({
5+
domain: z.string(), // Format: "https://example.com"
6+
urlPattern: z.string().optional(),
7+
patternType: z.enum(['regex', 'contains', 'all']).optional(),
8+
queueActivatesOn: z.number().optional(),
9+
slug: z.string(),
10+
timeout: z.number().optional()
11+
});
12+
13+
export const RoomsConfig = z.array(RoomConfig);
14+
315
//Gatekeeper Options
416
export const GatekeeperOptions = z.object({
517
debug: z.boolean().optional(),
@@ -8,6 +20,8 @@ export const GatekeeperOptions = z.object({
820
timeout: z.number().optional(),
921
trustOnFail: z.boolean().optional(),
1022
cookieName: z.string().optional(),
23+
liteValidator: z.boolean().optional(),
24+
roomsConfig: RoomsConfig.optional(), // Array of room configs
1125
});
1226

1327
export const GatekeeperKeyPair = z.object({
@@ -94,7 +108,7 @@ export const SignatureSourceObject = z.object({
94108
crowdhandlerCookieValue: CookieObject.optional(),
95109
});
96110

97-
export const GetTokenOptions = z.object({
111+
export const ExtractTokenOptions = z.object({
98112
//object can contain anything and we don't know any of the possible values
99113
crowdhandlerCookieValue: CookieObject.optional(),
100114
chID: z.string().optional(),
@@ -132,6 +146,8 @@ export const ValidateRequestObject = z.object({
132146
deployment: z.string().optional(),
133147
hash: z.string().nullable().optional(),
134148
token: z.string().optional(),
149+
liteValidatorRedirect: z.boolean().optional(),
150+
liteValidatorUrl: z.string().optional(),
135151
});
136152

137153
export const HttpErrorWrapper = z.object({

0 commit comments

Comments
 (0)