"""
Authentication module for 1WorldSync Content1 API
This module provides authentication mechanisms for the 1WorldSync Content1 API,
including HMAC authentication as required by the API.
"""
import base64
import hashlib
import hmac
import urllib.parse
from datetime import datetime, timezone
[docs]
class Content1HMACAuth:
"""
HMAC Authentication for 1WorldSync Content1 API
This class handles the HMAC authentication process required by the 1WorldSync Content1 API.
It generates the necessary hash code based on the request parameters and secret key.
"""
[docs]
def __init__(self, app_id, secret_key, gln=None):
"""
Initialize the HMAC authentication with app_id and secret_key
Args:
app_id (str): The application ID provided by 1WorldSync
secret_key (str): The secret key provided by 1WorldSync
gln (str, optional): Global Location Number for the user
"""
self.app_id = app_id
self.secret_key = secret_key
self.gln = gln
[docs]
def generate_timestamp(self):
"""
Generate a timestamp in the format required by the 1WorldSync Content1 API
Returns:
str: Timestamp in ISO 8601 format (YYYY-MM-DDThh:mm:ssZ)
"""
return datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
[docs]
def generate_hash(self, uri):
"""
Generate a hash code for the given URI using HMAC-SHA256
Args:
uri (str): The URI to hash
Returns:
str: Base64-encoded hash code
"""
# According to the error "Hashcode mismatch", we need to ensure our hash matches
# what the server expects. Let's try without URL encoding the URI.
# Create an HMAC using SHA256 and the secret key
hash_obj = hmac.new(
bytes(self.secret_key, 'utf-8'),
bytes(uri, 'utf-8'),
hashlib.sha256
)
# Return the Base64-encoded hash
return base64.b64encode(hash_obj.digest()).decode('utf-8')