|
| 1 | +# AuthZen TypeScript Client |
| 2 | + |
| 3 | +A comprehensive TypeScript client library for interacting with [AuthZen](https://openid.github.io/authzen/)-compliant Policy Decision Points (PDPs). This library implements the AuthZen Authorization API 1.0 specification. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- ✅ **Access Evaluation API** - Single authorization decisions |
| 8 | +- ✅ **Access Evaluations API** - Batch authorization decisions with multiple evaluation semantics |
| 9 | +- 🔄 **Search APIs** - Subject, Resource, and Action search (coming soon) |
| 10 | +- 🛡️ **Type Safety** - Full TypeScript support with comprehensive type definitions |
| 11 | +- 🚀 **Modern** - Built with ES2020, supports both Node.js and browser environments |
| 12 | +- 🔧 **Flexible** - Configurable fetch implementation, timeouts, and custom headers |
| 13 | +- 📝 **Well Documented** - Comprehensive JSDoc comments and examples |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```bash |
| 18 | +npm install authzen-client |
| 19 | +``` |
| 20 | + |
| 21 | +For Node.js environments, you'll also need to install node-fetch: |
| 22 | + |
| 23 | +```bash |
| 24 | +npm install node-fetch |
| 25 | +npm install --save-dev @types/node-fetch |
| 26 | +``` |
| 27 | + |
| 28 | +## Quick Start |
| 29 | + |
| 30 | +```typescript |
| 31 | +import { AuthZenClient, createSubject, createAction, createResource } from 'authzen-client'; |
| 32 | + |
| 33 | +// Create a client |
| 34 | +const client = new AuthZenClient({ |
| 35 | + baseUrl: 'https://pdp.mycompany.com', |
| 36 | + token: 'your-bearer-token', // Optional |
| 37 | +}); |
| 38 | + |
| 39 | +// Simple access evaluation |
| 40 | +const response = await client.evaluate({ |
| 41 | + subject: createSubject('user', 'alice@example.com'), |
| 42 | + action: createAction('can_read'), |
| 43 | + resource: createResource('document', '123'), |
| 44 | +}); |
| 45 | + |
| 46 | +if (response.decision) { |
| 47 | + console.log('✅ Access granted'); |
| 48 | +} else { |
| 49 | + console.log('❌ Access denied'); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## API Reference |
| 54 | + |
| 55 | +### Client Configuration |
| 56 | + |
| 57 | +```typescript |
| 58 | +interface AuthZenClientConfig { |
| 59 | + baseUrl: string; // PDP base URL (required) |
| 60 | + apiVersion?: string; // API version (default: 'v1') |
| 61 | + token?: string; // Bearer token for authentication |
| 62 | + headers?: Record<string, string>; // Custom headers |
| 63 | + timeout?: number; // Request timeout in ms (default: 30000) |
| 64 | + fetch?: Function; // Custom fetch implementation |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Single Access Evaluation |
| 69 | + |
| 70 | +Evaluate a single authorization request: |
| 71 | + |
| 72 | +```typescript |
| 73 | +const response = await client.evaluate({ |
| 74 | + subject: { |
| 75 | + type: 'user', |
| 76 | + id: 'alice@example.com', |
| 77 | + properties: { department: 'Sales' } |
| 78 | + }, |
| 79 | + action: { |
| 80 | + name: 'can_read', |
| 81 | + properties: { method: 'GET' } |
| 82 | + }, |
| 83 | + resource: { |
| 84 | + type: 'document', |
| 85 | + id: '123', |
| 86 | + properties: { classification: 'confidential' } |
| 87 | + }, |
| 88 | + context: { |
| 89 | + time: '2024-01-01T12:00:00Z', |
| 90 | + location: 'office' |
| 91 | + } |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +### Batch Access Evaluations |
| 96 | + |
| 97 | +Evaluate multiple authorization requests in a single call: |
| 98 | + |
| 99 | +```typescript |
| 100 | +const response = await client.evaluateBatch({ |
| 101 | + // Default values applied to all evaluations |
| 102 | + subject: createSubject('user', 'alice@example.com'), |
| 103 | + context: { time: new Date().toISOString() }, |
| 104 | + |
| 105 | + // Individual evaluations |
| 106 | + evaluations: [ |
| 107 | + { |
| 108 | + action: createAction('can_read'), |
| 109 | + resource: createResource('document', '123') |
| 110 | + }, |
| 111 | + { |
| 112 | + action: createAction('can_write'), |
| 113 | + resource: createResource('document', '456') |
| 114 | + } |
| 115 | + ], |
| 116 | + |
| 117 | + options: { |
| 118 | + evaluations_semantic: 'execute_all' // or 'deny_on_first_deny' or 'permit_on_first_permit' |
| 119 | + } |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
| 123 | +### Evaluation Semantics |
| 124 | + |
| 125 | +The batch evaluation API supports three evaluation semantics: |
| 126 | + |
| 127 | +- **`execute_all`** (default) - Execute all evaluations and return all results |
| 128 | +- **`deny_on_first_deny`** - Stop and return on the first denial (short-circuit AND) |
| 129 | +- **`permit_on_first_permit`** - Stop and return on the first permit (short-circuit OR) |
| 130 | + |
| 131 | +## Utility Functions |
| 132 | + |
| 133 | +The library provides helpful utility functions for creating AuthZen objects: |
| 134 | + |
| 135 | +```typescript |
| 136 | +import { |
| 137 | + createSubject, |
| 138 | + createResource, |
| 139 | + createAction, |
| 140 | + createContext, |
| 141 | + createContextWithTime, |
| 142 | + SubjectTypes, |
| 143 | + ResourceTypes, |
| 144 | + ActionNames |
| 145 | +} from 'authzen-client'; |
| 146 | + |
| 147 | +// Create objects with utilities |
| 148 | +const user = createSubject(SubjectTypes.USER, 'alice@example.com'); |
| 149 | +const document = createResource(ResourceTypes.DOCUMENT, '123'); |
| 150 | +const readAction = createAction(ActionNames.READ); |
| 151 | +const context = createContextWithTime({ location: 'office' }); |
| 152 | +``` |
| 153 | + |
| 154 | +### Built-in Constants |
| 155 | + |
| 156 | +```typescript |
| 157 | +// Subject types |
| 158 | +SubjectTypes.USER // 'user' |
| 159 | +SubjectTypes.SERVICE // 'service' |
| 160 | +SubjectTypes.GROUP // 'group' |
| 161 | + |
| 162 | +// Resource types |
| 163 | +ResourceTypes.DOCUMENT // 'document' |
| 164 | +ResourceTypes.API // 'api' |
| 165 | +ResourceTypes.FOLDER // 'folder' |
| 166 | + |
| 167 | +// Action names |
| 168 | +ActionNames.READ // 'can_read' |
| 169 | +ActionNames.WRITE // 'can_write' |
| 170 | +ActionNames.DELETE // 'can_delete' |
| 171 | +``` |
| 172 | + |
| 173 | +## Error Handling |
| 174 | + |
| 175 | +The client throws `AuthZenError` for API-related errors: |
| 176 | + |
| 177 | +```typescript |
| 178 | +import { AuthZenError } from 'authzen-client'; |
| 179 | + |
| 180 | +try { |
| 181 | + const response = await client.evaluate(request); |
| 182 | +} catch (error) { |
| 183 | + if (error instanceof AuthZenError) { |
| 184 | + console.error('Status:', error.status); |
| 185 | + console.error('Message:', error.message); |
| 186 | + console.error('Request ID:', error.requestId); |
| 187 | + } |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +## Node.js Usage |
| 192 | + |
| 193 | +For Node.js environments, provide a fetch implementation: |
| 194 | + |
| 195 | +```typescript |
| 196 | +import fetch from 'node-fetch'; |
| 197 | +import { AuthZenClient } from 'authzen-client'; |
| 198 | + |
| 199 | +const client = new AuthZenClient({ |
| 200 | + baseUrl: 'https://pdp.mycompany.com', |
| 201 | + fetch: fetch as any, // Provide fetch implementation |
| 202 | +}); |
| 203 | +``` |
| 204 | + |
| 205 | +## Browser Usage |
| 206 | + |
| 207 | +In browser environments, the global `fetch` API is used automatically: |
| 208 | + |
| 209 | +```typescript |
| 210 | +import { AuthZenClient } from 'authzen-client'; |
| 211 | + |
| 212 | +const client = new AuthZenClient({ |
| 213 | + baseUrl: 'https://pdp.mycompany.com', |
| 214 | + // No fetch needed - uses browser's global fetch |
| 215 | +}); |
| 216 | +``` |
| 217 | + |
| 218 | +## Advanced Examples |
| 219 | + |
| 220 | +### Complex Authorization with Rich Context |
| 221 | + |
| 222 | +```typescript |
| 223 | +const response = await client.evaluate({ |
| 224 | + subject: createSubject('user', 'alice@example.com', { |
| 225 | + department: 'Sales', |
| 226 | + role: 'Manager', |
| 227 | + clearance_level: 'confidential' |
| 228 | + }), |
| 229 | + action: createAction('can_read', { |
| 230 | + method: 'GET', |
| 231 | + api_endpoint: '/documents/123' |
| 232 | + }), |
| 233 | + resource: createResource('document', '123', { |
| 234 | + owner: 'bob@example.com', |
| 235 | + classification: 'confidential', |
| 236 | + project: 'Project Alpha' |
| 237 | + }), |
| 238 | + context: createContextWithTime({ |
| 239 | + location: 'office', |
| 240 | + device_type: 'laptop', |
| 241 | + ip_address: '192.168.1.100' |
| 242 | + }) |
| 243 | +}); |
| 244 | +``` |
| 245 | + |
| 246 | +### Batch Evaluation with Short-Circuit Logic |
| 247 | + |
| 248 | +```typescript |
| 249 | +const response = await client.evaluateBatch({ |
| 250 | + subject: createSubject('user', 'alice@example.com'), |
| 251 | + evaluations: [ |
| 252 | + { action: createAction('can_read'), resource: createResource('document', '1') }, |
| 253 | + { action: createAction('can_read'), resource: createResource('document', '2') }, |
| 254 | + { action: createAction('can_read'), resource: createResource('document', '3') } |
| 255 | + ], |
| 256 | + options: { |
| 257 | + evaluations_semantic: 'deny_on_first_deny' // Stop on first denial |
| 258 | + } |
| 259 | +}); |
| 260 | + |
| 261 | +console.log(`Evaluated ${response.evaluations.length} requests`); |
| 262 | +``` |
| 263 | + |
| 264 | +## Development |
| 265 | + |
| 266 | +### Building |
| 267 | + |
| 268 | +```bash |
| 269 | +npm run build |
| 270 | +``` |
| 271 | + |
| 272 | +### Testing |
| 273 | + |
| 274 | +```bash |
| 275 | +npm test |
| 276 | +``` |
| 277 | + |
| 278 | +### Linting |
| 279 | + |
| 280 | +```bash |
| 281 | +npm run lint |
| 282 | +``` |
| 283 | + |
| 284 | +## Contributing |
| 285 | + |
| 286 | +Contributions are welcome! Please read our contributing guidelines and submit pull requests for any improvements. |
| 287 | + |
| 288 | +## License |
| 289 | + |
| 290 | +MIT License - see LICENSE file for details. |
| 291 | + |
| 292 | +## Related |
| 293 | + |
| 294 | +- [AuthZen Specification](https://openid.github.io/authzen/) |
| 295 | +- [OpenID Foundation](https://openid.net/) |
| 296 | +- [Policy Decision Points (PDPs)](https://en.wikipedia.org/wiki/XACML#Policy_Decision_Point_(PDP)) |
| 297 | + |
| 298 | +## Changelog |
| 299 | + |
| 300 | +### 1.0.0 |
| 301 | + |
| 302 | +- Initial release |
| 303 | +- Support for Access Evaluation API |
| 304 | +- Support for Access Evaluations API (batch) |
| 305 | +- TypeScript support |
| 306 | +- Utility functions |
| 307 | +- Comprehensive documentation |
0 commit comments