Overview
Macro uses FusionAuth for centralized authentication and authorization. The system supports multiple authentication methods including password-based login, passwordless email login, OAuth2 (Google, GitHub), and SSO.Authentication Methods
- Password Login - Email and password authentication
- Passwordless Login - Magic link sent to email
- OAuth2 - Google and GitHub social login
- SSO - Single sign-on for enterprise customers
- Session Codes - Mobile app authentication via QR code/session exchange
All authentication flows return both an
access_token (JWT) and a refresh_token for token renewal.Token Types
The authentication system uses multiple token types:Access Token (JWT)
Short-lived JWT token used to authenticate API requests.Bearer token for API authentication. Valid for a limited time (typically 15-60 minutes).
- Token is verified using JWT public keys
- Expiration is checked on each request
- User ID and permissions are extracted from claims
Refresh Token
Long-lived token used to obtain new access tokens without re-authentication.Opaque token for refreshing access tokens. Valid for extended period (days/weeks).
Cookie-Based Auth
By default, the web application uses cookie-based authentication:- Access and refresh tokens are stored in HTTP-only cookies
- Requests automatically include credentials (
credentials: 'include') - Cookies are set on login and cleared on logout
Bearer Token Auth (Optional)
For API clients and mobile apps, bearer token authentication is supported:Authentication Flow
1. Login
Endpoint:POST /login/password
User’s email address (case-insensitive, stored lowercase)
User’s password
JWT access token for API authentication
Refresh token for obtaining new access tokens
access_token- HTTP-only, Secure, SameSite=Laxrefresh_token- HTTP-only, Secure, SameSite=Lax
2. Token Refresh
Endpoint:POST /jwt/refresh
When an access token expires, use the refresh endpoint to obtain a new token pair:
Headers:
If the access token is still valid, the same tokens are returned without calling FusionAuth.
- Client detects expired access token (401 response or JWT expiry check)
- Extract tokens from cookies or headers
- POST to
/jwt/refreshwith both tokens - FusionAuth validates refresh token and issues new token pair
- New cookies are set in response
3. Logout
Endpoint:POST /logout
Clears authentication cookies and optionally revokes tokens in FusionAuth.
Response:
- Cookies are cleared
- Returns 200 OK
FusionAuth Integration
Macro’s authentication service acts as a proxy to FusionAuth:Configuration
FusionAuth server URL (e.g.,
https://auth.macro.com)FusionAuth tenant identifier
OAuth2 client ID for the Macro application
Secret key name in AWS Secrets Manager
API key secret name for FusionAuth admin API calls
User Registration
If a user attempts to login but is not registered in FusionAuth:- User submits login credentials
- FusionAuth returns
UserNotRegisterederror - Authentication service automatically registers the user
- Login is retried with the newly registered user
This auto-registration flow ensures seamless onboarding for new users.
Email Verification
Users must verify their email before logging in:- On registration, FusionAuth sends verification email
- Unverified users receive
401 Unauthorizedwith message:"user has not verified their primary email" - Verification link redirects to
/email/verify/{token}
JWT Token Structure
Access tokens are JWTs signed with RS256 (RSA signature):Claims
Subject - User ID (UUID)
Issuer - FusionAuth tenant ID
Audience - Application client ID
Expiration timestamp (Unix epoch)
Issued at timestamp (Unix epoch)
User roles for authorization (e.g.,
["user", "admin"])Token Validation
The authentication service validates JWTs using:- Verify RS256 signature using public keys from FusionAuth
- Check token expiration (
expclaim) - Validate issuer and audience
- Extract user ID from
subclaim
OAuth2 Flows
Google OAuth
Endpoint:POST /oauth2/google
- Client initiates Google OAuth flow
- User authenticates with Google
- Google redirects with authorization code
- Service exchanges code for Google tokens
- FusionAuth links or creates user account
- Returns Macro access and refresh tokens
GitHub OAuth
Endpoint:POST /oauth2/github
Similar flow to Google OAuth:
- User authorizes GitHub access
- Service receives GitHub authorization code
- Code is exchanged for GitHub access token
- User profile is retrieved and linked to FusionAuth
- Macro tokens are issued
OAuth2 flows automatically create user accounts if they don’t exist.
Session-Based Authentication
Mobile apps use a session code flow for secure authentication:Session Creation
Endpoint:POST /session/create
- Desktop app generates session code and stores refresh token in Redis
- QR code or deep link is displayed with session code
- Mobile app scans QR code or opens deep link
- Mobile app calls
/session/login/{session_code} - Service retrieves refresh token from Redis
- New token pair is issued to mobile app
Temporary session identifier (stored in Redis with TTL)
Service-to-Service Authentication
Internal microservices use a different authentication mechanism:Internal API Key
Services authenticate with each other using a shared secret:Shared internal API key for service-to-service calls
Rate Limiting
Authentication endpoints are rate-limited to prevent abuse:- Passwordless login - Limited by email address (prevents email spam)
- Login attempts - Limited by IP address (prevents brute force)
- Token refresh - Advisory locks prevent concurrent refresh (currently disabled)
Security Best Practices
Token Storage
- Web apps: Use HTTP-only cookies (default)
- Mobile apps: Store tokens in secure platform storage (Keychain/Keystore)
- Never: Store tokens in localStorage or sessionStorage (vulnerable to XSS)
Token Rotation
- Access tokens should be refreshed before expiry
- Implement automatic token refresh in API client middleware
- Handle 401 responses by attempting refresh, then re-authenticating
HTTPS Only
- All authentication endpoints require HTTPS in production
- Cookies have
Secureflag set (HTTPS-only) - Bearer tokens should only be transmitted over HTTPS
Error Responses
Code Examples
JavaScript/TypeScript (Web)
Refresh Token Flow
Bearer Token (Mobile/API)
Next Steps
- Explore the API Overview for service architecture
- Review the Authentication Service API for specific endpoints
- Learn about user permissions and roles