|
| 1 | +import { worker, UserError } from "rivetkit"; |
| 2 | + |
| 3 | +// Basic auth worker - requires API key |
| 4 | +export const authWorker = worker({ |
| 5 | + state: { requests: 0 }, |
| 6 | + onAuth: (opts) => { |
| 7 | + const { req, intents, params } = opts; |
| 8 | + const apiKey = (params as any)?.apiKey; |
| 9 | + if (!apiKey) { |
| 10 | + throw new UserError("API key required", { code: "missing_auth" }); |
| 11 | + } |
| 12 | + |
| 13 | + if (apiKey !== "valid-api-key") { |
| 14 | + throw new UserError("Invalid API key", { code: "invalid_auth" }); |
| 15 | + } |
| 16 | + |
| 17 | + return { userId: "user123", token: apiKey }; |
| 18 | + }, |
| 19 | + actions: { |
| 20 | + getRequests: (c) => { |
| 21 | + c.state.requests++; |
| 22 | + return c.state.requests; |
| 23 | + }, |
| 24 | + getUserAuth: (c) => c.conn.auth, |
| 25 | + }, |
| 26 | +}); |
| 27 | + |
| 28 | +// Intent-specific auth worker - checks different permissions for different intents |
| 29 | +export const intentAuthWorker = worker({ |
| 30 | + state: { value: 0 }, |
| 31 | + onAuth: (opts) => { |
| 32 | + const { req, intents, params } = opts; |
| 33 | + console.log('intents', intents, params); |
| 34 | + const role = (params as any)?.role; |
| 35 | + |
| 36 | + if (intents.has("create") && role !== "admin") { |
| 37 | + throw new UserError("Admin role required for create operations", { code: "insufficient_permissions" }); |
| 38 | + } |
| 39 | + |
| 40 | + if (intents.has("action") && !["admin", "user"].includes(role || "")) { |
| 41 | + throw new UserError("User or admin role required for actions", { code: "insufficient_permissions" }); |
| 42 | + } |
| 43 | + |
| 44 | + return { role, timestamp: Date.now() }; |
| 45 | + }, |
| 46 | + actions: { |
| 47 | + getValue: (c) => c.state.value, |
| 48 | + setValue: (c, value: number) => { |
| 49 | + c.state.value = value; |
| 50 | + return value; |
| 51 | + }, |
| 52 | + getAuth: (c) => c.conn.auth, |
| 53 | + }, |
| 54 | +}); |
| 55 | + |
| 56 | +// Public worker - empty onAuth to allow public access |
| 57 | +export const publicWorker = worker({ |
| 58 | + state: { visitors: 0 }, |
| 59 | + onAuth: () => { |
| 60 | + return null; // Allow public access |
| 61 | + }, |
| 62 | + actions: { |
| 63 | + visit: (c) => { |
| 64 | + c.state.visitors++; |
| 65 | + return c.state.visitors; |
| 66 | + }, |
| 67 | + }, |
| 68 | +}); |
| 69 | + |
| 70 | +// No auth worker - should fail when accessed publicly (no onAuth defined) |
| 71 | +export const noAuthWorker = worker({ |
| 72 | + state: { value: 42 }, |
| 73 | + actions: { |
| 74 | + getValue: (c) => c.state.value, |
| 75 | + }, |
| 76 | +}); |
| 77 | + |
| 78 | +// Async auth worker - tests promise-based authentication |
| 79 | +export const asyncAuthWorker = worker({ |
| 80 | + state: { count: 0 }, |
| 81 | + onAuth: async (opts) => { |
| 82 | + const { req, intents, params } = opts; |
| 83 | + // Simulate async auth check (e.g., database lookup) |
| 84 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 85 | + |
| 86 | + const token = (params as any)?.token; |
| 87 | + if (!token) { |
| 88 | + throw new UserError("Token required", { code: "missing_token" }); |
| 89 | + } |
| 90 | + |
| 91 | + // Simulate token validation |
| 92 | + if (token === "invalid") { |
| 93 | + throw new UserError("Token is invalid", { code: "invalid_token" }); |
| 94 | + } |
| 95 | + |
| 96 | + return { userId: `user-${token}`, validated: true }; |
| 97 | + }, |
| 98 | + actions: { |
| 99 | + increment: (c) => { |
| 100 | + c.state.count++; |
| 101 | + return c.state.count; |
| 102 | + }, |
| 103 | + getAuthData: (c) => c.conn.auth, |
| 104 | + }, |
| 105 | +}); |
0 commit comments