A Python library for generating Shopify multipass tokens for customer authentication
Project description
Shopify Multipass
A Python library for generating Shopify multipass tokens for customer authentication.
Overview
This library provides a simple interface to generate Shopify multipass tokens, which allow you to authenticate customers on your Shopify store without requiring them to enter their credentials. Multipass is particularly useful for integrating external systems with Shopify stores while providing a seamless user experience.
Features
- 🔐 Generate multipass tokens for customer authentication
- 🔗 Create complete login URLs with embedded tokens
- ✅ Validate email formats and Shopify domains
- 🛡️ Support for both
.myshopify.comdomains and custom domains - 📝 Comprehensive error handling with descriptive messages
- 🔍 Type hints for better IDE support
- 🧪 Extensive test coverage
Installation
Install from PyPI:
pip install shopify-multipass
Or install from source:
git clone https://github.com/yourusername/shopify-multipass.git
cd shopify-multipass
pip install -e .
Requirements
- Python 3.7+
- pycryptodome>=3.20.0
Quick Start
from shopify_multipass import MultiPass
# Initialize with your Shopify multipass secret
multipass = MultiPass("your_multipass_secret_here")
# Generate a complete login URL (most common use case)
result = multipass.generate_login_url(
email="customer@example.com",
domain="your-shop.myshopify.com",
return_to="https://your-shop.myshopify.com/pages/welcome"
)
if result["error"] == 0:
print(f"Login URL: {result['redirect']}")
else:
print(f"Error: {result['message']}")
API Reference
MultiPass Class
__init__(secret: str)
Initialize the multipass service with your Shopify multipass secret.
Parameters:
secret(str): Your Shopify multipass secret key
Raises:
ValueError: If secret is empty or None
get_token_only(email: str, return_to: str) -> dict
Generate only a multipass token for the given email and return URL.
Parameters:
email(str): Customer email addressreturn_to(str): URL to redirect to after authentication
Returns:
dict: Response with error code and token or error message# Success {"error": 0, "token": "generated_token_here"} # Error {"error": 1, "message": "Error description"}
get_login_url_with_token(token: str, domain: str) -> dict
Generate a login URL using an existing token and domain.
Parameters:
token(str): Previously generated multipass tokendomain(str): Shopify domain for multipass authentication
Returns:
dict: Response with error code and redirect URL or error message# Success {"error": 0, "redirect": "https://domain.com/account/login/multipass/token"} # Error {"error": 1, "message": "Error description"}
generate_login_url(email: str, domain: str, return_to: str) -> dict
Generate a complete multipass login URL for a customer.
Parameters:
email(str): Customer email addressdomain(str): Shopify domain for multipass authenticationreturn_to(str): URL to redirect to after authentication
Returns:
dict: Response with error code and redirect URL or error message
Usage Examples
Basic Token Generation
from shopify_multipass import MultiPass
multipass = MultiPass("your_secret_key")
# Generate just a token
token_result = multipass.get_token_only(
email="customer@example.com",
return_to="https://shop.com/welcome"
)
if token_result["error"] == 0:
token = token_result["token"]
print(f"Generated token: {token}")
Generate Login URL from Existing Token
# Use a previously generated token
url_result = multipass.get_login_url_with_token(
token="previously_generated_token",
domain="your-shop.myshopify.com"
)
if url_result["error"] == 0:
print(f"Login URL: {url_result['redirect']}")
Complete Workflow
from shopify_multipass import MultiPass
def authenticate_customer(email, shop_domain, welcome_page):
multipass = MultiPass("your_multipass_secret")
result = multipass.generate_login_url(
email=email,
domain=shop_domain,
return_to=welcome_page
)
if result["error"] == 0:
# Redirect customer to this URL
return result["redirect"]
else:
# Handle error
raise Exception(f"Authentication failed: {result['message']}")
# Usage
login_url = authenticate_customer(
email="customer@example.com",
shop_domain="mystore.myshopify.com",
welcome_page="https://mystore.myshopify.com/pages/dashboard"
)
Error Handling
All methods return a dictionary with an error field:
error: 0- Successerror: 1- Error occurred, checkmessagefield for details
Common error scenarios:
- Invalid or missing email address
- Invalid domain format
- Missing required parameters
- Encryption/signing errors
result = multipass.generate_login_url(email, domain, return_to)
if result["error"] == 1:
print(f"Error occurred: {result['message']}")
# Handle error appropriately
else:
# Use result["redirect"] or result["token"]
pass
Domain Validation
The library validates Shopify domains and supports:
- Standard Shopify domains:
shop-name.myshopify.com - Custom domains:
shop.example.com - URLs with or without protocol (automatically adds
https://)
Security Notes
- Keep your multipass secret secure - Never expose it in client-side code
- Use HTTPS - Always use secure connections for multipass URLs
- Validate return URLs - Ensure return_to URLs are trusted domains
- Token expiration - Multipass tokens have a limited lifetime (configurable in Shopify)
Testing
Run the test suite:
python -m unittest tests/test_multipass.py -v
The test suite includes:
- Token generation and validation
- URL creation and formatting
- Email and domain validation
- Error handling scenarios
- Cryptographic operations
Development
Project Structure
shopify_multipass/
├── __init__.py # Package initialization
├── multipass.py # Main MultiPass class
└── __pycache__/
tests/
├── __init__.py
└── test_multipass.py # Comprehensive test suite
requirements.txt # Dependencies
Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
-
Clone the repository:
git clone https://github.com/yourusername/shopify-multipass.git cd shopify-multipass
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install development dependencies:
pip install -e . pip install -r requirements.txt
-
Run tests:
python -m unittest tests/test_multipass.py -v
Building for PyPI
-
Install build tools:
pip install build twine
-
Build the package:
python -m build
-
Upload to PyPI:
python -m twine upload dist/*
Changelog
See CHANGELOG.md for a detailed history of changes.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Shopify Multipass Setup
To use this library, you need to:
-
Enable multipass in your Shopify admin:
- Go to Settings > Checkout
- Scroll down to "Customer accounts"
- Enable "Multipass"
- Copy your multipass secret
-
Configure your application:
from shopify_multipass import MultiPass # Use your actual multipass secret from Shopify multipass = MultiPass("your_actual_multipass_secret_from_shopify")
-
Set up your authentication flow:
# In your application def shopify_login(request): email = request.user.email shop_domain = "your-shop.myshopify.com" return_url = "https://your-shop.myshopify.com/pages/welcome" result = multipass.generate_login_url(email, shop_domain, return_url) if result["error"] == 0: return redirect(result["redirect"]) else: return JsonResponse({"error": result["message"]})
For more information about Shopify multipass, visit the Shopify documentation.
Support
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shopify_multipass_auth-0.1.1.tar.gz.
File metadata
- Download URL: shopify_multipass_auth-0.1.1.tar.gz
- Upload date:
- Size: 20.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfbc0667881aca64f027f60605c42ca91de3945d78f45ba34df3f932b2e74447
|
|
| MD5 |
dd09060bad1a987588ff1daa1dc55bcd
|
|
| BLAKE2b-256 |
04228d57a11978c3b568949bc5f1d5ae2df06375d73f1b6ad7e5c08101ee8302
|
File details
Details for the file shopify_multipass_auth-0.1.1-py3-none-any.whl.
File metadata
- Download URL: shopify_multipass_auth-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f561f627563fb0c8ac7814fb280c69a4dce20f71506c6f9bf6848cbd73fd8abe
|
|
| MD5 |
641bd846960004e3cd31f3235caa674f
|
|
| BLAKE2b-256 |
0f1ca81b15741b4bd18a80f7de04561e34462a33b1033777e52e9230b2daff46
|