From dbd2a84d0a49336b1c249aff4916b5a0614ba224 Mon Sep 17 00:00:00 2001 From: Marcos Date: Wed, 7 Jul 2021 15:01:46 -0500 Subject: [PATCH] Update __init__.py Support for Sha 256 Signature Method --- oauth2/__init__.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index 38bf520b..b0924261 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -24,6 +24,7 @@ import base64 from hashlib import sha1 +from hashlib import sha256 import time import random import hmac @@ -848,7 +849,36 @@ def sign(self, request, consumer, token): # Calculate the digest base 64. return binascii.b2a_base64(hashed.digest())[:-1] + +class SignatureMethod_HMAC_SHA256(SignatureMethod): + name = 'HMAC-SHA256' + def signing_base(self, request, consumer, token): + if (not hasattr(request, 'normalized_url') or request.normalized_url is None): + raise ValueError("Base URL for request is not set.") + + sig = ( + escape(request.method), + escape(request.normalized_url), + escape(request.get_normalized_parameters()), + ) + + key = '%s&' % escape(consumer.secret) + if token: + key += escape(token.secret) + raw = '&'.join(sig) + return key.encode('ascii'), raw.encode('ascii') + + def sign(self, request, consumer, token): + """Builds the base signature string.""" + key, raw = self.signing_base(request, consumer, token) + + hashed = hmac.new(key, raw, sha256) + + # Calculate the digest base 64. + return binascii.b2a_base64(hashed.digest())[:-1] + + class SignatureMethod_PLAINTEXT(SignatureMethod): name = 'PLAINTEXT'