Token Utilities
The MentraOS SDK provides utility functions for creating and validating JWT tokens used for App authentication. These utilities are primarily used for implementing secure authentication mechanisms, especially for webviews.TokenUtils Namespace
createToken()
Creates a signed JWT token for App authentication.payload
: The data to include in the token payload (excluding auto-generated fields)config
: Configuration containing the secret key and optional expiration
validateToken()
Validates a JWT token using the provided secret key.token
: The JWT token string to validatesecretKey
: The secret key used to sign the token
TokenValidationResult
object with validation status and either the payload or error message
Example:
generateWebviewUrl()
Appends a JWT token as a query parameter to a base URL, making it easy to create authenticated webview URLs.baseUrl
: The base URL for the webviewtoken
: The JWT token string
extractTokenFromUrl()
Extracts the JWT token from the ‘token’ query parameter of a URL.url
: The URL string potentially containing the token
Token-Related Interfaces
AppTokenPayload
The data structure embedded within a App JWT token.TokenValidationResult
The result returned by thevalidateToken
utility function.
TokenConfig
Configuration options for creating a App token usingcreateToken
.
Token Usage in AppServer
TheAppServer
class includes a protected method for generating tokens:
AppServer
class and is useful for generating tokens within webhook handlers.
Common Token Usage Patterns
Creating a Secure Webview
Validating Tokens in a Web Application
Security Considerations
- Secret Key Management: Never expose your App secret key in client-side code. Always keep it on your server.
- Token Expiration: Set appropriate expiration times for tokens based on your security requirements.
- HTTPS: Always use HTTPS for webviews to prevent token interception.
- Validation: Always validate tokens on your server before granting access to protected resources.
- Payload Size: Keep token payloads minimal to reduce overhead and improve performance.