public async Task Sign(HttpRequestMessage request, DateTimeOffset timeOfSigning, TimeSpan expires) { if (request == null) { throw new ArgumentNullException(nameof(request)); } try { if (timeOfSigning > _systemClock.UtcNow) { throw new HttpMessageSigningException("Cannot create a signature that is created in the future."); } if (timeOfSigning.Add(expires) < _systemClock.UtcNow) { throw new HttpMessageSigningException("Cannot create a signature that has already expired."); } var clonedSettings = (SigningSettings)_signingSettings.Clone(); var onRequestSigningTask = _signingSettings.Events?.OnRequestSigning?.Invoke(request, clonedSettings); if (onRequestSigningTask != null) { await onRequestSigningTask.ConfigureAwait(continueOnCapturedContext : false); } _signingSettingsSanitizer.SanitizeHeaderNamesToInclude(clonedSettings, request); clonedSettings.Validate(); await _signatureHeaderEnsurer.EnsureHeader(request, clonedSettings, timeOfSigning).ConfigureAwait(continueOnCapturedContext: false); var signature = await _signatureCreator.CreateSignature(request, clonedSettings, timeOfSigning, expires).ConfigureAwait(continueOnCapturedContext: false); var onSignatureCreatedTask = _signingSettings.Events?.OnSignatureCreated?.Invoke(request, signature, clonedSettings); if (onSignatureCreatedTask != null) { await onSignatureCreatedTask.ConfigureAwait(continueOnCapturedContext : false); } var authParam = _authorizationHeaderParamCreator.CreateParam(signature); _logger?.LogDebug("Setting Authorization scheme to '{0}' and param to '{1}'.", clonedSettings.AuthorizationScheme, authParam); request.Headers.Authorization = new AuthenticationHeaderValue(clonedSettings.AuthorizationScheme, authParam); var onRequestSignedTask = _signingSettings.Events?.OnRequestSigned?.Invoke(request, signature, clonedSettings); if (onRequestSignedTask != null) { await onRequestSignedTask.ConfigureAwait(continueOnCapturedContext : false); } } catch (Exception ex) { _logger?.LogError(ex, "Could not sign the specified request. See inner exception."); throw; } }
public async Task Sign(HttpRequestMessage request) { try { if (request == null) { throw new ArgumentNullException(nameof(request)); } var clonedSettings = (SigningSettings)_signingSettings.Clone(); var onRequestSigningTask = _signingSettings.Events?.OnRequestSigning?.Invoke(request, clonedSettings); if (onRequestSigningTask != null) { await onRequestSigningTask; } clonedSettings.Validate(); var timeOfSigning = _systemClock.UtcNow; await _dateHeaderEnsurer.EnsureHeader(request, clonedSettings, timeOfSigning); await _digestHeaderEnsurer.EnsureHeader(request, clonedSettings, timeOfSigning); var signature = await _signatureCreator.CreateSignature(request, clonedSettings, timeOfSigning); var authParam = _authorizationHeaderParamCreator.CreateParam(signature); _logger?.LogDebug("Setting Authorization scheme to '{0}' and param to '{1}'.", AuthorizationScheme, authParam); request.Headers.Authorization = new AuthenticationHeaderValue(AuthorizationScheme, authParam); var onRequestSignedTask = _signingSettings.Events?.OnRequestSigned?.Invoke(request, signature, clonedSettings); if (onRequestSignedTask != null) { await onRequestSignedTask; } } catch (Exception ex) { _logger?.LogError(ex, "Could not sign the specified request. See inner exception."); throw; } }
public async Task EnsureHeader(HttpRequestMessage request, SigningSettings signingSettings, DateTimeOffset timeOfSigning) { await _dateHeaderEnsurer.EnsureHeader(request, signingSettings, timeOfSigning).ConfigureAwait(false); await _digestHeaderEnsurer.EnsureHeader(request, signingSettings, timeOfSigning).ConfigureAwait(false); }