Table of Contents

Class AccessKey

Namespace
Codebelt.SharedKernel.Security
Assembly
Codebelt.SharedKernel.dll

Represents an AccessKey object that can be used for API key-based authentication and similar.

public record AccessKey : ValueObject, IEquatable<ValueObject>, IEquatable<AccessKey>
Inheritance
object
AccessKey
Implements
Inherited Members
ValueObject.Equals(object)
ValueObject.<Clone>$()
ValueObject.EqualityContract
Extension Methods

Examples

The following example issues a one-year AccessKey from a Secret and a TimeToLive, then reports the resulting secret material and the validity window so an issuer can confirm what was handed to the caller. Use AccessKey.Issue whenever you need both the secret and the bounded validity period on the same value object.

using System;
using Codebelt.SharedKernel;
using Codebelt.SharedKernel.Security;

namespace KeyIssuanceSample;

public static class KeyIssuer
{
    public static (string Secret, DateTime ValidFrom, DateTime Expires) IssueYearlyKey(string hexSecret)
    {
        var secret = new Secret(hexSecret);
        var key = AccessKey.Issue(secret, TimeToLive.FromYears(1));

        // All three values are observable on the issued AccessKey and can flow into the caller response.
        return (key.Secret, key.ValidFrom, key.Expires);
    }
}

Remarks

Constructors

AccessKey(Secret, Action<AccessKeyOptions>)

Initializes a new instance of the AccessKey class.

public AccessKey(Secret secret, Action<AccessKeyOptions> setup = null)

Parameters

secret Secret

The Secret of the access key.

setup Action<AccessKeyOptions>

The AccessKeyOptions which may be configured.

Exceptions

ArgumentException

setup failed to configure an instance of AccessKeyOptions in a valid state.

Properties

DesiredTolerance

Gets the desired tolerance for potential clock skew variations.

public TimeSpan DesiredTolerance { get; }

Property Value

TimeSpan

The desired tolerance for potential clock skew variations.

Expires

Gets the expiry date and time of the access key expressed as Coordinated Universal Time (UTC).

public DateTime Expires { get; }

Property Value

DateTime

The expiry date and time of the access key expressed as Coordinated Universal Time (UTC).

Secret

Gets the secret of the access key.

public string Secret { get; }

Property Value

string

The secret of the access key.

ValidFrom

Gets the date and time,expressed as Coordinated Universal Time (UTC), from which the access key is valid.

public DateTime ValidFrom { get; }

Property Value

DateTime

The date and time, expressed as Coordinated Universal Time (UTC), from which the access key is valid.

Methods

Issue(Secret, TimeToLive)

Issues a new instance of AccessKey with an optional lifespan.

public static AccessKey Issue(Secret secret, TimeToLive lifespan = null)

Parameters

secret Secret

The secret used to generate the access key.

lifespan TimeToLive

The lifespan of the AccessKey. If null, the access key will not expire.

Returns

AccessKey

Returns a new AccessKey> instance with the specified secret and lifespan.

See Also