@superfunctions/auth 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +156 -0
  2. package/package.json +54 -0
package/README.md ADDED
@@ -0,0 +1,156 @@
1
+ # @superfunctions/auth
2
+
3
+ Framework-agnostic authentication abstraction layer for Superfunctions libraries.
4
+
5
+ ## Overview
6
+
7
+ `@superfunctions/auth` provides a standardized interface for authentication that works across any HTTP framework. Libraries can accept any auth provider conforming to this abstraction, and auth library authors can build implementations that work everywhere.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @superfunctions/auth
13
+ ```
14
+
15
+ ## Core Concepts
16
+
17
+ ### AuthProvider Interface
18
+
19
+ All auth implementations must implement the `AuthProvider` interface:
20
+
21
+ ```typescript
22
+ interface AuthProvider<TSession extends AuthSession = AuthSession> {
23
+ authenticate(request: Request): Promise<TSession | null>;
24
+ authorize?(session: TSession, resourceId: string): Promise<boolean>;
25
+ revoke?(sessionId: string): Promise<void>;
26
+ }
27
+ ```
28
+
29
+ ### AuthSession
30
+
31
+ Authentication results return an `AuthSession`:
32
+
33
+ ```typescript
34
+ interface AuthSession {
35
+ id: string; // Unique identifier
36
+ type: string; // Auth type: 'api-key', 'jwt', 'oauth', etc.
37
+ resourceIds: string[]; // Resources this session can access
38
+ scopes?: string[]; // Optional permissions
39
+ expiresAt?: Date; // Optional expiration
40
+ metadata?: any; // Optional additional data
41
+ }
42
+ ```
43
+
44
+ ## Usage
45
+
46
+ ### For Library Authors
47
+
48
+ Accept auth providers in your library:
49
+
50
+ ```typescript
51
+ import { createRouter } from '@superfunctions/http';
52
+ import { createAuthMiddleware, type AuthProvider } from '@superfunctions/auth';
53
+
54
+ export function createMyLibrary(config: {
55
+ auth?: AuthProvider;
56
+ // ... other config
57
+ }) {
58
+ const middleware = config.auth
59
+ ? [createAuthMiddleware(config.auth)]
60
+ : [];
61
+
62
+ return createRouter({
63
+ middleware,
64
+ routes: [...]
65
+ });
66
+ }
67
+ ```
68
+
69
+ ### For Auth Library Authors
70
+
71
+ Implement the `AuthProvider` interface:
72
+
73
+ ```typescript
74
+ import type { AuthProvider, AuthSession } from '@superfunctions/auth';
75
+
76
+ export function createMyAuth(config: MyAuthConfig): AuthProvider {
77
+ return {
78
+ async authenticate(request: Request): Promise<AuthSession | null> {
79
+ const token = request.headers.get('authorization');
80
+ // Validate token and return session
81
+ return {
82
+ id: 'user_123',
83
+ type: 'jwt',
84
+ resourceIds: ['project_abc'],
85
+ };
86
+ },
87
+
88
+ async authorize(session: AuthSession, resourceId: string): Promise<boolean> {
89
+ return session.resourceIds.includes(resourceId);
90
+ },
91
+ };
92
+ }
93
+ ```
94
+
95
+ ### For Application Developers
96
+
97
+ Use any conforming auth library:
98
+
99
+ ```typescript
100
+ import { createMyLibrary } from 'some-library';
101
+ import { createAuthFn } from 'authfn'; // or any other auth library
102
+
103
+ const auth = createAuthFn({
104
+ database: adapter,
105
+ // ... auth config
106
+ });
107
+
108
+ const library = createMyLibrary({
109
+ auth,
110
+ // ... other config
111
+ });
112
+ ```
113
+
114
+ ## Middleware Helpers
115
+
116
+ ### createAuthMiddleware
117
+
118
+ Creates middleware for `@superfunctions/http` routers:
119
+
120
+ ```typescript
121
+ import { createAuthMiddleware } from '@superfunctions/auth';
122
+
123
+ const authMiddleware = createAuthMiddleware(authProvider, {
124
+ skipPaths: ['/health', '/public'],
125
+ contextKey: 'auth', // default
126
+ });
127
+ ```
128
+
129
+ ### createResourceAuthMiddleware
130
+
131
+ Creates middleware for resource-level authorization:
132
+
133
+ ```typescript
134
+ import { createResourceAuthMiddleware } from '@superfunctions/auth';
135
+
136
+ const resourceAuth = createResourceAuthMiddleware(authProvider, {
137
+ resourceHeader: 'x-project-id',
138
+ contextKey: 'auth',
139
+ });
140
+ ```
141
+
142
+ ## Error Types
143
+
144
+ - `AuthError` - Base error class
145
+ - `AuthenticationError` - Authentication failed (401)
146
+ - `AuthorizationError` - Access denied (403)
147
+ - `InvalidCredentialsError` - Invalid credentials (401)
148
+ - `ExpiredCredentialsError` - Credentials expired (401)
149
+
150
+ ## Examples
151
+
152
+ See the [authfn](../../authfn) library for a complete reference implementation.
153
+
154
+ ## License
155
+
156
+ MIT
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@superfunctions/auth",
3
+ "version": "0.1.0",
4
+ "description": "Framework-agnostic authentication abstraction layer for Superfunctions libraries",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./types": {
14
+ "types": "./dist/types.d.ts"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "test": "vitest",
23
+ "test:watch": "vitest --watch",
24
+ "lint": "echo 'lint not configured'",
25
+ "typecheck": "tsc --noEmit",
26
+ "clean": "rm -rf dist"
27
+ },
28
+ "keywords": [
29
+ "auth",
30
+ "authentication",
31
+ "authorization",
32
+ "framework-agnostic",
33
+ "api-keys",
34
+ "superfunctions"
35
+ ],
36
+ "author": "21n",
37
+ "license": "MIT",
38
+ "bugs": {
39
+ "url": "https://github.com/21nCo/super-functions/issues"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/21nCo/super-functions.git",
44
+ "directory": "packages/auth"
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "devDependencies": {
50
+ "@types/node": "^22.0.0",
51
+ "typescript": "^5.6.0",
52
+ "vitest": "^3.2.4"
53
+ }
54
+ }