Files
sonr/cmd/motr/README.md
T

385 lines
9.1 KiB
Markdown
Raw Normal View History

2025-10-03 14:45:52 -04:00
# Motor WASM Service Worker - Payment Gateway & OIDC Authorization
Motor is a WebAssembly-based HTTP server that runs as a Service Worker in the browser, providing secure payment processing and OpenID Connect (OIDC) authorization without requiring backend infrastructure.
## Overview
Motor implements a comprehensive payment gateway and identity provider that runs entirely in the browser:
1. **Payment Gateway**: W3C Payment Handler API compliant payment processing with PCI DSS compliance
2. **OIDC Authorization**: Complete OpenID Connect provider with JWT token management
3. **Service Worker**: Runs as a browser service worker using go-wasm-http-server
## Features
### Payment Gateway (W3C Payment Handler API)
- ✅ Process payment transactions securely
- ✅ PCI DSS compliant card tokenization
- ✅ Card validation (Luhn algorithm, CVV, expiry)
- ✅ Transaction signing with HMAC-SHA256
- ✅ AES-256-GCM encryption for sensitive data
- ✅ Payment method validation
- ✅ Refund processing
- ✅ Comprehensive audit logging
### OIDC Authorization
- ✅ Discovery endpoint (`.well-known/openid-configuration`)
- ✅ Authorization endpoint with PKCE support
- ✅ Token endpoint with JWT generation
- ✅ UserInfo endpoint
- ✅ JWKS endpoint for key rotation
- ✅ RS256 JWT signing
- ✅ Refresh token support
### Security Features
- ✅ Rate limiting (100 requests/minute per client)
- ✅ Origin validation
- ✅ Security headers (CSP, X-Frame-Options, etc.)
- ✅ CORS configuration
- ✅ Secure token generation
- ✅ Card number masking
- ✅ Sensitive data sanitization
## API Endpoints
### Payment Gateway Endpoints
#### Process Payment
```http
POST /api/payment/process
Content-Type: application/json
{
"method": "card",
"amount": 100.00,
"currency": "USD",
"card_number": "4111111111111111",
"cvv": "123",
"expiry_month": 12,
"expiry_year": 2025,
"billing_address": {
"line1": "123 Main St",
"city": "San Francisco",
"state": "CA",
"postal_code": "94105",
"country": "US"
}
}
```
#### Validate Payment Method
```http
POST /api/payment/validate
Content-Type: application/json
{
"method": "card",
"card_number": "4111111111111111",
"cvv": "123",
"expiry_month": 12,
"expiry_year": 2025
}
```
#### Get Payment Status
```http
GET /api/payment/status/:id
```
#### Process Refund
```http
POST /api/payment/refund
Content-Type: application/json
{
"payment_id": "pay_abc123",
"amount": 50.00,
"reason": "Customer request"
}
```
#### W3C Payment Handler API
```http
GET /payment/instruments
POST /payment/canmakepayment
POST /payment/paymentrequest
```
### OIDC Endpoints
#### Discovery
```http
GET /.well-known/openid-configuration
```
#### Authorization
```http
GET /authorize?client_id=CLIENT_ID&redirect_uri=URI&response_type=code&scope=openid%20profile
```
#### Token Exchange
```http
POST /token
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=AUTH_CODE&client_id=CLIENT_ID
```
#### UserInfo
```http
GET /userinfo
Authorization: Bearer ACCESS_TOKEN
```
#### JWKS
```http
GET /.well-known/jwks.json
```
### Health & Monitoring
```http
GET /health
GET /status
```
## Building
### Using Make
```bash
# Build Motor WASM module
make motr-wasm
# Or build directly
cd cmd/motr
GOOS=js GOARCH=wasm go build -o ../../packages/es/src/plugins/motor/motor.wasm .
```
### Build Output
The WASM module is built to: `packages/es/src/plugins/motor/motor.wasm`
## Integration
### Service Worker Registration
```javascript
// motor-worker.js
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.4/misc/wasm/wasm_exec.js');
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.2.1/sw.js');
// Register Motor WASM as HTTP listener
registerWasmHTTPListener('motor.wasm', {
base: '/api'
});
```
### TypeScript Client Usage
```typescript
import { PaymentGatewayClient, OIDCClient } from '@sonr.io/es/plugins/motor';
// Initialize clients
const payment = new PaymentGatewayClient('https://localhost:3000');
const oidc = new OIDCClient('https://localhost:3000');
// Process a payment
const result = await payment.processPayment({
method: 'card',
amount: 100.00,
currency: 'USD',
card_number: '4111111111111111',
cvv: '123',
expiry_month: 12,
expiry_year: 2025
});
// OIDC authorization flow
const authUrl = await oidc.buildAuthorizationUrl({
client_id: 'my-app',
redirect_uri: 'https://myapp.com/callback',
scope: 'openid profile email'
});
// Exchange authorization code for tokens
const tokens = await oidc.exchangeCode('auth_code_here', 'code_verifier');
```
## Security Implementation
### PCI DSS Compliance
- **Tokenization**: Cards are immediately tokenized, raw data never stored
- **Encryption**: AES-256-GCM for all sensitive data at rest
- **Masking**: Card numbers always masked except last 4 digits
- **Audit Logging**: Complete audit trail for compliance
- **CVV Handling**: CVV never stored, only validated
### Transaction Security
- **Signing**: HMAC-SHA256 signatures on all transactions
- **Verification**: Signature verification before processing
- **Tamper Detection**: Any modification invalidates transaction
- **Idempotency**: Duplicate transaction prevention
### Authentication Security
- **JWT Signing**: RS256 with 2048-bit RSA keys
- **PKCE**: Proof Key for Code Exchange for authorization flow
- **Token Expiration**: Configurable expiration (default 1 hour)
- **Refresh Tokens**: Secure refresh token rotation
## Testing
### Unit Tests
```bash
# Run unit tests (without WASM constraints)
go test ./cmd/motr/...
```
### Integration Tests
```bash
# Build WASM first
make motr-wasm
# Run integration tests
cd cmd/motr
GOOS=js GOARCH=wasm go test -v
```
### Test Coverage
- ✅ Payment processing flows
- ✅ Card validation (Luhn, CVV, expiry)
- ✅ Tokenization and encryption
- ✅ Transaction signing/verification
- ✅ OIDC discovery and flows
- ✅ JWT generation/validation
- ✅ Rate limiting
- ✅ Security headers
- ✅ PCI compliance features
## Performance
### Bundle Size
- WASM module: ~3-4MB (production build)
- Service Worker: ~10KB
- TypeScript client: ~25KB (minified)
### Optimization
- Built with `-ldflags="-s -w"` for size reduction
- Gzip compression reduces transfer to ~1MB
- Lazy loading recommended for optimal performance
### Benchmarks
- Payment processing: <100ms average
- Token generation: <50ms
- Card validation: <10ms
- Encryption/decryption: <20ms
## Browser Compatibility
| Feature | Chrome | Firefox | Safari | Edge |
|---------|--------|---------|--------|------|
| Service Workers | 45+ | 44+ | 11.1+ | 17+ |
| WebAssembly | 57+ | 52+ | 11+ | 16+ |
| Payment Handler | 68+ | - | - | 79+ |
| Full Support | 68+ | 52+* | 11.1+* | 79+ |
*Payment Handler API has limited support
## Configuration
### Environment Variables
```javascript
// Configure in service worker
const config = {
issuer: 'https://motor.sonr.io',
rateLimit: 100, // requests per minute
rateWindow: 60000, // milliseconds
tokenExpiry: 3600, // seconds
allowedOrigins: ['https://localhost:3000']
};
```
### Security Settings
- Rate limiting: Configurable per-client limits
- CORS: Configurable allowed origins
- CSP: Customizable content security policy
- Token expiry: Adjustable for different use cases
## Development
### Prerequisites
- Go 1.21+ (1.23+ recommended)
- Modern browser with Service Worker support
- HTTPS or localhost (Service Workers requirement)
### Local Development
```bash
# Build WASM module
make motr-wasm
# Start local server (example)
cd packages/es/src/plugins/motor
python3 -m http.server 8080 --bind localhost
# Access at https://localhost:8080
```
### Debugging
- Browser DevTools: Network tab for API inspection
- Service Worker: Application tab for SW debugging
- Console: WASM logs and errors
- Payment Handler: chrome://settings/content/paymentHandler
## Production Deployment
### Best Practices
1. **HTTPS Required**: Service Workers only work over HTTPS
2. **Cache Strategy**: Implement proper cache headers
3. **Error Handling**: Comprehensive error logging
4. **Monitoring**: Track payment success rates
5. **Compliance**: Regular PCI DSS audits
### Deployment Checklist
- [ ] Configure production issuer URL
- [ ] Set appropriate rate limits
- [ ] Configure allowed origins
- [ ] Enable production encryption keys
- [ ] Set up monitoring and alerting
- [ ] Configure backup payment processors
- [ ] Implement fraud detection rules
- [ ] Schedule security audits
## Troubleshooting
### Common Issues
#### Service Worker Not Registering
- Ensure HTTPS or localhost
- Check browser compatibility
- Verify WASM file path
#### Payment Processing Errors
- Validate card details format
- Check rate limiting
- Verify origin is allowed
#### OIDC Flow Issues
- Ensure redirect URI matches
- Check PKCE implementation
- Verify token expiration
### Debug Mode
Enable debug logging in the service worker:
```javascript
// motor-worker.js
const DEBUG = true;
```
## License
This implementation is part of the Sonr project and follows the same license terms.
## Support
For issues, questions, or contributions:
- GitHub Issues: https://github.com/sonr-io/sonr/issues
- Documentation: https://docs.sonr.io
- Security: security@sonr.io (for security vulnerabilities)