-
Notifications
You must be signed in to change notification settings - Fork 2
✨ Adding multipass function #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
00d5590
add multipass functions:
ponty33 64886d2
Update README.md
ponty33 21ed79b
Merge branch 'main' into feature/multipass
ponty33 4e3ce38
auto format
ponty33 3d5d1b0
update index.md
ponty33 69b009e
fix typing
ponty33 5cf4b0c
resolve conflict
ponty33 e25904b
add doc
ponty33 465907b
update doc
ponty33 c52ae25
change doc
ponty33 1d0179c
use f-string
ponty33 dc70974
change
ponty33 865b3e7
update md
ponty33 ca68e3e
changes
ponty33 ba4b8ca
change text and variable name
ponty33 a6c48a6
Merge branch 'main' into feature/multipass
ponty33 8e91da8
remove unused code
ponty33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ Shopify components managed by SPyLib: | |
* Install an app through OAuth | ||
* Session tokens | ||
* Webhooks | ||
* Multipass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ Shopify components managed by SPyLib: | |
* Install an app through OAuth | ||
* Session tokens | ||
* Webhooks | ||
* Multipass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Multipass | ||
[Shopify Multipass - Shopify Documentation](https://shopify.dev/docs/admin-api/rest/reference/plus/multipass) <br> | ||
This helper class generates token or URL that's needed for Shopify Multipass login. | ||
|
||
```python | ||
from spylib import multipass | ||
|
||
|
||
# Customer email is required to generate token or URL | ||
customer_data = {'email': 'customer@email.com'} | ||
|
||
# Generate URL | ||
url = multipass.generate_url( | ||
secret='MULTIPASS_SECRET', | ||
customer_data=customer_data, | ||
store_url='https://example.myshopify.com' | ||
) | ||
# https://example.myshopify.com/account/login/multipass/{MultipassToken} | ||
|
||
# If for some reason you need the token, you can also generate the token used in the URL separately: | ||
token = multipass.generate_token(secret='MULTIPASS_SECRET', customer_data=customer_data) | ||
|
||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import datetime | ||
import json | ||
from base64 import urlsafe_b64encode | ||
from typing import Any, Dict | ||
|
||
from Crypto.Cipher import AES | ||
from Crypto.Hash import HMAC, SHA256 | ||
from Crypto.Random import get_random_bytes | ||
|
||
|
||
def generate_token(secret: str, customer_data: Dict[str, Any]) -> bytes: | ||
key = SHA256.new(secret.encode('utf-8')).digest() | ||
encryption_key = key[0:16] | ||
signature_key = key[16:32] | ||
|
||
if 'email' not in customer_data: | ||
raise ValueError('Missing email in customer data') | ||
|
||
customer_data['created_at'] = datetime.datetime.utcnow().isoformat() | ||
cypher_text = _encrypt(encryption_key, json.dumps(customer_data)) | ||
return urlsafe_b64encode(cypher_text + _sign(signature_key, cypher_text)) | ||
|
||
|
||
def generate_url(secret: str, customer_data: Dict[str, Any], store_url) -> str: | ||
token = generate_token(secret, customer_data).decode('utf-8') | ||
return f'{store_url}/account/login/multipass/{token}' | ||
|
||
|
||
def _encrypt(encryption_key, plain_text) -> bytes: | ||
plain_text = _pad(plain_text) | ||
iv = get_random_bytes(AES.block_size) | ||
cipher = AES.new(encryption_key, AES.MODE_CBC, iv) | ||
return iv + cipher.encrypt(str.encode(plain_text)) | ||
|
||
|
||
def _sign(signature_key, cypher_text): | ||
return HMAC.new(signature_key, cypher_text, SHA256).digest() | ||
|
||
|
||
def _pad(s): | ||
return s + (AES.block_size - len(s) % AES.block_size) * chr( | ||
AES.block_size - len(s) % AES.block_size | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.