TsgcWSAPIKeyManagerMethods › IssueKey

IssueKey Method

Generates, hashes and stores a new key; returns the plaintext (only time it can be observed).

Syntax

function IssueKey(const aOwner: string; const aScopes: TsgcAPIKeyScopes; aExpiresInSec: Integer = 0): string;

Parameters

NameTypeDescription
aOwnerconst stringTenant, customer or user identifier the key is issued to. Indexed so GetKeysByOwner can look up all keys for a tenant.
aScopesconst TsgcAPIKeyScopesArray of scope strings to attach to the key. Every entry must already exist (and be enabled) in the Scopes catalog when Scopes.Enabled is True.
aExpiresInSecIntegerLifetime of the key in seconds from now. Pass 0 to inherit Expiration.DefaultTTLSec; pass 0 with DefaultTTLSec = 0 for a key that never expires.

Return Value

The plaintext key (prefix + random body + optional checksum) — this is the only time the raw value is observable; only its hash is persisted. Deliver it to the customer from this call or from the OnKeyIssued handler. (string)

Remarks

Generates a cryptographically random key body according to the Generation group, prepends the KeyPrefix, appends a checksum when IncludeChecksum is True, then hashes the full plaintext with the Hashing group and stores the digest plus owner, scopes, timestamps and status (kksActive). Fires OnKeyIssued, appends an audit entry with action kaaIssued, updates Stats and — when StorageType is kstFile — triggers a persist. Raises if Generation.Enabled is False.

Example

var
  vKey: string;
begin
  vKey := sgcWSAPIKeyManager1.IssueKey('customer-123',
    TsgcAPIKeyScopes.Create('read:orders', 'write:orders'),
    30 * 86400);
  ShowMessage('Deliver to customer once: ' + vKey);
end;

Back to Methods