xsrfutil.py 文件源码

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

项目:oscars2016 作者: 0x0ece 项目源码 文件源码
def validate_token(key, token, user_id, action_id="", current_time=None):
    """Validates that the given token authorizes the user for the action.

    Tokens are invalid if the time of issue is too old or if the token
    does not match what generateToken outputs (i.e. the token was forged).

    Args:
        key: secret key to use.
        token: a string of the token generated by generateToken.
        user_id: the user ID of the authenticated user.
        action_id: a string identifier of the action they requested
                   authorization for.

    Returns:
        A boolean - True if the user is authorized for the action, False
        otherwise.
    """
    if not token:
        return False
    try:
        decoded = base64.urlsafe_b64decode(token)
        token_time = int(decoded.split(DELIMITER)[-1])
    except (TypeError, ValueError, binascii.Error):
        return False
    if current_time is None:
        current_time = time.time()
    # If the token is too old it's not valid.
    if current_time - token_time > DEFAULT_TIMEOUT_SECS:
        return False

    # The given token should match the generated one with the same time.
    expected_token = generate_token(key, user_id, action_id=action_id,
                                    when=token_time)
    if len(token) != len(expected_token):
        return False

    # Perform constant time comparison to avoid timing attacks
    different = 0
    for x, y in zip(bytearray(token), bytearray(expected_token)):
        different |= x ^ y
    return not different
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号