Table of Contents

Class AccessKeyExtensions

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

Extension methods for the AccessKey record.

public static class AccessKeyExtensions
Inheritance
object
AccessKeyExtensions

Examples

The following example issues an AccessKey with a one-hour lifespan and then calls the IsValid extension against a CoordinatedUniversalTime snapshot taken from a hypothetical verifier clock, so the result reflects clock-skew tolerance rather than local "now". Use IsValid whenever an HTTP request, a queue consumer, or a service-to-service call needs to decide whether a presented key is still trustworthy.

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

namespace KeyVerificationSample;

public static class Verifier
{
    public static bool IsKeyTrusted(string hexSecret, DateTime verifierClock)
    {
        var key = AccessKey.Issue(new Secret(hexSecret), TimeToLive.FromHours(1));
        CoordinatedUniversalTime snapshot = verifierClock;

        // The extension returns false when the snapshot is outside the key's validity window plus tolerance.
        return key.IsValid(snapshot);
    }
}

Methods

IsValid(AccessKey, CoordinatedUniversalTime)

Returns true if ... is valid.

public static bool IsValid(this AccessKey accessKey, CoordinatedUniversalTime utc = null)

Parameters

accessKey AccessKey

The AccessKey to extend.

utc CoordinatedUniversalTime

The optional CoordinatedUniversalTime to use instead of Now().

Returns

bool

true if the specified accessKey is valid; otherwise, false.

Exceptions

ArgumentNullException

accessKey cannot be null.