This repository was archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Feature/aes signature #50
Open
YoungWing
wants to merge
15
commits into
AfterShip:master
Choose a base branch
from
YoungWing:feature/aes-signature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
04b7046
add files
YoungWing 8051beb
add aes signature
YoungWing 8bb46c8
delete api-key
YoungWing 58cf552
fix api-key header
YoungWing cf9d52e
delete dependencies lib pycryptodome
YoungWing bbd0f94
fix dependencies
YoungWing 3e3acdc
format code
YoungWing 269688d
format code
YoungWing 7f325dc
format code
YoungWing a19996a
format code
YoungWing 2392744
add test case
YoungWing 6422436
add test case
YoungWing 6faf27d
delete unused lib
YoungWing 19c1bdf
delete unused code
YoungWing e3b7a34
delete unused code
YoungWing 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 |
---|---|---|
|
@@ -4,3 +4,5 @@ | |
__version__ = '1.3.0' | ||
|
||
api_key = None | ||
|
||
api_secret = None |
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 |
---|---|---|
@@ -1,4 +1,10 @@ | ||
API_KEY_FILED_NAME = 'aftership-api-key' | ||
AS_API_KEY = 'as-api-key' | ||
AS_SIGNATURE_HMAC_SHA256 = 'as-signature-hmac-sha256' | ||
|
||
CONTENT_TYPE = "application/json" | ||
|
||
API_VERSION = "v4" | ||
API_ENDPOINT = "https://api.aftership.com/v4/" | ||
|
||
SIGNATURE_AES_HMAC_SHA256 = "AES-HMAC-SHA256" |
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,16 @@ | ||
# coding=utf-8 | ||
|
||
import os | ||
import hashlib | ||
import base64 | ||
import hmac | ||
|
||
class Hmac(): | ||
def __init__(self, api_secret): | ||
self.api_secret = api_secret | ||
|
||
def hmac_signature(self, sign_string: str) -> str: | ||
if self.api_secret is None: | ||
self.api_secret = os.getenv('AFTERSHIP_API_SECRET') | ||
signature_str = hmac.new(bytes(self.api_secret.encode()), msg=bytes(sign_string.encode()), digestmod=hashlib.sha256).digest() | ||
return base64.b64encode(signature_str).decode() |
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,71 @@ | ||
# coding=utf-8 | ||
|
||
import os | ||
import hashlib | ||
import urllib | ||
import time | ||
import json | ||
from urllib import parse | ||
from typing import Union,Text,Dict | ||
# from aftership.const import API_KEY_FILED_NAME, API_ENDPOINT, AS_SIGNATURE_HMAC_SHA256, AS_SIGNATURE_RSA_SHA256, AS_API_KEY | ||
YoungWing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
BODY = Union[Text,Dict] | ||
|
||
class SignString(): | ||
def __init__(self, api_secret:str): | ||
self.api_secret = api_secret | ||
|
||
def _gen_sign_string(self, method: str, body:BODY, content_type: str, date: str, canonicalized_as_headers: str, | ||
canonicalized_resource: str) -> str: | ||
result = '' | ||
result += method + '\n' | ||
if body: | ||
if isinstance(body, dict): | ||
body = json.dumps(body) | ||
body = hashlib.md5(body.encode()).hexdigest().upper() | ||
else: | ||
body = '' | ||
content_type = '' | ||
|
||
result += body + '\n' | ||
result += content_type + '\n' | ||
result += date + '\n' | ||
result += canonicalized_as_headers + '\n' | ||
result += canonicalized_resource | ||
|
||
return result | ||
|
||
def _get_canonicalized_as_headers(self, headers: dict) -> str: | ||
new_header = {} | ||
for k, v in headers.items(): | ||
new_key = k.lower() | ||
new_value = v.strip() | ||
new_header.update({new_key: new_value}) | ||
|
||
new_header = dict(sorted(new_header.items())) | ||
|
||
result = '\n'.join([k + ':' + v for k, v in new_header.items()]) | ||
return result | ||
|
||
def _get_canonicalized_resource(self, raw_url: str) -> str: | ||
url_parse_result = parse.urlsplit(raw_url) | ||
path = url_parse_result.path | ||
query = urllib.parse.urlencode(sorted(dict(parse.parse_qsl(url_parse_result.query)).items())) | ||
if query: | ||
path = path + '?' + query | ||
return path | ||
|
||
def gen_sign_string(self, method: str, uri: str, body: str, as_header: dict, content_type: str) -> tuple: | ||
if self.api_secret is None: | ||
self.api_secret = os.getenv('AFTERSHIP_API_SECRET') | ||
|
||
canonicalized_as_headers = self._get_canonicalized_as_headers(as_header) | ||
canonicalized_resource = self._get_canonicalized_resource(uri) | ||
|
||
date = time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime()) | ||
# date = "Tue, 29 Jun 2021 07:55:55 GMT" | ||
YoungWing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
sign_string = self._gen_sign_string(method=method, body=body, content_type=content_type, date=date, | ||
canonicalized_as_headers=canonicalized_as_headers, | ||
canonicalized_resource=canonicalized_resource) | ||
|
||
return date, sign_string |
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,21 @@ | ||
import aftership | ||
|
||
aftership.api_key = 'asak_61f86xxx' | ||
aftership.api_secret = 'assk_e8b4bxxx' | ||
|
||
|
||
def get_enabled_courier_names(): | ||
result = aftership.courier.list_couriers(signature_type="AES") | ||
courier_list = [courier['name'] for courier in result['couriers']] | ||
return courier_list | ||
|
||
|
||
def get_supported_courier_names(): | ||
result = aftership.courier.list_all_couriers(signature_type="AES") | ||
courier_list = [courier['name'] for courier in result['couriers']] | ||
return courier_list | ||
|
||
|
||
if __name__ == '__main__': | ||
enabled_couriers = get_enabled_courier_names(signature_type="AES") | ||
print(enabled_couriers) |
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 |
---|---|---|
|
@@ -3,6 +3,9 @@ | |
import pytest | ||
|
||
from aftership import courier | ||
from aftership import const | ||
|
||
body='{"source_id":"1710452100BO","name":"1710452100BO","number":"1710452100BO","currency":"EUR","status":"closed","financial_status":"paid","fulfillment_status":"fulfilled","order_total":"399","shipping_total":"0","tax_total":"63.71","discount_total":"0","subtotal":"335.29","items":[{"source_id":"230278-01","sku":"230278-01","quantity":1,"unit_weight":{"unit":"kg","value":8.06},"unit_price":"399","discount":"0","tax":"63.71","return_quantity":1,"fulfillable_quantity":1,"source_product_id":"230278-01","source_variant_id":"230278-01","title":"Cinetic Big Ball Multi Floor 2","hs_code":null,"origin_country_region":"DEU","image_urls":["https://dyson-h.assetsadobe2.com/is/image/content/dam/dyson/images/products/primary/230278-01.png"]}],"source_created_at":"2022-08-08T09:34:03.000Z","source_updated_at":"2022-08-08T09:34:03.000Z","customer":{"source_id":"1710452100BO","first_name":"MICH DE EU RETURNS","last_name":"MICH DE EU RETURNS","emails":["[email protected]"],"phones":["3745345234"]},"shipping_address":{"first_name":"Boon","address_line_1":"Albrechtstrasse","city":"Oberhausen","state":"Nordrhein-Westfalen","country_region":"DEU","postal_code":"46145","phone":"3745345234"}}' | ||
|
||
|
||
class CourierTestCase(TestCase): | ||
|
@@ -12,14 +15,34 @@ def test_get_couriers(self): | |
self.assertIn('total', resp) | ||
self.assertIn('couriers', resp) | ||
|
||
@pytest.mark.vcr() | ||
def test_aes_get_couriers(self): | ||
resp = courier.list_couriers(signature_type=const.SIGNATURE_AES_HMAC_SHA256) | ||
self.assertIn('total', resp) | ||
self.assertIn('couriers', resp) | ||
|
||
|
||
@pytest.mark.vcr() | ||
def test_get_all_couriers(self): | ||
resp = courier.list_all_couriers() | ||
self.assertIn('total', resp) | ||
self.assertIn('couriers', resp) | ||
|
||
@pytest.mark.vcr() | ||
def test_aes_get_all_couriers(self): | ||
resp = courier.list_all_couriers(signature_type=const.SIGNATURE_AES_HMAC_SHA256) | ||
self.assertIn('total', resp) | ||
self.assertIn('couriers', resp) | ||
|
||
@pytest.mark.vcr() | ||
def test_detect_courier(self): | ||
resp = courier.detect_courier(tracking={'tracking_number': '1234567890'}) | ||
resp = courier.detect_courier(tracking={"tracking_number": "1234567890"}) | ||
self.assertIn('total', resp) | ||
self.assertIn('couriers', resp) | ||
|
||
@pytest.mark.vcr() | ||
def test_aes_detect_courier(self): | ||
resp = courier.detect_courier(signature_type=const.SIGNATURE_AES_HMAC_SHA256, | ||
tracking={"tracking_number": "1234567890"}) | ||
self.assertIn('total', resp) | ||
self.assertIn('couriers', resp) |
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 |
---|---|---|
|
@@ -4,18 +4,34 @@ | |
|
||
import aftership | ||
|
||
from aftership import const | ||
|
||
class NotificationTestCase(TestCase): | ||
@pytest.mark.vcr() | ||
def test_list_notification(self): | ||
response = aftership.notification.list_notifications(tracking_id='k5lh7dy7vvqeck71p5loe011') | ||
|
||
@pytest.mark.vcr() | ||
def test_aes_list_notification(self): | ||
response = aftership.notification.list_notifications(tracking_id='k5lh7dy7vvqeck71p5loe011', | ||
signature_type=const.SIGNATURE_AES_HMAC_SHA256) | ||
|
||
@pytest.mark.vcr() | ||
def test_add_notification(self): | ||
response = aftership.notification.add_notification(tracking_id='k5lh7dy7vvqeck71p5loe011', | ||
notification={'emails': ['[email protected]']}) | ||
|
||
def test_aes_add_notification(self): | ||
response = aftership.notification.add_notification(tracking_id='k5lh7dy7vvqeck71p5loe011', | ||
notification={"emails": ["[email protected]"]}, | ||
signature_type=const.SIGNATURE_AES_HMAC_SHA256) | ||
|
||
@pytest.mark.vcr() | ||
def test_remove_notification(self): | ||
response = aftership.notification.remove_notification(tracking_id='k5lh7dy7vvqeck71p5loe011', | ||
notification={'emails': ['[email protected]']}) | ||
notification={"emails": ["[email protected]"]}) | ||
|
||
def test_aes_remove_notification(self): | ||
response = aftership.notification.remove_notification(tracking_id='k5lh7dy7vvqeck71p5loe011', | ||
notification={"emails": ["[email protected]"]}, | ||
signature_type=const.SIGNATURE_AES_HMAC_SHA256) |
Oops, something went wrong.
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.