Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7538f04

Browse files
authored
add HMAC-SHA256 signature validation (#691)
add HMAC-SHA256 signature validation
2 parents 0a9fd41 + 36e4512 commit 7538f04

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

oauthlib/oauth1/rfc5849/endpoints/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from oauthlib.common import CaseInsensitiveDict, Request, generate_token
1414

15-
from .. import (CONTENT_TYPE_FORM_URLENCODED, SIGNATURE_HMAC, SIGNATURE_RSA,
15+
from .. import (CONTENT_TYPE_FORM_URLENCODED, SIGNATURE_HMAC_SHA1, SIGNATURE_HMAC_SHA256, SIGNATURE_RSA,
1616
SIGNATURE_TYPE_AUTH_HEADER, SIGNATURE_TYPE_BODY,
1717
SIGNATURE_TYPE_QUERY, errors, signature, utils)
1818

@@ -204,9 +204,12 @@ def _check_signature(self, request, is_token_request=False):
204204
resource_owner_secret = self.request_validator.get_access_token_secret(
205205
request.client_key, request.resource_owner_key, request)
206206

207-
if request.signature_method == SIGNATURE_HMAC:
207+
if request.signature_method == SIGNATURE_HMAC_SHA1:
208208
valid_signature = signature.verify_hmac_sha1(request,
209209
client_secret, resource_owner_secret)
210+
elif request.signature_method == SIGNATURE_HMAC_SHA256:
211+
valid_signature = signature.verify_hmac_sha256(request,
212+
client_secret, resource_owner_secret)
210213
else:
211214
valid_signature = signature.verify_plaintext(request,
212215
client_secret, resource_owner_secret)

oauthlib/oauth1/rfc5849/signature.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,36 @@ def verify_hmac_sha1(request, client_secret=None,
661661
return match
662662

663663

664+
def verify_hmac_sha256(request, client_secret=None,
665+
resource_owner_secret=None):
666+
"""Verify a HMAC-SHA256 signature.
667+
668+
Per `section 3.4`_ of the spec.
669+
670+
.. _`section 3.4`: https://tools.ietf.org/html/rfc5849#section-3.4
671+
672+
To satisfy `RFC2616 section 5.2`_ item 1, the request argument's uri
673+
attribute MUST be an absolute URI whose netloc part identifies the
674+
origin server or gateway on which the resource resides. Any Host
675+
item of the request argument's headers dict attribute will be
676+
ignored.
677+
678+
.. _`RFC2616 section 5.2`: https://tools.ietf.org/html/rfc2616#section-5.2
679+
680+
"""
681+
norm_params = normalize_parameters(request.params)
682+
bs_uri = base_string_uri(request.uri)
683+
sig_base_str = signature_base_string(request.http_method, bs_uri,
684+
norm_params)
685+
signature = sign_hmac_sha256(sig_base_str, client_secret,
686+
resource_owner_secret)
687+
match = safe_string_equals(signature, request.signature)
688+
if not match:
689+
log.debug('Verify HMAC-SHA256 failed: signature base string: %s',
690+
sig_base_str)
691+
return match
692+
693+
664694
def _prepare_key_plus(alg, keystr):
665695
if isinstance(keystr, bytes):
666696
keystr = keystr.decode('utf-8')

0 commit comments

Comments
 (0)