Exemplo n.º 1
0
        public void  CalculateMacWithPayloadTests()
        {
            var hash = HawkCrypto.CalculatePayloadHash("text/plain", "Thank you for flying Hawk");

            Assert.Equal("Yi9LfIIFRtBEPt74PVmbTF/xVAwPn7ub15ePICfgnuY=", hash);

            var mac = HawkCrypto.CalculateMac(Key, 1353832234, "j4h3g2", "POST", "/resource/1?b=1&a=2", "example.com", 8000, hash, "some-app-ext-data");

            Assert.Equal("aSe1DERmZuRl3pI36/9BdZmnErTw3sNzOOAUlfeKjVw=", mac);
        }
Exemplo n.º 2
0
        public async Task SignAsync(HttpRequestMessage request, string?ext = null, bool requirePayloadHash = false)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            request.Headers.Host ??= request.RequestUri?.Host;

            var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            var nonce     = HawkCrypto.RandomString();
            var hash      = "";

            if (requirePayloadHash || _credential.RequirePayloadHash)
            {
                if (request.Content == null)
                {
                    throw new ArgumentNullException(nameof(request));
                }

                var contentType = request.Content.Headers.ContentType?.MediaType;
                if (string.IsNullOrWhiteSpace(contentType))
                {
                    throw new NullReferenceException(nameof(contentType));
                }

                hash = HawkCrypto.CalculatePayloadHash(contentType, await request.Content.ReadAsStringAsync().ConfigureAwait(false));
            }

            if (request.RequestUri == null)
            {
                throw new NullReferenceException();
            }

            var mac = HawkCrypto.CalculateMac(_credential.Key, timestamp, nonce, request.Method.Method, request.RequestUri.PathAndQuery, request.RequestUri.Host, request.RequestUri.Port, hash, ext);

            var signature = new HawkSignature
            {
                KeyId     = _credential.KeyId,
                Timestamp = timestamp,
                Nonce     = nonce,
                Hash      = hash,
                Ext       = ext,
                Mac       = mac
            };

            var header = $"{HawkConstants.AuthenticationScheme} {signature}";

            request.Headers.TryAddWithoutValidation("Authorization", header);
        }
Exemplo n.º 3
0
        public void  CalculateMacTests()
        {
            var mac = HawkCrypto.CalculateMac(Key, 1353832234, "j4h3g2", "GET", "/resource/1?b=1&a=2", "example.com", 8000, null, "some-app-ext-data");

            Assert.Equal("6R4rV5iE+NPoym+WwjeHzjAGXUtLNIxmo1vpMofpLAE=", mac);
        }