public SigningStringCompositionRequest CreateForSigning(
            HttpRequestForSigning request,
            SigningSettings signingSettings,
            DateTimeOffset?timeOfComposing,
            TimeSpan?expires)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (signingSettings == null)
            {
                throw new ArgumentNullException(nameof(signingSettings));
            }

            return(new SigningStringCompositionRequest {
                Request = request.ToHttpRequestForSignatureString(),
                RequestTargetEscaping = signingSettings.RequestTargetEscaping,
                HeadersToInclude = signingSettings.Headers,
                TimeOfComposing = timeOfComposing,
                Expires = expires,
                Nonce = signingSettings.EnableNonce
                    ? _nonceGenerator.GenerateNonce()
                    : null
            });
        }
        public async Task <Signature> CreateSignature(HttpRequestMessage request, SigningSettings settings, DateTimeOffset timeOfSigning)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            _signingSettingsSanitizer.SanitizeHeaderNamesToInclude(settings, request);

            settings.Validate();

            var nonce             = settings.EnableNonce ? _nonceGenerator.GenerateNonce() : null;
            var requestForSigning = request.ToRequestForSigning();
            var signingString     = _signingStringComposer.Compose(
                requestForSigning,
                settings.SignatureAlgorithm.Name,
                settings.Headers,
                timeOfSigning,
                settings.Expires,
                nonce);

            var eventTask = settings.Events?.OnSigningStringComposed?.Invoke(request, signingString);

            if (eventTask != null)
            {
                await eventTask;
            }

            _logger?.LogDebug("Composed the following signing string for request signing: {0}", signingString);

            var signatureHash   = settings.SignatureAlgorithm.ComputeHash(signingString);
            var signatureString = _base64Converter.ToBase64(signatureHash);

            _logger?.LogDebug("The base64 hash of the signature string for signing is '{0}'.", signatureString);

            var signature = new Signature {
                KeyId     = settings.KeyId,
                Algorithm = $"{settings.SignatureAlgorithm.Name.ToLowerInvariant()}-{settings.SignatureAlgorithm.HashAlgorithm.ToString().ToLowerInvariant()}",
                Created   = timeOfSigning,
                Expires   = timeOfSigning.Add(settings.Expires),
                Headers   = settings.Headers,
                Nonce     = nonce,
                String    = signatureString
            };

            return(signature);
        }
Exemple #3
0
        public void SignContext(IOAuthContext context)
        {
            EnsureStateIsValid();

            context.UseAuthorizationHeader = UseHeaderForOAuthParameters;
            context.Nonce           = _nonceGenerator.GenerateNonce(context);
            context.ConsumerKey     = ConsumerKey;
            context.Realm           = Realm;
            context.SignatureMethod = SignatureMethod;
            context.Timestamp       = DateTime.Now.Epoch().ToString();
            context.Version         = "1.0";
            context.Nonce           = NonceGenerator.GenerateNonce(context);

            string signatureBase = context.GenerateSignatureBase();

            _signer.SignContext(context, new SigningContext {
                Algorithm = Key, SignatureBase = signatureBase, ConsumerSecret = ConsumerSecret
            });
        }