@wipal/agent-team 1.0.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.
- package/.claude/rules/common/general-rules.md +141 -0
- package/.claude/rules/lessons/lessons.md +91 -0
- package/.claude/rules/role-rules/dev-fe-rules.md +146 -0
- package/.claude/rules/role-rules/sa-rules.md +226 -0
- package/.claude/skills/SKILL-INDEX.md +299 -0
- package/.claude/skills/community/security-validator/SKILL.md +392 -0
- package/.claude/skills/core/agent-creation/SKILL.md +338 -0
- package/.claude/skills/core/code-review/SKILL.md +154 -0
- package/.claude/skills/core/git-automation/SKILL.md +93 -0
- package/.claude/skills/core/retrospect-work/SKILL.md +172 -0
- package/.claude/skills/domain/architecture/adr-writing/SKILL.md +254 -0
- package/.claude/skills/domain/architecture/adr-writing/references/adr-best-practices.md +257 -0
- package/.claude/skills/domain/architecture/adr-writing/references/adr-examples.md +246 -0
- package/.claude/skills/domain/architecture/adr-writing/references/adr-template.md +160 -0
- package/.claude/skills/domain/architecture/architecture-patterns/SKILL.md +316 -0
- package/.claude/skills/domain/architecture/architecture-patterns/references/event-driven.md +393 -0
- package/.claude/skills/domain/architecture/architecture-patterns/references/microservices.md +315 -0
- package/.claude/skills/domain/architecture/architecture-patterns/references/monolith.md +321 -0
- package/.claude/skills/domain/architecture/architecture-patterns/references/serverless.md +457 -0
- package/.claude/skills/domain/architecture/performance-engineering/SKILL.md +227 -0
- package/.claude/skills/domain/architecture/performance-engineering/references/benchmarking.md +336 -0
- package/.claude/skills/domain/architecture/performance-engineering/references/caching-strategies.md +284 -0
- package/.claude/skills/domain/architecture/performance-engineering/references/optimization.md +298 -0
- package/.claude/skills/domain/architecture/security-architecture/SKILL.md +206 -0
- package/.claude/skills/domain/architecture/security-architecture/references/auth-patterns.md +209 -0
- package/.claude/skills/domain/architecture/security-architecture/references/compliance.md +246 -0
- package/.claude/skills/domain/architecture/security-architecture/references/threat-modeling.md +219 -0
- package/.claude/skills/domain/architecture/system-design/SKILL.md +227 -0
- package/.claude/skills/domain/architecture/system-design/references/distributed-systems.md +231 -0
- package/.claude/skills/domain/architecture/system-design/references/resilience.md +344 -0
- package/.claude/skills/domain/architecture/system-design/references/scalability.md +303 -0
- package/.claude/skills/domain/architecture/tech-selection/SKILL.md +192 -0
- package/.claude/skills/domain/architecture/tech-selection/references/build-vs-buy.md +258 -0
- package/.claude/skills/domain/architecture/tech-selection/references/evaluation-framework.md +203 -0
- package/.claude/skills/domain/architecture/tech-selection/references/tech-radar.md +257 -0
- package/.claude/skills/domain/backend/api-design/SKILL.md +121 -0
- package/.claude/skills/domain/backend/database-design/SKILL.md +156 -0
- package/.claude/skills/domain/backend/performance-be/SKILL.md +210 -0
- package/.claude/skills/domain/backend/security/SKILL.md +138 -0
- package/.claude/skills/domain/backend/testing-be/SKILL.md +203 -0
- package/.claude/skills/domain/devops/ci-cd/SKILL.md +188 -0
- package/.claude/skills/domain/devops/containerization/SKILL.md +177 -0
- package/.claude/skills/domain/devops/deployment/SKILL.md +198 -0
- package/.claude/skills/domain/devops/infrastructure-as-code/SKILL.md +178 -0
- package/.claude/skills/domain/devops/monitoring/SKILL.md +163 -0
- package/.claude/skills/domain/frontend/accessibility/SKILL.md +179 -0
- package/.claude/skills/domain/frontend/frontend-design/SKILL.md +138 -0
- package/.claude/skills/domain/frontend/performance-fe/SKILL.md +195 -0
- package/.claude/skills/domain/frontend/state-management/SKILL.md +190 -0
- package/.claude/skills/domain/frontend/testing-fe/SKILL.md +193 -0
- package/.claude/skills/domain/product/requirements-gathering/SKILL.md +136 -0
- package/.claude/skills/domain/product/roadmap-planning/SKILL.md +169 -0
- package/.claude/skills/domain/product/sprint-planning/SKILL.md +151 -0
- package/.claude/skills/domain/product/stakeholder-communication/SKILL.md +162 -0
- package/.claude/skills/domain/product/user-stories/SKILL.md +141 -0
- package/.claude/skills/domain/quality/bug-reporting/SKILL.md +150 -0
- package/.claude/skills/domain/quality/regression-testing/SKILL.md +178 -0
- package/.claude/skills/domain/quality/test-automation/SKILL.md +185 -0
- package/.claude/skills/domain/quality/test-planning/SKILL.md +177 -0
- package/.claude/skills/leadership/code-review-advanced/SKILL.md +167 -0
- package/.claude/skills/leadership/mentoring/SKILL.md +151 -0
- package/.claude/skills/leadership/technical-debt/SKILL.md +166 -0
- package/.claude/skills/leadership/technical-decision/SKILL.md +160 -0
- package/.claude/skills/security-reports/.gitkeep +0 -0
- package/.claude/skills/skills-registry.yaml +441 -0
- package/README.md +232 -0
- package/bin/agent-team.js +107 -0
- package/package.json +51 -0
- package/src/commands/add.js +227 -0
- package/src/commands/init.js +136 -0
- package/src/commands/list.js +66 -0
- package/src/commands/remove.js +71 -0
- package/src/commands/switch.js +53 -0
- package/src/index.js +11 -0
- package/src/interactive/prompts.js +153 -0
- package/src/server/api/agents.js +150 -0
- package/src/server/api/roles.js +97 -0
- package/src/server/api/skills.js +79 -0
- package/src/server/index.js +78 -0
- package/src/ui/agents.html +174 -0
- package/src/ui/css/styles.css +470 -0
- package/src/ui/index.html +107 -0
- package/src/ui/roles.html +371 -0
- package/src/ui/skills.html +332 -0
- package/src/utils/file-utils.js +193 -0
- package/src/utils/skill-resolver.js +594 -0
- package/src/utils/skill-scanner.js +154 -0
- package/templates/CLAUDE.md.tmpl +42 -0
- package/templates/knowledge.md.tmpl +31 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
# Optimization Techniques
|
|
2
|
+
|
|
3
|
+
## Optimization Process
|
|
4
|
+
|
|
5
|
+
### 1. Measure First
|
|
6
|
+
```
|
|
7
|
+
"Premature optimization is the root of all evil" - Donald Knuth
|
|
8
|
+
|
|
9
|
+
Steps:
|
|
10
|
+
1. Establish baseline metrics
|
|
11
|
+
2. Identify bottlenecks
|
|
12
|
+
3. Profile to find root causes
|
|
13
|
+
4. Optimize
|
|
14
|
+
5. Measure improvement
|
|
15
|
+
6. Document
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### 2. Identify Bottlenecks
|
|
19
|
+
```
|
|
20
|
+
Common Bottlenecks (in order):
|
|
21
|
+
1. Database (most common)
|
|
22
|
+
2. Network I/O
|
|
23
|
+
3. Disk I/O
|
|
24
|
+
4. CPU
|
|
25
|
+
5. Memory
|
|
26
|
+
|
|
27
|
+
Tools:
|
|
28
|
+
- APM: DataDog, New Relic, Dynatrace
|
|
29
|
+
- Profilers: pprof, py-spy, JProfiler
|
|
30
|
+
- Database: EXPLAIN, slow query log
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Database Optimization
|
|
34
|
+
|
|
35
|
+
### Query Optimization
|
|
36
|
+
```
|
|
37
|
+
1. Use EXPLAIN
|
|
38
|
+
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 123;
|
|
39
|
+
|
|
40
|
+
Look for:
|
|
41
|
+
- Sequential scans (should use index)
|
|
42
|
+
- High row estimates
|
|
43
|
+
- Nested loops with many iterations
|
|
44
|
+
|
|
45
|
+
2. Index Strategy
|
|
46
|
+
- Index columns in WHERE clauses
|
|
47
|
+
- Composite indexes for multiple conditions
|
|
48
|
+
- Covering indexes to avoid table access
|
|
49
|
+
- Don't over-index (write performance)
|
|
50
|
+
|
|
51
|
+
3. Avoid N+1 Queries
|
|
52
|
+
# Bad
|
|
53
|
+
users = db.query("SELECT * FROM users")
|
|
54
|
+
for user in users:
|
|
55
|
+
orders = db.query("SELECT * FROM orders WHERE user_id = ?", user.id)
|
|
56
|
+
|
|
57
|
+
# Good
|
|
58
|
+
users = db.query("""
|
|
59
|
+
SELECT u.*, o.id as order_id
|
|
60
|
+
FROM users u
|
|
61
|
+
LEFT JOIN orders o ON u.id = o.user_id
|
|
62
|
+
""")
|
|
63
|
+
|
|
64
|
+
4. Pagination
|
|
65
|
+
# Bad (offset gets slower with large offset)
|
|
66
|
+
SELECT * FROM posts ORDER BY created_at LIMIT 10 OFFSET 10000
|
|
67
|
+
|
|
68
|
+
# Good (keyset pagination)
|
|
69
|
+
SELECT * FROM posts
|
|
70
|
+
WHERE created_at < '2024-01-01'
|
|
71
|
+
ORDER BY created_at DESC
|
|
72
|
+
LIMIT 10
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Connection Pooling
|
|
76
|
+
```
|
|
77
|
+
Problem: Opening DB connections is expensive
|
|
78
|
+
|
|
79
|
+
Solution: Connection Pool
|
|
80
|
+
|
|
81
|
+
┌─────────────────────────────────────────────┐
|
|
82
|
+
│ Connection Pool │
|
|
83
|
+
│ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ │
|
|
84
|
+
│ │ C │ │ C │ │ C │ │ C │ │ C │ │ C │ │
|
|
85
|
+
│ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ │
|
|
86
|
+
└─────────────────────────────────────────────┘
|
|
87
|
+
│
|
|
88
|
+
▼
|
|
89
|
+
┌─────────┐
|
|
90
|
+
│Database │
|
|
91
|
+
└─────────┘
|
|
92
|
+
|
|
93
|
+
Configuration:
|
|
94
|
+
pool_size = cpu_cores * 2 + disk_spindles
|
|
95
|
+
max_overflow = pool_size
|
|
96
|
+
pool_timeout = 30
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Application Optimization
|
|
100
|
+
|
|
101
|
+
### Algorithmic Optimization
|
|
102
|
+
```
|
|
103
|
+
Time Complexity Matters:
|
|
104
|
+
|
|
105
|
+
O(n²) for n=10,000 = 100,000,000 operations
|
|
106
|
+
O(n log n) for n=10,000 = 133,000 operations
|
|
107
|
+
|
|
108
|
+
Common Improvements:
|
|
109
|
+
- Nested loop → Hash map lookup
|
|
110
|
+
- Linear search → Binary search
|
|
111
|
+
- Repeated calculation → Memoization
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Memory Optimization
|
|
115
|
+
```
|
|
116
|
+
1. Object Pooling
|
|
117
|
+
# Bad: Create new object each time
|
|
118
|
+
def process():
|
|
119
|
+
buffer = bytearray(1024*1024) # 1MB allocation
|
|
120
|
+
...
|
|
121
|
+
|
|
122
|
+
# Good: Reuse buffer
|
|
123
|
+
buffer_pool = Pool(bytearray, args=(1024*1024,), maxsize=10)
|
|
124
|
+
|
|
125
|
+
def process():
|
|
126
|
+
buffer = buffer_pool.get()
|
|
127
|
+
try:
|
|
128
|
+
...
|
|
129
|
+
finally:
|
|
130
|
+
buffer_pool.put(buffer)
|
|
131
|
+
|
|
132
|
+
2. Streaming
|
|
133
|
+
# Bad: Load entire file
|
|
134
|
+
data = file.read() # OOM for large files
|
|
135
|
+
|
|
136
|
+
# Good: Stream processing
|
|
137
|
+
for line in file:
|
|
138
|
+
process(line)
|
|
139
|
+
|
|
140
|
+
3. Lazy Loading
|
|
141
|
+
class User:
|
|
142
|
+
@property
|
|
143
|
+
def orders(self):
|
|
144
|
+
if not hasattr(self, '_orders'):
|
|
145
|
+
self._orders = Order.query.filter_by(user_id=self.id)
|
|
146
|
+
return self._orders
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Concurrency Optimization
|
|
150
|
+
```
|
|
151
|
+
1. Async I/O
|
|
152
|
+
# Sync (blocking)
|
|
153
|
+
result1 = api.call1() # Wait 100ms
|
|
154
|
+
result2 = api.call2() # Wait 100ms
|
|
155
|
+
# Total: 200ms
|
|
156
|
+
|
|
157
|
+
# Async (concurrent)
|
|
158
|
+
result1, result2 = await asyncio.gather(
|
|
159
|
+
api.call1(),
|
|
160
|
+
api.call2()
|
|
161
|
+
)
|
|
162
|
+
# Total: 100ms
|
|
163
|
+
|
|
164
|
+
2. Thread Pools
|
|
165
|
+
with ThreadPoolExecutor(max_workers=10) as executor:
|
|
166
|
+
futures = [executor.submit(process, item) for item in items]
|
|
167
|
+
results = [f.result() for f in futures]
|
|
168
|
+
|
|
169
|
+
3. Connection Reuse
|
|
170
|
+
# Bad: New connection per request
|
|
171
|
+
def call_api():
|
|
172
|
+
conn = http.client.HTTPSConnection("api.example.com")
|
|
173
|
+
conn.request("GET", "/data")
|
|
174
|
+
...
|
|
175
|
+
|
|
176
|
+
# Good: Connection pool
|
|
177
|
+
session = requests.Session() # Reuse connections
|
|
178
|
+
def call_api():
|
|
179
|
+
return session.get("https://api.example.com/data")
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Network Optimization
|
|
183
|
+
|
|
184
|
+
### HTTP Optimization
|
|
185
|
+
```
|
|
186
|
+
1. Keep-Alive
|
|
187
|
+
Connection: keep-alive
|
|
188
|
+
Reuse TCP connections
|
|
189
|
+
|
|
190
|
+
2. Compression
|
|
191
|
+
Accept-Encoding: gzip, br
|
|
192
|
+
Brotli > Gzip for compression ratio
|
|
193
|
+
|
|
194
|
+
3. HTTP/2
|
|
195
|
+
- Multiplexing (multiple requests per connection)
|
|
196
|
+
- Header compression
|
|
197
|
+
- Server push
|
|
198
|
+
|
|
199
|
+
4. HTTP/3 (QUIC)
|
|
200
|
+
- UDP-based
|
|
201
|
+
- Faster connection establishment
|
|
202
|
+
- Better on unreliable networks
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### API Optimization
|
|
206
|
+
```
|
|
207
|
+
1. Batching
|
|
208
|
+
# Bad: Multiple requests
|
|
209
|
+
GET /users/1
|
|
210
|
+
GET /users/2
|
|
211
|
+
GET /users/3
|
|
212
|
+
|
|
213
|
+
# Good: Batch request
|
|
214
|
+
GET /users?ids=1,2,3
|
|
215
|
+
|
|
216
|
+
2. Field Selection
|
|
217
|
+
# Bad: Return all fields
|
|
218
|
+
GET /users/1
|
|
219
|
+
{id, name, email, address, phone, created_at, updated_at, ...}
|
|
220
|
+
|
|
221
|
+
# Good: Request needed fields
|
|
222
|
+
GET /users/1?fields=id,name
|
|
223
|
+
|
|
224
|
+
3. Pagination
|
|
225
|
+
GET /posts?page=1&limit=20
|
|
226
|
+
Headers: X-Total-Count: 1000
|
|
227
|
+
Link: <...page=2>; rel="next"
|
|
228
|
+
|
|
229
|
+
4. GraphQL (for complex needs)
|
|
230
|
+
query {
|
|
231
|
+
user(id: 1) {
|
|
232
|
+
name
|
|
233
|
+
email
|
|
234
|
+
posts(first: 10) {
|
|
235
|
+
title
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## Frontend Optimization
|
|
242
|
+
|
|
243
|
+
### Critical Rendering Path
|
|
244
|
+
```
|
|
245
|
+
1. Eliminate render-blocking resources
|
|
246
|
+
- Async/defer scripts
|
|
247
|
+
- Inline critical CSS
|
|
248
|
+
|
|
249
|
+
2. Minimize main thread work
|
|
250
|
+
- Code splitting
|
|
251
|
+
- Tree shaking
|
|
252
|
+
|
|
253
|
+
3. Optimize images
|
|
254
|
+
- WebP/AVIF formats
|
|
255
|
+
- Responsive images
|
|
256
|
+
- Lazy loading
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Bundle Optimization
|
|
260
|
+
```
|
|
261
|
+
1. Code Splitting
|
|
262
|
+
const LazyComponent = React.lazy(() => import('./Heavy'))
|
|
263
|
+
|
|
264
|
+
2. Tree Shaking
|
|
265
|
+
// Bad
|
|
266
|
+
import _ from 'lodash'
|
|
267
|
+
// Good
|
|
268
|
+
import debounce from 'lodash/debounce'
|
|
269
|
+
|
|
270
|
+
3. Minification
|
|
271
|
+
- Terser for JS
|
|
272
|
+
- cssnano for CSS
|
|
273
|
+
- ImageOptim for images
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Cost-Benefit Analysis
|
|
277
|
+
|
|
278
|
+
### Optimization ROI
|
|
279
|
+
```
|
|
280
|
+
Before optimizing, consider:
|
|
281
|
+
1. How much time will this save?
|
|
282
|
+
2. How many users affected?
|
|
283
|
+
3. What's the development cost?
|
|
284
|
+
4. What's the maintenance cost?
|
|
285
|
+
|
|
286
|
+
Formula:
|
|
287
|
+
ROI = (Time_saved × Users × Value_per_second) / Development_cost
|
|
288
|
+
|
|
289
|
+
Example:
|
|
290
|
+
- Optimization saves 100ms
|
|
291
|
+
- 1M requests/day
|
|
292
|
+
- $0.001 per second of user time
|
|
293
|
+
- 2 days development
|
|
294
|
+
|
|
295
|
+
ROI = (0.1s × 1M × $0.001) / (16h × $100/h)
|
|
296
|
+
= $100/day / $1600
|
|
297
|
+
= 16 days to break even
|
|
298
|
+
```
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: security-architecture
|
|
3
|
+
description: |
|
|
4
|
+
Use when designing security architecture, threat modeling, or ensuring
|
|
5
|
+
compliance. Covers authentication patterns, authorization models,
|
|
6
|
+
threat modeling (STRIDE), and compliance frameworks.
|
|
7
|
+
version: 1.0.0
|
|
8
|
+
category: security
|
|
9
|
+
tags:
|
|
10
|
+
- security
|
|
11
|
+
- threat-modeling
|
|
12
|
+
- authentication
|
|
13
|
+
- compliance
|
|
14
|
+
dependencies: []
|
|
15
|
+
references:
|
|
16
|
+
- references/threat-modeling.md
|
|
17
|
+
- references/auth-patterns.md
|
|
18
|
+
- references/compliance.md
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
# Security Architecture
|
|
22
|
+
|
|
23
|
+
## Core Principle
|
|
24
|
+
**Security by design, defense in depth, least privilege.**
|
|
25
|
+
|
|
26
|
+
## When to Use This Skill
|
|
27
|
+
|
|
28
|
+
### Trigger Conditions
|
|
29
|
+
- Designing authentication/authorization system
|
|
30
|
+
- Conducting threat modeling
|
|
31
|
+
- Planning security controls
|
|
32
|
+
- Addressing compliance requirements
|
|
33
|
+
- Reviewing architecture for security
|
|
34
|
+
|
|
35
|
+
### Keywords
|
|
36
|
+
- "security architecture"
|
|
37
|
+
- "authentication"
|
|
38
|
+
- "authorization"
|
|
39
|
+
- "threat model"
|
|
40
|
+
- "compliance"
|
|
41
|
+
- "encryption"
|
|
42
|
+
|
|
43
|
+
## Security Principles
|
|
44
|
+
|
|
45
|
+
### 1. Defense in Depth
|
|
46
|
+
```
|
|
47
|
+
Multiple layers of security:
|
|
48
|
+
|
|
49
|
+
┌─────────────────────────────────────────┐
|
|
50
|
+
│ Layer 1: Network Security (Firewall) │
|
|
51
|
+
├─────────────────────────────────────────┤
|
|
52
|
+
│ Layer 2: Application Security (WAF) │
|
|
53
|
+
├─────────────────────────────────────────┤
|
|
54
|
+
│ Layer 3: Authentication (Identity) │
|
|
55
|
+
├─────────────────────────────────────────┤
|
|
56
|
+
│ Layer 4: Authorization (RBAC/ABAC) │
|
|
57
|
+
├─────────────────────────────────────────┤
|
|
58
|
+
│ Layer 5: Data Security (Encryption) │
|
|
59
|
+
├─────────────────────────────────────────┤
|
|
60
|
+
│ Layer 6: Audit Logging │
|
|
61
|
+
└─────────────────────────────────────────┘
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 2. Least Privilege
|
|
65
|
+
```
|
|
66
|
+
Grant minimum permissions needed:
|
|
67
|
+
- Users: Only access their data
|
|
68
|
+
- Services: Only permissions for their function
|
|
69
|
+
- Admins: Limited scope, time-bound elevation
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 3. Zero Trust
|
|
73
|
+
```
|
|
74
|
+
Never trust, always verify:
|
|
75
|
+
- Verify every request
|
|
76
|
+
- Authenticate every service
|
|
77
|
+
- Encrypt all communication
|
|
78
|
+
- Assume breach
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Authentication Patterns
|
|
82
|
+
|
|
83
|
+
### OAuth 2.0 / OIDC
|
|
84
|
+
```
|
|
85
|
+
┌─────────┐ ┌─────────┐ ┌─────────┐
|
|
86
|
+
│ User │────▶│ Auth │────▶│ App │
|
|
87
|
+
│ │ │ Server │ │ │
|
|
88
|
+
└─────────┘ └────┬────┘ └─────────┘
|
|
89
|
+
│
|
|
90
|
+
Access Token
|
|
91
|
+
│
|
|
92
|
+
▼
|
|
93
|
+
┌─────────┐
|
|
94
|
+
│ API │
|
|
95
|
+
└─────────┘
|
|
96
|
+
|
|
97
|
+
Flows:
|
|
98
|
+
- Authorization Code (web apps)
|
|
99
|
+
- PKCE (mobile/SPA)
|
|
100
|
+
- Client Credentials (service-to-service)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### JWT Pattern
|
|
104
|
+
```
|
|
105
|
+
JWT Structure:
|
|
106
|
+
Header.Payload.Signature
|
|
107
|
+
|
|
108
|
+
Best Practices:
|
|
109
|
+
- Short expiration (15-60 min)
|
|
110
|
+
- Refresh tokens for longevity
|
|
111
|
+
- Validate signature always
|
|
112
|
+
- Check claims (iss, aud, exp)
|
|
113
|
+
- Don't store sensitive data
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Authorization Models
|
|
117
|
+
|
|
118
|
+
### RBAC (Role-Based)
|
|
119
|
+
```
|
|
120
|
+
Roles → Permissions
|
|
121
|
+
|
|
122
|
+
Admin → [read:*, write:*, delete:*]
|
|
123
|
+
Editor → [read:*, write:*]
|
|
124
|
+
Viewer → [read:*]
|
|
125
|
+
|
|
126
|
+
User → Role → Permissions
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### ABAC (Attribute-Based)
|
|
130
|
+
```
|
|
131
|
+
Attributes determine access:
|
|
132
|
+
|
|
133
|
+
User: department=HR, level=manager
|
|
134
|
+
Resource: type=salary_data
|
|
135
|
+
Policy: department=HR AND level>employee
|
|
136
|
+
|
|
137
|
+
More flexible, more complex
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Threat Modeling (STRIDE)
|
|
141
|
+
|
|
142
|
+
| Threat | Description | Mitigation |
|
|
143
|
+
|--------|-------------|------------|
|
|
144
|
+
| **S**poofing | Impersonation | Authentication |
|
|
145
|
+
| **T**ampering | Data modification | Integrity checks |
|
|
146
|
+
| **R**epudiation | Deny actions | Audit logs |
|
|
147
|
+
| **I**nfo Disclosure | Data leak | Encryption |
|
|
148
|
+
| **D**enial of Service | Availability | Rate limiting |
|
|
149
|
+
| **E**levation of Privilege | Unauthorized access | Authorization |
|
|
150
|
+
|
|
151
|
+
## Security Checklist
|
|
152
|
+
|
|
153
|
+
### Authentication
|
|
154
|
+
- [ ] Strong password policy
|
|
155
|
+
- [ ] Multi-factor authentication
|
|
156
|
+
- [ ] Secure password recovery
|
|
157
|
+
- [ ] Session management
|
|
158
|
+
- [ ] Token revocation
|
|
159
|
+
|
|
160
|
+
### Authorization
|
|
161
|
+
- [ ] Role-based access control
|
|
162
|
+
- [ ] Resource-level permissions
|
|
163
|
+
- [ ] API authorization
|
|
164
|
+
- [ ] Admin access controls
|
|
165
|
+
|
|
166
|
+
### Data Protection
|
|
167
|
+
- [ ] Encryption at rest
|
|
168
|
+
- [ ] Encryption in transit (TLS)
|
|
169
|
+
- [ ] Key management
|
|
170
|
+
- [ ] Data classification
|
|
171
|
+
|
|
172
|
+
### API Security
|
|
173
|
+
- [ ] Input validation
|
|
174
|
+
- [ ] Rate limiting
|
|
175
|
+
- [ ] API keys/tokens
|
|
176
|
+
- [ ] CORS configuration
|
|
177
|
+
|
|
178
|
+
### Infrastructure
|
|
179
|
+
- [ ] Network segmentation
|
|
180
|
+
- [ ] Firewall rules
|
|
181
|
+
- [ ] VPN/Bastion hosts
|
|
182
|
+
- [ ] Patch management
|
|
183
|
+
|
|
184
|
+
## Output
|
|
185
|
+
|
|
186
|
+
When designing security:
|
|
187
|
+
1. **Threat Model** - STRIDE analysis
|
|
188
|
+
2. **Security Architecture** - Diagram with controls
|
|
189
|
+
3. **Security ADR** - Key decisions
|
|
190
|
+
4. **Compliance Checklist** - Requirements mapped
|
|
191
|
+
|
|
192
|
+
## Rules
|
|
193
|
+
|
|
194
|
+
### DO
|
|
195
|
+
- ✅ Threat model every design
|
|
196
|
+
- ✅ Encrypt sensitive data
|
|
197
|
+
- ✅ Log security events
|
|
198
|
+
- ✅ Validate all input
|
|
199
|
+
- ✅ Use proven libraries
|
|
200
|
+
|
|
201
|
+
### DON'T
|
|
202
|
+
- ❌ Roll your own crypto
|
|
203
|
+
- ❌ Store secrets in code
|
|
204
|
+
- ❌ Trust user input
|
|
205
|
+
- ❌ Skip security review
|
|
206
|
+
- ❌ Ignore compliance
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# Authentication & Authorization Patterns
|
|
2
|
+
|
|
3
|
+
## Authentication Patterns
|
|
4
|
+
|
|
5
|
+
### Session-Based Auth
|
|
6
|
+
```
|
|
7
|
+
┌─────────┐ ┌─────────┐
|
|
8
|
+
│ Client │──── Login ──────────▶│ Server │
|
|
9
|
+
│ │ │ │
|
|
10
|
+
│ │◀─── Set-Cookie ──────│ │
|
|
11
|
+
│ │ (session_id) │ │
|
|
12
|
+
│ │ │ │
|
|
13
|
+
│ │──── Request + ──────▶│ │
|
|
14
|
+
│ │ Cookie │ │
|
|
15
|
+
│ │ │ │
|
|
16
|
+
│ │◀─── Response ────────│ │
|
|
17
|
+
└─────────┘ └─────────┘
|
|
18
|
+
|
|
19
|
+
Pros: Simple, server-controlled
|
|
20
|
+
Cons: Server state, scaling challenges
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Token-Based Auth (JWT)
|
|
24
|
+
```
|
|
25
|
+
┌─────────┐ ┌─────────┐
|
|
26
|
+
│ Client │──── Login ──────────▶│ Auth │
|
|
27
|
+
│ │ │ Server │
|
|
28
|
+
│ │◀─── JWT Token ───────│ │
|
|
29
|
+
│ │ └─────────┘
|
|
30
|
+
│ │
|
|
31
|
+
│ │──── Request + ──────▶┌─────────┐
|
|
32
|
+
│ │ Bearer Token │ API │
|
|
33
|
+
│ │ │ Server │
|
|
34
|
+
│ │◀─── Response ────────│ │
|
|
35
|
+
└─────────┘ └─────────┘
|
|
36
|
+
|
|
37
|
+
Pros: Stateless, scalable
|
|
38
|
+
Cons: Token revocation complex
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### OAuth 2.0 Flows
|
|
42
|
+
|
|
43
|
+
#### Authorization Code Flow (Web Apps)
|
|
44
|
+
```
|
|
45
|
+
1. User → App: Click "Login with Google"
|
|
46
|
+
2. App → User: Redirect to Google
|
|
47
|
+
3. User → Google: Authenticate
|
|
48
|
+
4. Google → User: Redirect with auth code
|
|
49
|
+
5. User → App: Auth code
|
|
50
|
+
6. App → Google: Exchange code for token
|
|
51
|
+
7. Google → App: Access token + Refresh token
|
|
52
|
+
8. App → API: Request with token
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### PKCE Flow (Mobile/SPA)
|
|
56
|
+
```
|
|
57
|
+
Adds code_verifier to Authorization Code:
|
|
58
|
+
1. Generate code_verifier (random string)
|
|
59
|
+
2. Create code_challenge = SHA256(verifier)
|
|
60
|
+
3. Send challenge with auth request
|
|
61
|
+
4. Exchange code + verifier for token
|
|
62
|
+
5. Server verifies challenge matches
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
#### Client Credentials Flow (Service-to-Service)
|
|
66
|
+
```
|
|
67
|
+
┌─────────────┐ ┌─────────────┐
|
|
68
|
+
│ Service │─── client_id/secret─▶│ Auth │
|
|
69
|
+
│ A │ │ Server │
|
|
70
|
+
│ │◀──── access_token ───│ │
|
|
71
|
+
│ │ └─────────────┘
|
|
72
|
+
│ │
|
|
73
|
+
│ │──── Request + ──────▶┌─────────────┐
|
|
74
|
+
│ │ Bearer Token │ Service │
|
|
75
|
+
│ │ │ B │
|
|
76
|
+
└─────────────┘ └─────────────┘
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### OIDC (OpenID Connect)
|
|
80
|
+
```
|
|
81
|
+
Adds identity layer to OAuth 2.0:
|
|
82
|
+
|
|
83
|
+
Standard Claims:
|
|
84
|
+
- sub: Subject (user ID)
|
|
85
|
+
- name: Full name
|
|
86
|
+
- email: Email address
|
|
87
|
+
- picture: Profile picture
|
|
88
|
+
|
|
89
|
+
ID Token (JWT):
|
|
90
|
+
{
|
|
91
|
+
"iss": "https://auth.example.com",
|
|
92
|
+
"sub": "user123",
|
|
93
|
+
"aud": "client_id",
|
|
94
|
+
"exp": 1234567890,
|
|
95
|
+
"iat": 1234560000,
|
|
96
|
+
"name": "John Doe",
|
|
97
|
+
"email": "john@example.com"
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Authorization Patterns
|
|
102
|
+
|
|
103
|
+
### Role-Based Access Control (RBAC)
|
|
104
|
+
```
|
|
105
|
+
┌─────────┐ ┌─────────┐ ┌─────────────┐
|
|
106
|
+
│ User │────▶│ Role │────▶│ Permission │
|
|
107
|
+
└─────────┘ └─────────┘ └─────────────┘
|
|
108
|
+
|
|
109
|
+
Example:
|
|
110
|
+
User: alice
|
|
111
|
+
Roles: [admin, developer]
|
|
112
|
+
Permissions:
|
|
113
|
+
admin → [users:read, users:write, system:config]
|
|
114
|
+
developer → [code:read, code:write, deploy:staging]
|
|
115
|
+
|
|
116
|
+
Implementation:
|
|
117
|
+
def check_permission(user, action, resource):
|
|
118
|
+
for role in user.roles:
|
|
119
|
+
if action in role.permissions:
|
|
120
|
+
return True
|
|
121
|
+
return False
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Attribute-Based Access Control (ABAC)
|
|
125
|
+
```
|
|
126
|
+
Policy-based authorization:
|
|
127
|
+
|
|
128
|
+
Policy:
|
|
129
|
+
ALLOW IF user.department == resource.department
|
|
130
|
+
AND user.clearance >= resource.classification
|
|
131
|
+
|
|
132
|
+
Attributes:
|
|
133
|
+
- User: department, clearance, role
|
|
134
|
+
- Resource: department, classification, owner
|
|
135
|
+
- Environment: time, location, device
|
|
136
|
+
|
|
137
|
+
Implementation:
|
|
138
|
+
def evaluate_policy(user, resource, action, env):
|
|
139
|
+
policy = get_policy(action, resource.type)
|
|
140
|
+
return policy.evaluate(user, resource, env)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Resource-Based Authorization
|
|
144
|
+
```
|
|
145
|
+
Check access to specific resource:
|
|
146
|
+
|
|
147
|
+
def can_access_document(user, document):
|
|
148
|
+
if user.is_admin:
|
|
149
|
+
return True
|
|
150
|
+
if document.owner_id == user.id:
|
|
151
|
+
return True
|
|
152
|
+
if user.id in document.shared_with:
|
|
153
|
+
return True
|
|
154
|
+
return False
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Best Practices
|
|
158
|
+
|
|
159
|
+
### Password Storage
|
|
160
|
+
```
|
|
161
|
+
✅ Use strong hashing:
|
|
162
|
+
- bcrypt (cost factor 12+)
|
|
163
|
+
- argon2id (preferred)
|
|
164
|
+
- scrypt
|
|
165
|
+
|
|
166
|
+
❌ Never use:
|
|
167
|
+
- MD5, SHA1 (broken)
|
|
168
|
+
- Plain text
|
|
169
|
+
- Reversible encryption
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Session Management
|
|
173
|
+
```
|
|
174
|
+
- Generate cryptographically random session IDs
|
|
175
|
+
- Set secure cookie flags: HttpOnly, Secure, SameSite
|
|
176
|
+
- Implement session timeout
|
|
177
|
+
- Regenerate session after login
|
|
178
|
+
- Store minimal session data
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Token Security
|
|
182
|
+
```
|
|
183
|
+
JWT Best Practices:
|
|
184
|
+
- Short expiration (15-60 min)
|
|
185
|
+
- Use refresh tokens for longer sessions
|
|
186
|
+
- Validate signature on every request
|
|
187
|
+
- Check all claims (iss, aud, exp, iat)
|
|
188
|
+
- Implement token revocation list
|
|
189
|
+
- Don't store sensitive data in JWT
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### MFA Implementation
|
|
193
|
+
```
|
|
194
|
+
Factors:
|
|
195
|
+
1. Something you know (password)
|
|
196
|
+
2. Something you have (phone, token)
|
|
197
|
+
3. Something you are (biometric)
|
|
198
|
+
|
|
199
|
+
Common Methods:
|
|
200
|
+
- TOTP (Time-based OTP)
|
|
201
|
+
- SMS OTP (less secure)
|
|
202
|
+
- Push notifications
|
|
203
|
+
- Hardware keys (YubiKey)
|
|
204
|
+
|
|
205
|
+
Implementation:
|
|
206
|
+
- Require MFA for sensitive operations
|
|
207
|
+
- Offer backup codes
|
|
208
|
+
- Handle device loss gracefully
|
|
209
|
+
```
|