digest_auth.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:vspheretools 作者: devopshq 项目源码 文件源码
def generate_response(chaldict,uri,username,passwd,method='GET',cnonce=None):
    """
    Generate an authorization response dictionary. chaldict should contain the digest
    challenge in dict form. Use fetch_challenge to create a chaldict from a HTTPResponse
    object like this: fetch_challenge(res.getheaders()).

    returns dict (the authdict)

    Note. Use build_authorization_arg() to turn an authdict into the final Authorization
    header value.
    """
    authdict = {} 
    qop = chaldict.get('qop')
    nonce = chaldict.get('nonce')
    algorithm = chaldict.get('algorithm','MD5')
    realm = chaldict.get('realm','MD5')
    nc = "00000001"
    if not cnonce:
        cnonce = H(str(random.randint(0,10000000)))[:16]

    if algorithm.lower()=='md5-sess':
        a1 = A1(username,realm,passwd,nonce,cnonce)
    else:
        a1 = A1(username,realm,passwd)

    a2 = A2(method,uri)

    secret = H(a1)
    data = '%s:%s:%s:%s:%s' % (nonce,nc,cnonce,qop,H(a2))
    authdict['username'] = '"%s"' % username
    authdict['realm'] = '"%s"' % realm
    authdict['nonce'] = '"%s"' % nonce
    authdict['uri'] = '"%s"' % uri
    authdict['response'] = '"%s"' % KD(secret,data)
    authdict['qop'] = '"%s"' % qop
    authdict['nc'] = nc
    authdict['cnonce'] = '"%s"' % cnonce

    return authdict
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号