简体   繁体   English

通过 HMAC-SHA1 验证 Python3 上的传入请求

[英]Verifying incoming requests on Python3 by HMAC-SHA1

I need to validate incoming request using HMAC-SHA1.我需要使用 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?是否有任何 python 库可以从请求中生成基本字符串,如果可能的话,进行验证?

From my understanding, you are not asking about OAuth 1.0 requests, you are asking about the sign and verify function, right?据我了解,您不是在询问 OAuth 1.0 请求,而是在询问有关签名和验证 function 的问题,对吧?

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如果这是您要问的,我不确定是否有任何库,但是在Authlib的代码中,有一个模块可以进行签名和验证签名: 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 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 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.您可以从 Authlib 代码中学习。 But if you are just want to send OAuth 1.0 requests, you can use Authlib directly.但是如果你只是想发送 OAuth 1.0 请求,你可以直接使用 Authlib。 Documentation is here: https://docs.authlib.org/en/latest/client/oauth1.html文档在这里: https://docs.authlib.org/en/latest/client/oauth1.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM