0

I need to validate incoming request using HMAC-SHA1. The main issue for me is to create the base string for it. Are there any libraries for python that can generate the base string from the request and the if its possible, made validation?

Zagorodniy Olexiy
  • 2,132
  • 3
  • 22
  • 47

1 Answers1

1

From my understanding, you are not asking about OAuth 1.0 requests, you are asking about the sign and verify function, right?

If you this is what you are asking, I'm not sure if there are any libraries, but in Authlib's code, there is a module to do sign and verify signatures: https://github.com/lepture/authlib/blob/master/authlib/oauth1/rfc5849/signature.py

Checkout:

  1. sign_hmac_sha1 https://github.com/lepture/authlib/blob/master/authlib/oauth1/rfc5849/signature.py#L350
def sign_hmac_sha1(client, request):
    """Sign a HMAC-SHA1 signature."""
    base_string = generate_signature_base_string(request)
    return hmac_sha1_signature(
        base_string, client.client_secret, client.token_secret)
  1. verify_hmac_sha1 https://github.com/lepture/authlib/blob/master/authlib/oauth1/rfc5849/signature.py#L368
def verify_hmac_sha1(request):
    """Verify a HMAC-SHA1 signature."""
    base_string = generate_signature_base_string(request)
    sig = hmac_sha1_signature(
        base_string, request.client_secret, request.token_secret)
    return hmac.compare_digest(sig, request.signature)

You can learn from Authlib code. But if you are just want to send OAuth 1.0 requests, you can use Authlib directly. Documentation is here: https://docs.authlib.org/en/latest/client/oauth1.html

lepture
  • 2,307
  • 16
  • 18
  • Thank you very much @lepture! Seems, it is what i looking for. Do you know how to install server to the Django, because i have an error when it try to use signature.py. `'Request' object has no attribute 'uri` , Thank you once again! – Zagorodniy Olexiy Jul 21 '20 at 21:34
  • that request is created by Authlib, you can find it in https://github.com/lepture/authlib/blob/master/authlib/oauth1/rfc5849/wrapper.py#L17 – lepture Jul 21 '20 at 23:13
  • Can you please show example in your answer how to install this library and call this functions. Or i can create new topic if its needed – Zagorodniy Olexiy Jul 22 '20 at 17:29
  • i've created new topic, will be appraciated if you'll help me with it https://stackoverflow.com/questions/63043331/how-to-implement-hmac-sha1-verification-in-django-using-authlib – Zagorodniy Olexiy Jul 22 '20 at 21:18