Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and cleanup #34

Open
wants to merge 128 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
a3eb4a6
fix: reorganize services into their own folders
whoabuddy Dec 19, 2024
f21ce9f
fix: one more spot to update names
whoabuddy Dec 19, 2024
9fe8168
docs: add context for aider
whoabuddy Dec 20, 2024
b2f2942
refactor: Update response formats for conversations endpoints
whoabuddy Dec 20, 2024
f665597
feat: Standardize response formats and error handling in database-do.ts
whoabuddy Dec 20, 2024
51cbf7b
refactor: Standardize Agent endpoint response formats and error handling
whoabuddy Dec 20, 2024
26ec371
refactor: Standardize Task endpoint responses with consistent error a…
whoabuddy Dec 20, 2024
1dc2b71
refactor: Standardize API response structure for multiple endpoints
whoabuddy Dec 20, 2024
a029243
refactor: Standardize error and success responses in CDN and Auth DOs
whoabuddy Dec 20, 2024
9cf481e
refactor: Standardize error response formats across services
whoabuddy Dec 20, 2024
c49acd5
refactor: Standardize error responses across auth and cdn endpoints
whoabuddy Dec 20, 2024
a11b30f
refactor: Standardize endpoint responses in auth-do.ts
whoabuddy Dec 20, 2024
a3564a5
refactor: Standardize response formats in auth and cdn endpoints
whoabuddy Dec 20, 2024
d483e35
fix: adjust return type helper response
whoabuddy Dec 20, 2024
c6ddf99
docs: Update consistent endpoints documentation for createApiResponse
whoabuddy Dec 20, 2024
47b8881
refactor: Replace createJsonResponse with createApiResponse in auth-d…
whoabuddy Dec 20, 2024
635407e
refactor: Update cdn-do.ts to use createApiResponse consistently
whoabuddy Dec 20, 2024
24b8618
refactor: Update database-do.ts to use createApiResponse consistently
whoabuddy Dec 20, 2024
eda8a20
refactor: Replace createJsonResponse with createApiResponse across en…
whoabuddy Dec 20, 2024
91ea10a
refactor: Update endpoints to use createApiResponse consistently
whoabuddy Dec 20, 2024
5cd8115
feat: Update cron endpoints to use createApiResponse consistently
whoabuddy Dec 20, 2024
1cff3fc
refactor: Update profile endpoints to use createApiResponse consistently
whoabuddy Dec 20, 2024
eb601d9
refactor: Update endpoints to use createApiResponse consistently
whoabuddy Dec 20, 2024
3bc2b10
refactor: Update endpoints to use createApiResponse consistently
whoabuddy Dec 20, 2024
0f16cfb
feat: Refactor createApiResponse usage in database-do.ts endpoints
whoabuddy Dec 20, 2024
70543a8
refactor: Replace createJsonResponse with createApiResponse for error…
whoabuddy Dec 20, 2024
0b22334
refactor: Update endpoints to use createApiResponse consistently
whoabuddy Dec 20, 2024
025278f
feat: Update database-do.ts to use createApiResponse consistently
whoabuddy Dec 20, 2024
19a8a30
refactor: Update endpoints to use createApiResponse with consistent r…
whoabuddy Dec 20, 2024
47d11fe
refactor: Wrap crews data in object for consistent API response
whoabuddy Dec 20, 2024
69b5689
refactor: Update endpoints to use createApiResponse consistently
whoabuddy Dec 20, 2024
7994540
refactor: Update error response method in database-do.ts
whoabuddy Dec 20, 2024
4c4da93
refactor: Update endpoints to use createApiResponse consistently
whoabuddy Dec 20, 2024
ec561d2
refactor: Update endpoints to use createApiResponse consistently
whoabuddy Dec 20, 2024
3c06d04
refactor: Replace createJsonResponse with createApiResponse in agents…
whoabuddy Dec 20, 2024
0ca7a88
refactor: Standardize API response handling using createApiResponse
whoabuddy Dec 20, 2024
af9d3ea
refactor: Update endpoints to use createApiResponse consistently
whoabuddy Dec 20, 2024
ff47b88
refactor: Update endpoints to use createApiResponse consistently
whoabuddy Dec 20, 2024
1de7244
refactor: Replace createJsonResponse with createApiResponse and updat…
whoabuddy Dec 20, 2024
6f2571b
refactor: Replace createJsonResponse with createApiResponse for threa…
whoabuddy Dec 20, 2024
cc576e4
refactor: Update tasks endpoints to use createApiResponse consistently
whoabuddy Dec 20, 2024
6a76630
refactor: Replace createJsonResponse with createApiResponse and modif…
whoabuddy Dec 20, 2024
6676016
refactor: Standardize API responses using createApiResponse across en…
whoabuddy Dec 20, 2024
940d9a8
refactor: Update image-generator endpoints to use createApiResponse c…
whoabuddy Dec 20, 2024
01beb24
refactor: Update metadata-generator endpoints to use createApiRespons…
whoabuddy Dec 20, 2024
c7c9ed9
refactor: Update scheduler-do.ts to use createApiResponse consistently
whoabuddy Dec 20, 2024
3a4e830
refactor: Update tools-do.ts to use createApiResponse consistently
whoabuddy Dec 20, 2024
34a3088
fix: cleanup missed items and lint
whoabuddy Dec 20, 2024
e6fab57
refactor: Reorder supported CDN endpoints alphabetically
whoabuddy Dec 20, 2024
7723d5a
fix: standardize root and unrecognized route patterns
whoabuddy Dec 20, 2024
de75e95
fix: Resolve type error in createApiResponse by safely spreading data
whoabuddy Dec 20, 2024
a5c7918
refactor: Simplify createApiResponse function with inline ternary logic
whoabuddy Dec 20, 2024
f59243b
refactor: Simplify createApiResponse function with improved readability
whoabuddy Dec 20, 2024
f7f4a14
refactor: Make API response data type more flexible with `unknown`
whoabuddy Dec 20, 2024
dc2e247
docs: make a home for refs
whoabuddy Dec 20, 2024
8d0ebe2
fix: add handlers folder to split endpoints
whoabuddy Dec 20, 2024
2d54d5b
feat: Add functional handler system for conversations endpoints
whoabuddy Dec 20, 2024
a256860
refactor: Migrate conversation endpoints to handler-based system
whoabuddy Dec 20, 2024
bc51bb5
refactor: Simplify conversation endpoint handling and remove unnecess…
whoabuddy Dec 20, 2024
fc1ddd3
feat: Add crews handler with profile, public, get, create, update, an…
whoabuddy Dec 20, 2024
18a5e4a
feat: Add agents handler with CRUD operations
whoabuddy Dec 20, 2024
32df66c
refactor: Remove redundant comments in endpoint handling logic
whoabuddy Dec 20, 2024
d49acb3
feat: Add tasks handler with CRUD operations and routing
whoabuddy Dec 20, 2024
b0e6f1b
refactor: Improve code formatting and indentation in database-do.ts
whoabuddy Dec 20, 2024
df50f34
feat: Add crons handler and update database-do to use handler-based r…
whoabuddy Dec 20, 2024
1db1b05
feat: Add crew executions and steps handling to crews handler
whoabuddy Dec 20, 2024
8437d62
feat: Add profiles handler for centralized profile management
whoabuddy Dec 20, 2024
db92bb6
feat: Add Twitter handler for X Bot endpoints
whoabuddy Dec 20, 2024
024ed7e
refactor: Reorganize twitter handler placement in database-do.ts
whoabuddy Dec 20, 2024
5532a3e
refactor: Simplify endpoint handler routing by removing redundant checks
whoabuddy Dec 20, 2024
63ca8ee
feat: Add comprehensive endpoint management documentation
whoabuddy Dec 20, 2024
6414b12
feat: add handlers to separate endpoints
whoabuddy Dec 20, 2024
eb596a7
chore: cleanup and formatting
whoabuddy Dec 20, 2024
de0d137
feat: Add request validation and standardization utilities
whoabuddy Dec 20, 2024
1ffafdd
fix: apply try/catch to fetch in index
whoabuddy Dec 20, 2024
7c1b846
docs: Add comprehensive authentication system improvements documentation
whoabuddy Dec 20, 2024
eccc566
fix: remove unused alarms, documented format in #35
whoabuddy Dec 20, 2024
553bb4a
fix: use stx_address in place of profile_id
whoabuddy Dec 20, 2024
2d9ac66
docs: add analysis of database schema
whoabuddy Dec 20, 2024
4ae7f2f
feat: Add new handler definition types and documentation methods
whoabuddy Dec 23, 2024
67dab1d
refactor: Update profiles handler to use new HandlerDefinition structure
whoabuddy Dec 23, 2024
0a88eb4
refactor: Convert conversations handler to new format with endpoints
whoabuddy Dec 23, 2024
202f7bf
refactor: Convert agents handler to new HandlerDefinition format
whoabuddy Dec 23, 2024
5d78b41
refactor: Convert tasks handler to new HandlerDefinition format
whoabuddy Dec 23, 2024
f9aadfd
refactor: Convert crews handler to new HandlerDefinition format
whoabuddy Dec 23, 2024
5aa16b2
refactor: Convert crons handler to new handler definition format
whoabuddy Dec 23, 2024
4b499cd
refactor: Convert twitter handler to new handler definition format
whoabuddy Dec 23, 2024
eb1fe42
human intervention: cleanup trailing braces, remove unused type
whoabuddy Dec 23, 2024
55b058b
refactor: Update endpoint documentation and types for self-documentin…
whoabuddy Dec 23, 2024
fe9fb8e
refactor: Enhance agents and conversations handlers with authenticati…
whoabuddy Dec 23, 2024
238f41e
feat: Add authentication and documentation to crews handler endpoints
whoabuddy Dec 23, 2024
5aeabe1
feat: Add authentication and documentation to tasks handler endpoints
whoabuddy Dec 23, 2024
90bccca
feat: Add authentication and detailed documentation for crons handler…
whoabuddy Dec 23, 2024
9bf6545
feat: Add authentication and detailed documentation to profiles endpo…
whoabuddy Dec 23, 2024
b1d4b06
feat: Add authentication and detailed documentation to Twitter handle…
whoabuddy Dec 23, 2024
cb017be
refactor: Update crews handler endpoint documentation
whoabuddy Dec 23, 2024
25cae21
refactor: Update request body parameters to use camelCase
whoabuddy Dec 23, 2024
02d22cf
refactor: Convert request body parameters to camelCase across handlers
whoabuddy Dec 23, 2024
cf38aa2
style: Format agents handler with consistent trailing commas
whoabuddy Dec 23, 2024
50027d9
human intervention: code cleanup
whoabuddy Dec 23, 2024
03d2178
refactor: Add camelCase transformation for UserAgents model
whoabuddy Dec 23, 2024
03f1874
refactor: Generalize case transformation utilities with generic types
whoabuddy Dec 23, 2024
e74357a
feat: Update UserCrons model with camelCase interface and case transf…
whoabuddy Dec 23, 2024
41376e2
feat: Add camelCase interface and case transformation for UserCrews m…
whoabuddy Dec 23, 2024
cb66a86
feat: Add case transformation and interface for UserTasks model
whoabuddy Dec 23, 2024
f699664
feat: Add case transformation utilities to UserCrewExecutions model
whoabuddy Dec 23, 2024
05f0623
feat: Add case transformation utilities to UserCrewExecutionSteps model
whoabuddy Dec 23, 2024
b0cbb5a
feat: Add case transformation utilities to UserConversations model
whoabuddy Dec 23, 2024
6bd0e13
feat: Add case transformation and interface for UserProfile model
whoabuddy Dec 23, 2024
61900e2
feat: Add case transformation utilities to UserSocials model
whoabuddy Dec 23, 2024
436a4d7
feat: Add case transformation utilities to XBotAuthors model
whoabuddy Dec 23, 2024
6b39b52
feat: Add case transformation utilities to XBotThreads model
whoabuddy Dec 23, 2024
c1dc347
feat: Add case transformation utilities to XBotTweets model
whoabuddy Dec 23, 2024
a5a91ef
refactor: Update XBotLogs model with case transformation utilities
whoabuddy Dec 23, 2024
7ecb0c4
refactor: Standardize import order, comment spacing, and file structu…
whoabuddy Dec 23, 2024
fc30389
style: Standardize model interfaces and formatting across files
whoabuddy Dec 23, 2024
6cfe32d
refactor: Add updated_at field to UserCrewExecutionSteps and XBotLogs…
whoabuddy Dec 23, 2024
f6eb5ba
refactor: Rename transform functions with model-specific prefixes
whoabuddy Dec 23, 2024
ab5dfa3
refactor: Update UserAgents model with consistent indentation
whoabuddy Dec 23, 2024
38f3e20
refactor: Improve UserAgent transform functions with explicit type ma…
whoabuddy Dec 23, 2024
cd3261a
refactor: Replace generic transform functions with explicit mapping f…
whoabuddy Dec 23, 2024
ccb3002
refactor: Remove unused case transformer imports and utility file
whoabuddy Dec 23, 2024
ac6f5a6
refactor: Replace generic case transformers with explicit mapping
whoabuddy Dec 23, 2024
b4cefbc
fix: remove generic utility, unused after changes
whoabuddy Dec 23, 2024
9c2975d
refactor: Remove unused case transformer imports from model files
whoabuddy Dec 23, 2024
6437997
refactor: Improve null coalescing and optional handling in model tran…
whoabuddy Dec 23, 2024
e42a0cf
fix: Improve boolean conversion in database model transformations
whoabuddy Dec 23, 2024
486f2ce
human intervention: cleanup and consistency
whoabuddy Dec 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 228 additions & 0 deletions docs/authentication-improvements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
# Authentication System Improvements

## Overview

This document outlines improvements to our authentication system while maintaining simplicity and security. The changes focus on standardizing token handling, improving security, and following best practices.

## Key Improvements

1. Bearer Token Format
2. HTTP-Only Secure Cookies
3. Token Rotation (Access + Refresh Tokens)
4. Rate Limiting
5. CSRF Protection

## Implementation Details

### 1. Bearer Token Format

Update the Authorization header handling to use the standard Bearer format:

```typescript
// Before
Authorization: your-token-here

// After
Authorization: Bearer your-token-here
```

### 2. Token Structure

Implement a two-token system:
- Access Token: Short-lived (1 hour)
- Refresh Token: Long-lived (30 days)

```typescript
interface TokenPair {
accessToken: string; // 1 hour lifetime
refreshToken: string; // 30 days lifetime
}

interface TokenMetadata {
address: string;
type: 'access' | 'refresh';
exp: number;
}
```

### 3. Token Storage

Store tokens in KV with appropriate TTL:

```typescript
// Access token (1 hour)
await kv.put(`auth:access:${accessToken}`, JSON.stringify({
address,
type: 'access',
exp: Date.now() + 3600000
}), { expirationTtl: 3600 });

// Refresh token (30 days)
await kv.put(`auth:refresh:${refreshToken}`, JSON.stringify({
address,
type: 'refresh',
exp: Date.now() + 2592000000
}), { expirationTtl: 2592000 });
```

### 4. Cookie Security

Set secure HTTP-only cookies for both tokens:

```typescript
const setCookieHeaders = {
'Set-Cookie': [
`access_token=${accessToken}; HttpOnly; Secure; SameSite=Strict; Max-Age=3600`,
`refresh_token=${refreshToken}; HttpOnly; Secure; SameSite=Strict; Max-Age=2592000`
]
};
```

### 5. Rate Limiting

Implement rate limiting using DurableObject storage:

```typescript
const RATE_LIMIT = {
WINDOW_MS: 3600000, // 1 hour
MAX_ATTEMPTS: 5
};

async function checkRateLimit(address: string): Promise<boolean> {
const key = `ratelimit:auth:${address}`;
const attempts = await this.storage.get(key) || 0;

if (attempts >= RATE_LIMIT.MAX_ATTEMPTS) {
return false;
}

await this.storage.put(key, attempts + 1, {
expiration: Date.now() + RATE_LIMIT.WINDOW_MS
});

return true;
}
```

### 6. CSRF Protection

Add CSRF token validation for sensitive operations:

```typescript
const csrfToken = crypto.randomUUID();
await kv.put(`csrf:${csrfToken}`, address, {
expirationTtl: 3600
});

// Include in response headers
headers['X-CSRF-Token'] = csrfToken;
```

## API Endpoints

### 1. Request Auth Token

```typescript
POST /auth/request-auth-token
Content-Type: application/json
Authorization: Bearer <shared-key>

{
"signature": "...",
"publicKey": "..."
}

Response:
{
"success": true,
"data": {
"message": "Authentication successful",
"address": "ST..."
}
}
+ Set-Cookie headers
```

### 2. Refresh Token

```typescript
POST /auth/refresh
Cookie: refresh_token=<token>

Response:
{
"success": true,
"data": {
"message": "Token refreshed"
}
}
+ New Set-Cookie headers
```

### 3. Verify Token

```typescript
POST /auth/verify
Cookie: access_token=<token>

Response:
{
"success": true,
"data": {
"address": "ST...",
"exp": 1234567890
}
}
```

### 4. Revoke Token

```typescript
POST /auth/revoke
Cookie: access_token=<token>
X-CSRF-Token: <csrf-token>

Response:
{
"success": true,
"data": {
"message": "Tokens revoked"
}
}
```

## Migration Steps

1. Update `validateSharedKeyAuth` to handle Bearer format
2. Implement token rotation system
3. Add cookie-based token storage
4. Add rate limiting
5. Add CSRF protection
6. Update existing endpoints to use new token system
7. Add new endpoints (refresh, revoke)

## Security Considerations

1. Always use HTTPS
2. Set appropriate cookie security flags
3. Implement proper error handling
4. Log security-relevant events
5. Regular token cleanup
6. Monitor for suspicious activity

## Testing

1. Test token rotation
2. Verify rate limiting
3. Test CSRF protection
4. Verify cookie security
5. Test token revocation
6. Test error scenarios

## Future Improvements

1. Add token blacklisting
2. Implement device fingerprinting
3. Add audit logging
4. Add automated security monitoring
5. Implement progressive security measures
Loading