This document explains the security model of iaptic's Stripe integration and how it protects your subscription data.
Security Layers
1. Access Tokens
graph LR
A[Client] -- Access Token --> B[Iaptic]
B -- Verify --> C[Subscription]
C -- Rotate --> D[New Token]
D -- Return --> A
Access tokens are:
- Generated during checkout
- Re-issued by the server while reading purchases, when the stored receipt needs refreshing (see Token Rotation)
- Customer-specific
- Stored client-side only, under localStorage['iaptic_access_token']
2. API Keys
graph LR
A[Iaptic] -- Secret Key --> B[Stripe API]
C[Client] -- Public Key --> D[Stripe Elements]
Two types of Stripe keys:
- Secret key (server-side only)
- Public key (safe for client-side)
3. Webhook Security
graph LR
A[Stripe] -- Event --> B[Webhook]
B -- Verify Signature --> C[Process]
B -- Invalid --> D[Reject]
Each webhook request:
- Includes signature header
- Uses unique signing secret
- Validates timestamp
- Verifies payload integrity
Token Management
Access Token Format
// Access tokens are JWTs: three dot-separated parts
// Example
const accessToken = 'abcdef123456.xxx.yyy';
Token Storage
// Client-side (correct): the library stores the token for you
// under localStorage['iaptic_access_token']
const accessToken = iaptic.getAccessToken();
// Server-side (avoid)
user.accessToken = accessToken; // Don't store unencrypted
// Server-side (if needed)
user.accessToken = await encrypt(accessToken);
Token Rotation
A new access token is issued at request time, while reading purchases, when:
- The checkout session the receipt was created for has expired
- The receipt is older than 48 hours — or carries no payment map — so payment data must be reloaded from Stripe
Creating a checkout session also returns a fresh token. Nothing else issues one: a plan change and the webhook handler do not.
The library stores each new token under iaptic_access_token and notifies onAccessTokenChange() callbacks.
Secure Communication
API Requests
// Correct: Send in request body
fetch('/api/check', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ accessToken })
});
// Incorrect: Don't send in URL
fetch(`/api/check?accessToken=${accessToken}`); // Avoid
GET /v3/stripe/purchases is the exception — it takes the token as an
accessToken query parameter by design. Keep it out of the URL on your own
endpoints, where it would land in logs and referrers.
Webhook Processing
// Verify webhook signature
const event = stripe.webhooks.constructEvent(
rawBody,
signature,
webhookSecret
);
// Always verify managed_by_iaptic flag
if (event.data.object.metadata?.managed_by_iaptic !== 'true') {
return;
}
Data Protection
Sensitive Data
Never store:
- Credit card numbers
- Full card details
- CVV codes
- Unencrypted access tokens
Metadata Usage
// Safe to store in metadata
{
"application_username": "user123",
"features": "premium,api",
"tier": "enterprise"
}
// Never store in metadata
{
"accessToken": "xxx.yyy.zzz", // No!
"password": "secret", // No!
"personal_info": "private" // No!
}
Security Best Practices
1. Access Token Handling
// The library stores the access token for you under
// localStorage['iaptic_access_token'] — don't hand-roll your own copy.
// Read the stored token
const accessToken = iaptic.getAccessToken();
// Keep your backend in sync when the token is rotated
iaptic.onAccessTokenChange((accessToken) => {
// Persist the new token alongside the signed-in user
});
// Clear on sign-out
iaptic.clearStoredData();
2. Error Handling
// Don't expose internal errors
try {
await validateSubscription();
} catch (error) {
// Log internally
console.error('Validation failed:', error);
// Return safe message
throw new Error('Subscription check failed');
}
3. Version Control
// Always specify API version
const stripe = new Stripe(secretKey, {
apiVersion: '2026-01-28.clover'
});
Security Checklist
Implementation
- [ ] Use HTTPS everywhere
- [ ] Validate all inputs
- [ ] Handle token rotation
- [ ] Process webhooks securely
- [ ] Store tokens safely
- [ ] Use correct API version
Monitoring
- [ ] Watch for failed webhooks
- [ ] Monitor token rotations
- [ ] Track access patterns
- [ ] Log security events
- [ ] Alert on anomalies
Maintenance
- [ ] Update API version
- [ ] Rotate webhook secrets
- [ ] Clear stored tokens on sign-out
- [ ] Audit metadata
- [ ] Review permissions
Common Vulnerabilities
1. Token Exposure
- Logging access tokens
- Storing server-side
- Sending in URLs
- Committing to code
2. Webhook Security
- Missing signature verification
- Old API versions
- Replay attacks
- Race conditions
3. Access Control
- Incorrect entitlement strategy
- Missing validation
- Improper token rotation
- Metadata leaks
Authentication Flow
sequenceDiagram
participant C as Client
participant I as Iaptic
participant S as Stripe
C->>I: Create Checkout Session
I->>S: Create Session
S-->>I: Session + Access Token
I-->>C: Redirect URL + Token
C->>S: Complete Payment
S->>I: Webhook Event
I->>I: Validate Token
I-->>C: Confirmation
Access Token Rotation
stateDiagram-v2
[*] --> Active
Active --> Rotating: Update
Rotating --> Transitioning: New Token
Transitioning --> Active: Complete
Transitioning --> Failed: Error
Failed --> Active: Retry
