All cached API Applications from the base engine.
ProtectedBaseAccess to the cached metadata from APIKeysEngineBase. This allows direct access to cached scopes, applications, and key bindings.
Check if the engine has been configured.
The full resolved key generation config (read-only).
The configured API key prefix (e.g., 'mj_sk_').
All cached API Scopes from the base engine.
Validate and authorize an API key request against scope rules. This method ALWAYS logs the authorization decision for audit purposes.
This implements the three-tier permission model:
The SHA-256 hash of the API key
The name of the calling application (e.g., 'MJAPI', 'MCPServer')
The scope being requested (e.g., 'view:run')
The specific resource (e.g., entity name)
User context for database operations
OptionalrequestContext: {Optional request context for logging (endpoint, method, etc.)
Optionaloptions: { skipLogging?: boolean }OptionalskipLogging?: booleanWhen true, skip writing to the usage log. Useful for speculative checks (e.g. full_access probe) that are not the real authorization decision.
Authorization result with optional log ID
Authorize and log the request.
Configure the engine and ensure the base engine is loaded. This should be called during server startup to preload all metadata.
OptionalforceRefresh: booleanIf true, forces a reload even if already loaded
OptionalcontextUser: UserInfoUser context for database operations
Optionalprovider: IMetadataProviderOptional metadata provider override
Creates a new API key and stores it in the database.
This method:
IMPORTANT: The raw key is only returned once. Store it securely or show it to the user immediately - it cannot be recovered later.
Configuration for the new API key
User context for database operations
Optionalprovider: IMetadataProviderResult containing the raw key (if successful) or error
Generates a new API key using the configured generation parameters.
The key format is: {prefix}{encodedRandomBytes}
mj_sk_ prefix, 32 bytes entropy, hex encoding, SHA-256 hashObject containing the raw key and its hash (hash always hex-encoded)
Get application by ID. Uses cached data from APIKeysEngineBase.
The application ID
Kept for API compatibility
Get application by name. Uses cached data from APIKeysEngineBase.
The application name (case-insensitive)
Kept for API compatibility
Get the pattern matcher utility.
Get the scope evaluator for direct access if needed.
Get the usage logger for direct access if needed.
Hashes an API key for storage or comparison.
Uses the configured hash algorithm to create a one-way hash of the key. The hash is always output as a hex string regardless of the key encoding.
The raw API key to hash
The hash as a hex string
Validates that an API key has the correct format.
Checks that the key matches the configured prefix, encoding character set, and expected length derived from the entropy bytes. This is a quick syntactic check before attempting database validation.
The API key to validate
True if the format is valid, false otherwise
Clear all caches and force a refresh of the base engine.
User context for database operations (required for refresh)
Revokes an API key, permanently disabling it.
Once revoked, an API key cannot be reactivated. Create a new key if needed.
The database ID of the API key to revoke
User context for database operations
Optionalprovider: IMetadataProviderTrue if revocation succeeded, false otherwise
Update LastUsedAt for an API key.
Optionalprovider: IMetadataProviderValidates an API key and returns the associated user context.
This is the main entry point for API key authentication. It:
Validation options including the raw key and request context
User context for database operations
Validation result with user context if valid
const result = await engine.ValidateAPIKey({
RawKey: request.headers['x-api-key'],
ApplicationName: 'MCPServer', // Check if key is valid for MCP
Endpoint: '/graphql',
Method: 'POST',
Operation: 'GetUsersRecord',
StatusCode: 200,
IPAddress: request.ip,
UserAgent: request.headers['user-agent']
}, systemUser);
if (result.IsValid) {
// Proceed with the user context
// Use result.APIKeyHash for subsequent Authorize() calls
return result.User;
}
Validate an API key by its hash (internal method).
Main orchestrator for API key operations and authorization
Provides methods for:
Example