Class Aws4HmacAuthorizationHeaderBuilder
- Namespace
- Codebelt.Extensions.AwsSignature4
- Assembly
- Codebelt.Extensions.AwsSignature4.dll
Provides a way to fluently represent a HTTP AWS4-HMAC-SHA256 Authentication header.
public class Aws4HmacAuthorizationHeaderBuilder : HmacAuthorizationHeaderBuilder<Aws4HmacAuthorizationHeaderBuilder>
- Inheritance
-
Aws4HmacAuthorizationHeaderBuilder
- Inherited Members
Examples
Examples
This example demonstrates how to build an AWS Signature Version 4 authorization header using the fluent builder API:
using System;
using Codebelt.Extensions.AwsSignature4;
namespace Aws4Example;
class Program
{
static void Main()
{
var timestamp = DateTime.UtcNow;
// Create the builder and add credentials
var headerBuilder = new Aws4HmacAuthorizationHeaderBuilder()
.AddClientId("AKIAIOSFODNN7EXAMPLE")
.AddClientSecret("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")
.AddCredentialScope(timestamp);
Console.WriteLine($"Client ID: AKIAIOSFODNN7EXAMPLE");
Console.WriteLine($"Credential Scope: {timestamp.ToAwsDateString()}/eu-west-1/s3/aws4_request");
Console.WriteLine("");
Console.WriteLine("To complete the header:");
Console.WriteLine("1. Call AddFromRequest() with an HttpRequest to populate headers, method, URI, and payload");
Console.WriteLine("2. Call Build() to generate the AWS4-HMAC-SHA256 Authorization header");
}
}
For a complete, real-world example that demonstrates building a full AWS Signature Version 4 header from an HttpRequest, see the Aws4HmacAuthorizationHeaderBuilderTest unit test in the repository.
Remarks
Constructors
Aws4HmacAuthorizationHeaderBuilder()
Initializes a new instance of the Aws4HmacAuthorizationHeaderBuilder class.
public Aws4HmacAuthorizationHeaderBuilder()
Methods
AddCredentialScope(DateTime, string, string, string)
Adds the credential scope that defines the remote resource.
public Aws4HmacAuthorizationHeaderBuilder AddCredentialScope(DateTime timestamp, string region = "eu-west-1", string service = "s3", string termination = "aws4_request")
Parameters
timestampDateTimeThe DateTime value part of the credential scope.
regionstringThe AWS region part of the credential scope. Default is
eu-west-1.servicestringThe service name part of the credential scope. Default is
s3(Simple Storage Service).terminationstringThe termination string part of the credential scope. Default is
aws4_request.
Returns
- Aws4HmacAuthorizationHeaderBuilder
A reference to this instance so that additional calls can be chained.
Remarks
The following string represents the scope part of the Credential parameter for a S3 request in the eu-west-1 Region: 20220710/eu-west-1/s3/aws4_request
AddFromRequest(HttpRequest)
Adds the necessary fields that is part of an HTTP request.
public override Aws4HmacAuthorizationHeaderBuilder AddFromRequest(HttpRequest request)
Parameters
requestHttpRequestAn instance of the HttpRequest object.
Returns
- Aws4HmacAuthorizationHeaderBuilder
A reference to this instance so that additional calls can be chained.
Build()
Builds an instance of HmacAuthorizationHeader that implements AuthorizationHeader.
public override HmacAuthorizationHeader Build()
Returns
- HmacAuthorizationHeader
An instance of HmacAuthorizationHeader.
Remarks
ComputeCanonicalRequest()
Converts the request to a standardized (canonical) format and computes a message digest.
public override string ComputeCanonicalRequest()
Returns
Remarks
ComputeSignature()
Computes the signature of this instance using a series of hash-based message authentication codes (HMACs).
public override string ComputeSignature()
Returns
Remarks
https://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html, https://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html