授权码验证(OpenID Connect 1.0)

时间:2018-07-11 13:21:32

标签: vb.net identityserver4 oidc

我尝试按照以下步骤来验证授权码:

  

验证来自授权的授权码   具有ID令牌的端点,客户端应执行以下操作:

     

1-)将带有哈希值的代码的ASCII表示形式的八位字节哈希   JWA [JWA]中为alg标头参数指定的算法   ID令牌的JOSE标头。例如,如果alg是RS256,则哈希   使用的算法是SHA-256。

     

2-)取哈希的最左半部分,然后   base64url对其进行编码。

     

3-) ID令牌中的c_hash值必须匹配   如果ID中存在c_hash,则在上一步中生成的值   令牌。

当我第一次请求授权终结点以尝试从WebForms客户端对用户进行身份验证时,我具有此CODE:

code=0655d48df75629d9fdbd5a060141bf66ca04418a0e762a6a5e6382c2748753af

我也有这个C_HASH,我可以从id_token那里得到它:

"c_hash": "QadHSCSim4aHM8q1F1F6Bg"

我正尝试通过下一个验证代码:

Private Shared Function IsValidAuthorizationCode(authorizationCode As String, stringIdTokenPayload As String) As Boolean
    Dim serializer As New JavaScriptSerializer()
    Dim BytesPayload As Byte() = Decode(stringIdTokenPayload)
    Dim stringPayload As String = System.Text.ASCIIEncoding.ASCII.GetString(BytesPayload)
    Dim deserialized_payload = serializer.Deserialize(Of Dictionary(Of String, Object))(stringPayload)
    Dim c_hash = deserialized_payload.Item("c_hash").ToString()

    Dim mySHA256 = SHA256Managed.Create()
    Dim authorizationCodeOCTETS = Decode(authorizationCode)
    Dim elemntsToIterate = mySHA256.ComputeHash(authorizationCodeOCTETS)
    Dim length = elemntsToIterate.Length
    Dim hashedCode(length/2 - 1) As Byte

    Dim count = -1
    For Each element As Byte in elemntsToIterate
        count += 1
        If count > 15 Then
            hashedCode(count - 16) = element
        End If
    Next

    Dim hashedCodeLikeString = Convert.ToBase64String(hashedCode)
    If hashedCodeLikeString.Length <> hashedCode.Length
        Return False
    Dim result As Boolean = True
    For value As Integer = 0 To hashedCodeLikeString.Length
        If (hashedCodeLikeString(value) <> hashedCode(value)) Then
            result = False
            Exit For
        End If
    Next
    Return result
End Function

但是我没有得到预期的结果。我需要获取一个TRUE值,但我得到一个FALSE。我认为我做错了什么,但我看不出它是什么。有什么帮助吗?

非常感谢您。

1 个答案:

答案 0 :(得分:1)

我不知道您的编程语言,但这是OidcClient的代码

https://github.com/IdentityModel/IdentityModel.OidcClient2

    public bool ValidateHash(string data, string hashedData, string signatureAlgorithm)
    {
        var hashAlgorithm = GetMatchingHashAlgorithm(signatureAlgorithm);

        using (hashAlgorithm)
        {
            var hash = hashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(data));

            byte[] leftPart = new byte[hashAlgorithm.HashSize / 16];
            Array.Copy(hash, leftPart, hashAlgorithm.HashSize / 16);

            var leftPartB64 = Base64Url.Encode(leftPart);
            var match = leftPartB64.Equals(hashedData);

            if (!match)
            {
                _logger.LogError($"data ({leftPartB64}) does not match hash from token ({hashedData})");
            }

            return match;
        }
    }

    public HashAlgorithm GetMatchingHashAlgorithm(string signatureAlgorithm)
    {
        var signingAlgorithmBits = int.Parse(signatureAlgorithm.Substring(signatureAlgorithm.Length - 3));

        switch (signingAlgorithmBits)
        {
            case 256:
                _logger.LogDebug("SHA256");
                return SHA256.Create();
            case 384:
                _logger.LogDebug("SHA384");
                return SHA384.Create();
            case 512:
                _logger.LogDebug("SHA512");
                return SHA512.Create();
            default:
                return null;
        }
    }