Class InMemoryApiKeyStore

java.lang.Object
com.svenruppert.jsentinel.authentication.InMemoryApiKeyStore
All Implemented Interfaces:
ApiKeyStore

@ExperimentalJSentinelApi public final class InMemoryApiKeyStore extends Object implements ApiKeyStore
In-memory ApiKeyStore backed by a LinkedHashMap so listBySubject(TenantId, SubjectId) preserves insertion order. Read-write lock keeps mutations atomic while letting concurrent lookups proceed.
  • Constructor Details

    • InMemoryApiKeyStore

      public InMemoryApiKeyStore()
      Creates an empty store.
  • Method Details

    • findByHash

      public Optional<ApiKeyRecord> findByHash(String keyHash)
      Description copied from interface: ApiKeyStore
      Looks up an API-key record by its hash.
      Specified by:
      findByHash in interface ApiKeyStore
      Parameters:
      keyHash - key hash; must not be null or blank
      Returns:
      the record, if present
    • save

      public void save(ApiKeyRecord record)
      Description copied from interface: ApiKeyStore
      Persists or replaces the supplied record. Keyed on ApiKeyRecord.keyHash().
      Specified by:
      save in interface ApiKeyStore
      Parameters:
      record - record to persist; must not be null
    • listBySubject

      public List<ApiKeyRecord> listBySubject(TenantId tenant, SubjectId subjectId)
      Description copied from interface: ApiKeyStore
      Returns every record (active, revoked, or expired) belonging to subjectId within tenant, in insertion order (oldest first). Used by an "manage my API keys" UI.
      Specified by:
      listBySubject in interface ApiKeyStore
      Parameters:
      tenant - tenant scope; must not be null
      subjectId - subject; must not be null
      Returns:
      immutable list of records; empty when none exist
    • markUsed

      public boolean markUsed(String keyHash, Instant at)
      Description copied from interface: ApiKeyStore
      Updates the ApiKeyRecord.lastUsedAt() timestamp of the record for keyHash.
      Specified by:
      markUsed in interface ApiKeyStore
      Parameters:
      keyHash - key hash; must not be null or blank
      at - instant of use; must not be null
      Returns:
      true if the record existed and was updated, false when no such key existed
    • revoke

      public boolean revoke(String keyHash, Instant at)
      Description copied from interface: ApiKeyStore
      Marks the record for keyHash revoked at at. No-op on an already-revoked record.
      Specified by:
      revoke in interface ApiKeyStore
      Parameters:
      keyHash - key hash; must not be null or blank
      at - instant of revocation; must not be null
      Returns:
      true if a not-yet-revoked record was marked, false when no such key existed or when it was already revoked
    • deleteByHash

      public boolean deleteByHash(String keyHash)
      Description copied from interface: ApiKeyStore
      Removes the record for keyHash.
      Specified by:
      deleteByHash in interface ApiKeyStore
      Parameters:
      keyHash - key hash; must not be null or blank
      Returns:
      true when a record was removed
    • purgeExpired

      public int purgeExpired(Instant now)
      Description copied from interface: ApiKeyStore
      Drops every record whose ApiKeyRecord.expiresAt() is at or before now.
      Specified by:
      purgeExpired in interface ApiKeyStore
      Parameters:
      now - retention boundary; must not be null
      Returns:
      number of records purged