Table of Contents

Class SharedKeyToken

Namespace
VAST.Network
Assembly
VAST.Common.dll

Provides shared key token generation and validation for secure API authentication.

public class SharedKeyToken
Inheritance
SharedKeyToken
Inherited Members

Remarks

This class uses SHA-512 hashing with a shared secret key, timestamp, and IP address to generate and validate time-limited authentication tokens.

Constructors

SharedKeyToken()

Initializes a new instance of the SharedKeyToken class.

public SharedKeyToken()

Remarks

The Initialize(string, string, bool) method must be called before using this instance.

SharedKeyToken(string, bool)

Initializes a new instance of the SharedKeyToken class with the specified secret key.

public SharedKeyToken(string secretKey, bool isOnlineMode)

Parameters

secretKey string

The shared secret key used for token generation and validation.

isOnlineMode bool

A value indicating whether to use online services to determine the external IP address.

SharedKeyToken(string, string, bool)

Initializes a new instance of the SharedKeyToken class with the specified secret key and remote server.

public SharedKeyToken(string secretKey, string remoteServer, bool isOnlineMode)

Parameters

secretKey string

The shared secret key used for token generation and validation.

remoteServer string

The remote server URL to query for the external IP address, or null to skip.

isOnlineMode bool

A value indicating whether to use online services to determine the external IP address.

Exceptions

Exception

Thrown when initialization fails.

Properties

Expiration

Gets or sets the token expiration time window.

public TimeSpan Expiration { get; set; }

Property Value

TimeSpan

Remarks

Tokens older than this time span will be considered expired during validation. The default value is 60 seconds.

Methods

Generate(string, string)

Generates a new shared key token for the specified HTTP method and API function.

public string Generate(string httpMethod, string apiFunction)

Parameters

httpMethod string

The HTTP method (e.g., GET, POST) for the request.

apiFunction string

The API function or endpoint path.

Returns

string

A token string containing the version, timestamp, and hash.

Initialize(string, string, bool)

Initializes the shared key token with the specified parameters.

public Task<bool> Initialize(string secretKey, string remoteServer, bool isOnlineMode)

Parameters

secretKey string

The shared secret key used for token generation and validation.

remoteServer string

The remote server URL to query for the external IP address, or null to skip.

isOnlineMode bool

A value indicating whether to use online services to determine the external IP address.

Returns

Task<bool>

true if initialization succeeded; otherwise, false.

IsSharedKeyToken(string)

Determines whether the specified string is a valid shared key token format.

public bool IsSharedKeyToken(string token)

Parameters

token string

The token string to check.

Returns

bool

true if the token has a valid shared key token format; otherwise, false.

Remarks

This method parses the token and stores its components for subsequent validation.

Validate(string, string, string, string)

Validates the specified token against the HTTP method, API function, and request IP.

public bool Validate(string token, string httpMethod, string apiFunction, string requestIp)

Parameters

token string

The token string to validate.

httpMethod string

The HTTP method (e.g., GET, POST) for the request.

apiFunction string

The API function or endpoint path.

requestIp string

The IP address of the requester.

Returns

bool

true if the token is valid and not expired; otherwise, false.

Remarks

If the request IP does not match, validation is also attempted using the external IP address to handle NAT scenarios.