SSO-Manager
SSO Manager Documentation
Overview
The SSO Manager provides a unified authentication flow for both web and native app platforms. It handles OAuth and token-based authentication with external SSO providers, supporting multiple trigger mechanisms including URL parameters and postMessage communication.
Singleton Pattern: The SSOManager class is a singleton, ensuring only one authentication instance exists throughout the application lifecycle.
Key Features
- Dual Platform Support: Works seamlessly on both web and native apps
- Multiple Trigger Methods: URL parameters, postMessage (iframe integration)
- Automatic State Detection: Determines login/logout requirements based on SSO provider responses
- Configurable: Highly customizable through a central configuration file
- Return URL Handling: Automatically manages return navigation after authentication
- Singleton Architecture: Built-in singleton pattern ensures consistent state management
File Structure
Installation & Setup
1. Import in custom.js
Add the import to your main custom.js file:
File: default/storefront/assets/scripts/custom/custom.js
This will automatically initialize the SSO Manager when Purple Service is ready.
2. Access the SSO Manager
Important: Due to the singleton pattern, multiple instantiations return the same instance:
3. Configure
Edit config.js to match your environment:
File: default/storefront/assets/scripts/custom/sso-manager/config.js
Configuration Reference
Core Settings
type
Type: 'oAuth' | 'transferToken' Description: Specifies which SSO procedure is active. Example: "oAuth"
withPostMessage
Type: boolean Description: Enable triggering login/logout via postMessage for iframe integration. Default: false
External State Configuration
The externalState object defines URL parameters that indicate authentication state from the SSO provider.
Structure
Each state is defined as an array: [urlParameterKey, expectedValue?]
- If expectedValue is provided, the parameter value must match exactly
- If expectedValue is omitted, any value for that parameter is considered valid
externalState.login
Type: [string, string?] Description: Indicates external login state Example: ["logged_in", "true"] - Checks if URL has ?logged_in=true
externalState.logout
Type: [string, string?] Description: Indicates external logout state Example: ["logged_in", "false"] - Checks if URL has ?logged_in=false
externalState.purchase
Type: [string, string?] Description: Indicates external purchase action completed Example: ["purchase", "true"] - Checks if URL has ?purchase=true
externalState.token
Type: [string, string?] Description: TransferToken parameter, usually declared without expected value Example: ["transferToken"] - Checks if URL has ?transferToken=<any_value>
URL Parameter Keys
Defines the URL parameter names used to trigger and handle authentication flows.
urlParamKeys.returnUrl
Type: string Description: URL parameter name under which the external API expects the return URL Example: "returnTo" - Results in ?returnTo=https://yoursite.com
urlParamKeys.loginViaUrl
Type: string Description: URL parameter which triggers login. Can receive a string value to use as targetUrl Example: "external_login" - Trigger with ?external_login or ?external_login=https://custom-sso.com
urlParamKeys.checkoutViaUrl
Type: string Description: URL parameter which triggers checkout. Can receive a string value to use as targetUrl Example: "external_checkout" - Trigger with ?external_checkout or ?external_checkout=https://custom-checkout.com
urlParamKeys.logoutViaUrl
Type: string Description: URL parameter which triggers logout Example: "external_logout" - Trigger with ?external_logout
PostMessage Configuration
postMessage.validOrigins
Type: string[] Description: Array of origins allowed to trigger SSO Manager login/logout via postMessage Security: Only messages from these origins will be processed Example:
Additional URL Parameters
targetUrlParams
Type: Record<string, string | number | boolean> Description: Additional parameters to add to the target SSO URL Example:
returnUrlParams
Type: Record<string, string | number | boolean> Description: Additional parameters to add to the return URL Example:
removeUrlParams
Type: string[] Description: URL parameters to remove upon login/logout to avoid endless loops or expose sensitive data Example:
SSO Endpoint URLs
loginUrl
Type: string Description: URL to be called for external login when none is provided in the login call Example: "https://checkout-stage.hz.de/dispatch"
logoutUrl
Type: string Description: URL to be called for logout (always used internally) Example: "https://checkout-stage.hz.de/logout"
checkoutUrl
Type: string Description: URL to be called for external checkout when none is provided in the checkout call Example: "https://checkout-stage.hz.de/start"
Configuration Examples
Basic OAuth Configuration
Configuration with PostMessage Support
Transfer Token Configuration
Usage Examples
1. Trigger Login via URL
Basic login (uses default loginUrl):
Login with custom SSO URL:
Login from a specific page:
After authentication, user returns to /premium-article.
2. Trigger Checkout via URL
Basic checkout:
Checkout with custom URL:
3. Trigger Logout via URL
4. Programmatic Login/Logout
5. PostMessage Integration (for iframes)
Enable in config:
From iframe:
Authentication Flow
Web Platform Flow
App Platform Flow
Advanced Configuration
Singleton Pattern
The SSOManager class uses a built-in singleton pattern. When you extend this class, the singleton behavior is automatically inherited:
When you extend the class in init.js, the extended class automatically inherits the singleton pattern:
For Testing:
Custom Lifecycle Hooks
Extend the class in init.js to add custom behavior. The singleton pattern is automatically inherited - no extra work needed:
File: default/storefront/assets/scripts/custom/sso-manager/init.js
That's it! The singleton pattern is inherited from the parent class, and initialization happens automatically when Purple Service initializes.
Adding Parameters to Target URL
Result: SSO URL will include these params automatically.
Adding Parameters to Return URL
Result: Return URL will include these params.
Removing Sensitive Parameters
These parameters are removed after authentication to keep URLs clean.
API Reference
Singleton Methods
SSOManager.getInstance()
Get the singleton instance. Creates a new instance if none exists.
Returns: SSOManager - The singleton instance
Example:
SSOManager.resetInstance()
Reset the singleton instance. Useful for testing or reinitialization.
Returns: void
Example:
Initialization Functions
The SSO Manager uses automatic initialization via Purple Service hooks:
No manual initialization required - the singleton is created automatically when Purple Service initializes.
Direct Access
If you need to access the singleton directly without waiting for the hook:
Public Methods
login(targetUrl?, options?)
Trigger the login flow.
Parameters:
- targetUrl (string, optional): SSO login URL. Uses config.loginUrl if omitted.
- options (object, optional):
- params (array): Additional URL parameters as [key, value] pairs
- returnUrl (string): Custom return URL for this login
Example:
checkout(targetUrl?, options?)
Trigger the checkout flow. Same signature as login().
Example:
logout()
Trigger the logout flow.
Example:
Public Properties
config
Access the configuration object.
isWeb
Boolean indicating if running on web platform.
utils
Utility functions for URL handling.
Troubleshooting
Singleton-Related Issues
Problem: Configuration changes don't take effect.
Solution: The singleton instance is created with the initial configuration. To change configuration:
Problem: Different parts of the app have different authentication states.
Solution: This shouldn't happen with the singleton pattern. If it does, verify that:
- You're not accidentally calling resetInstance() somewhere
- All parts of your app import from the same module path
- No bundler/module issues are creating duplicate instances
All instantiations should go through the same singleton.
Login Not Triggered
Problem: Clicking login link doesn't redirect to SSO.
Solutions:
- Check if loginUrl is set in config
- Verify URL parameter matches loginViaUrl in config
- Check browser console for errors
Infinite Redirect Loop
Problem: Page keeps redirecting to SSO and back.
Solutions:
- Verify externalState configuration matches SSO parameters
- Add problematic parameters to removeUrlParams array
- Check that SSO is sending expected parameter names/values
PostMessage Not Working
Problem: iframe postMessage doesn't trigger login.
Solutions:
- Enable postMessage: withPostMessage: true
- Add iframe origin to validOrigins array
- Check browser console for origin validation errors
- For non-standard protocols (resource://), ensure URL format is correct
Return URL Issues
Problem: User returns to wrong page after authentication.
Solutions:
- Check returnUrl parameter name matches SSO expectation
- Verify urlParamKeys.returnUrl is configured correctly
- Use custom return URL in login options if needed
App Authentication Fails
Problem: Authentication doesn't work in native app.
Solutions:
- Verify purple.app.performAuthentication is available
- Check callbackParamName matches app configuration
- Ensure app has proper URL scheme registration
Security Considerations
PostMessage Origin Validation
Always specify exact origins in validOrigins:
Never use wildcards in production!
Token Handling
- Tokens are passed via URL parameters and immediately processed
- URLs are cleaned after token extraction using removeUrlParams
- Tokens should be short-lived (< 5 minutes)
HTTPS Only
Always use HTTPS URLs for SSO endpoints in production:
Browser Support
- Modern Browsers: Chrome 90+, Firefox 88+, Safari 14+, Edge 90+
- Mobile: iOS Safari 14+, Chrome Mobile 90+
- Required APIs:
- URL / URLSearchParams
- window.history.replaceState
- window.addEventListener (postMessage)
- Promises / async-await
Support
For issues or questions:
- Check console logs (debug level)
- Verify configuration matches SSO provider requirements
- Review authentication flow in Network tab
- Contact your SSO provider for server-side issues