Skip to content

Instantly share code, notes, and snippets.

@realFranco
Created July 1, 2022 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realFranco/40db531aee2366929462bce9814625d3 to your computer and use it in GitHub Desktop.
Save realFranco/40db531aee2366929462bce9814625d3 to your computer and use it in GitHub Desktop.
OAuth Signature - Netsuite headers generation #oauth #netsuite #python
"""
Github: @realFranco
References:
- https://oauth.net/core/1.0/
- steps:
- normalize request parameters
- create sign key
- ceate base string
- generate key using sha1 be default or the required by the client
"""
import time
from base64 import b64encode
import hashlib
import binascii
from urllib.parse import urlencode
from random import getrandbits
from oauthlib.oauth1.rfc5849.signature import signature_base_string
import hmac
NETSUITE_CONSUMER_KEY = 'x'
NETSUITE_CONSUMER_SECRET = 'x'
NETSUITE_ACCESS_TOKEN = 'x'
NETSUITE_TOKEN_SECRET = 'x'
NETSUITE_REALM = 'x'
url = 'https://x.restlets.api.netsuite.com/app/site/hosting/restlet.nl' # Ignore any url-query
keys = {
'oauth_consumer_key': NETSUITE_CONSUMER_KEY,
'oauth_nonce': 'x', # Create a function that return stings random, maybe a random string with 11 chars on it
'oauth_signature_method': 'HMAC-SHA256',
'oauth_timestamp': '1656678297', # Use int(time.time())
'oauth_token': NETSUITE_ACCESS_TOKEN,
'oauth_version': '1.0',
'realm': NETSUITE_REALM,
# Add here any url-query from the url
# Take in mind a lexicographic order, or read the oauth standard, as reference
}
def create_base_string(keys: dict) -> str:
# Remove the "real" from the incoming properties
new_keys = keys.copy()
del new_keys['realm']
params = urlencode(new_keys)
base_string = signature_base_string(
http_method="POST",
base_str_uri=url,
normalized_encoded_request_parameters=params # ends params
)
print(base_string)
return base_string
def create_signature(sign_key, base_string) -> str:
sign_key = sign_key.encode('utf-8')
base_string = base_string.encode('utf-8')
# Netsuite is not longer support SHA1 for OAuth1 security issues
temp = hmac.new(sign_key, base_string, hashlib.sha256).hexdigest()
byte_array = b64encode(binascii.unhexlify(temp))
return byte_array.decode("utf-8")
def collect_o_auth_headers(keys: dict) -> str:
"""Collect the OAuth Netsuite credentials used on the Headers
"""
compose_out = ''
for key, value in keys.items():
compose_out += f'{key}="{value}",'
# Ignore the last comma and append the OAuth constant
compose_out = f'OAuth {compose_out[:-1]}' if compose_out else ''
headers = {'Content-Type': 'application/json', 'Authorization': compose_out}
return headers
# Create sign key
sign_key = f'{NETSUITE_CONSUMER_SECRET}&{NETSUITE_TOKEN_SECRET}' # At OAuth standard
# Creating base string
base_string = create_base_string(keys)
# Generate signature key
sig_string = create_signature(sign_key, base_string)
# Set the oauth_signature into the headers and compose the headers
keys.update({'oauth_signature': sig_string})
headers = collect_o_auth_headers(keys)
print(headers)
# At this point you can generate the HTTP request, using request, httpx or any other python module
# Thanks you for read and also thanks for the collaborators!
# Hope its helps!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment