Skip to content

@nestarc/api-keys

Classes

ApiKeyError

Defined in: src/errors.ts:25

Extends

  • Error

Constructors

Constructor
ts
new ApiKeyError(code, reason?): ApiKeyError;

Defined in: src/errors.ts:29

Parameters
ParameterType
codeApiKeyErrorCode
reason?string
Returns

ApiKeyError

Overrides
ts
Error.constructor

Properties

cause?
ts
optional cause?: unknown;

Defined in: node_modules/typescript/lib/lib.es2022.error.d.ts:26

Inherited from
ts
Error.cause

code
ts
readonly code: ApiKeyErrorCode;

Defined in: src/errors.ts:26

httpStatus
ts
readonly httpStatus: number;

Defined in: src/errors.ts:27

message
ts
message: string;

Defined in: node_modules/typescript/lib/lib.es5.d.ts:1077

Inherited from
ts
Error.message

name
ts
name: string;

Defined in: node_modules/typescript/lib/lib.es5.d.ts:1076

Inherited from
ts
Error.name

stack?
ts
optional stack?: string;

Defined in: node_modules/typescript/lib/lib.es5.d.ts:1078

Inherited from
ts
Error.stack

stackTraceLimit
ts
static stackTraceLimit: number;

Defined in: node_modules/@types/node/globals.d.ts:68

The Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)).

The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed.

If set to a non-number value, or set to a negative number, stack traces will not capture any frames.

Inherited from
ts
Error.stackTraceLimit

Methods

captureStackTrace()
ts
static captureStackTrace(targetObject, constructorOpt?): void;

Defined in: node_modules/@types/node/globals.d.ts:52

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

js
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

js
function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw error;
}

a();
Parameters
ParameterType
targetObjectobject
constructorOpt?Function
Returns

void

Inherited from
ts
Error.captureStackTrace

prepareStackTrace()
ts
static prepareStackTrace(err, stackTraces): any;

Defined in: node_modules/@types/node/globals.d.ts:56

Parameters
ParameterType
errError
stackTracesCallSite[]
Returns

any

See

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from
ts
Error.prepareStackTrace

ApiKeyOperationError

Defined in: src/errors.ts:45

Extends

  • Error

Constructors

Constructor
ts
new ApiKeyOperationError(code, reason?): ApiKeyOperationError;

Defined in: src/errors.ts:48

Parameters
ParameterType
codeApiKeyOperationErrorCode
reason?string
Returns

ApiKeyOperationError

Overrides
ts
Error.constructor

Properties

cause?
ts
optional cause?: unknown;

Defined in: node_modules/typescript/lib/lib.es2022.error.d.ts:26

Inherited from
ts
Error.cause

code
ts
readonly code: ApiKeyOperationErrorCode;

Defined in: src/errors.ts:46

message
ts
message: string;

Defined in: node_modules/typescript/lib/lib.es5.d.ts:1077

Inherited from
ts
Error.message

name
ts
name: string;

Defined in: node_modules/typescript/lib/lib.es5.d.ts:1076

Inherited from
ts
Error.name

stack?
ts
optional stack?: string;

Defined in: node_modules/typescript/lib/lib.es5.d.ts:1078

Inherited from
ts
Error.stack

stackTraceLimit
ts
static stackTraceLimit: number;

Defined in: node_modules/@types/node/globals.d.ts:68

The Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)).

The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed.

If set to a non-number value, or set to a negative number, stack traces will not capture any frames.

Inherited from
ts
Error.stackTraceLimit

Methods

captureStackTrace()
ts
static captureStackTrace(targetObject, constructorOpt?): void;

Defined in: node_modules/@types/node/globals.d.ts:52

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

js
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

js
function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw error;
}

a();
Parameters
ParameterType
targetObjectobject
constructorOpt?Function
Returns

void

Inherited from
ts
Error.captureStackTrace

prepareStackTrace()
ts
static prepareStackTrace(err, stackTraces): any;

Defined in: node_modules/@types/node/globals.d.ts:56

Parameters
ParameterType
errError
stackTracesCallSite[]
Returns

any

See

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from
ts
Error.prepareStackTrace

ApiKeysGuard

Defined in: src/api-keys.guard.ts:27

Implements

  • CanActivate

Constructors

Constructor
ts
new ApiKeysGuard(
   service,
   reflector,
   contextWriter?,
   clientIpResolver?): ApiKeysGuard;

Defined in: src/api-keys.guard.ts:28

Parameters
ParameterType
serviceApiKeysService
reflectorReflector
contextWriter?ApiKeyContextWriter
clientIpResolver?ApiKeyClientIpResolver
Returns

ApiKeysGuard

Methods

canActivate()
ts
canActivate(context): Promise<boolean>;

Defined in: src/api-keys.guard.ts:39

Parameters
ParameterTypeDescription
contextExecutionContextCurrent execution context. Provides access to details about the current request pipeline.
Returns

Promise<boolean>

Value indicating whether or not the current request is allowed to proceed.

Implementation of
ts
CanActivate.canActivate

ApiKeysModule

Defined in: src/api-keys.module.ts:38

Constructors

Constructor
ts
new ApiKeysModule(): ApiKeysModule;
Returns

ApiKeysModule

Methods

forRoot()
ts
static forRoot(options): DynamicModule;

Defined in: src/api-keys.module.ts:39

Parameters
ParameterType
optionsApiKeysModuleOptions
Returns

DynamicModule


ApiKeysService

Defined in: src/api-keys.service.ts:46

Constructors

Constructor
ts
new ApiKeysService(deps): ApiKeysService;

Defined in: src/api-keys.service.ts:64

Parameters
ParameterType
depsApiKeysServiceDeps
Returns

ApiKeysService

Methods

create()
ts
create(input): Promise<CreateApiKeyResult>;

Defined in: src/api-keys.service.ts:81

Parameters
ParameterType
inputCreateApiKeyInput
Returns

Promise<CreateApiKeyResult>

list()
ts
list(tenantId, opts?): Promise<ApiKeyRecord[]>;

Defined in: src/api-keys.service.ts:337

Parameters
ParameterType
tenantIdstring
opts{ includeRevoked?: boolean; }
opts.includeRevoked?boolean
Returns

Promise<ApiKeyRecord[]>

revoke()
ts
revoke(id): Promise<void>;

Defined in: src/api-keys.service.ts:228

Parameters
ParameterType
idstring
Returns

Promise<void>

rotate()
ts
rotate(id, input?): Promise<RotateApiKeyResult>;

Defined in: src/api-keys.service.ts:243

Parameters
ParameterType
idstring
inputRotateApiKeyInput
Returns

Promise<RotateApiKeyResult>

verify()
ts
verify(rawKey): Promise<ApiKeyContext>;

Defined in: src/api-keys.service.ts:141

Parameters
ParameterType
rawKeystring
Returns

Promise<ApiKeyContext>


InMemoryApiKeyStorage

Defined in: src/storage/in-memory-storage.ts:8

Implements

Constructors

Constructor
ts
new InMemoryApiKeyStorage(): InMemoryApiKeyStorage;
Returns

InMemoryApiKeyStorage

Methods

findById()
ts
findById(id): Promise<ApiKeyRecord | null>;

Defined in: src/storage/in-memory-storage.ts:23

Parameters
ParameterType
idstring
Returns

Promise<ApiKeyRecord | null>

Implementation of

ApiKeyStorage.findById

findByPrefix()
ts
findByPrefix(prefix): Promise<ApiKeyRecord | null>;

Defined in: src/storage/in-memory-storage.ts:28

Parameters
ParameterType
prefixstring
Returns

Promise<ApiKeyRecord | null>

Implementation of

ApiKeyStorage.findByPrefix

insert()
ts
insert(record): Promise<void>;

Defined in: src/storage/in-memory-storage.ts:11

Parameters
ParameterType
recordApiKeyRecord
Returns

Promise<void>

Implementation of

ApiKeyStorage.insert

listByTenant()
ts
listByTenant(tenantId, opts?): Promise<ApiKeyRecord[]>;

Defined in: src/storage/in-memory-storage.ts:38

Parameters
ParameterType
tenantIdstring
optsListApiKeysOptions
Returns

Promise<ApiKeyRecord[]>

Implementation of

ApiKeyStorage.listByTenant

markRevoked()
ts
markRevoked(id, at): Promise<void>;

Defined in: src/storage/in-memory-storage.ts:50

Parameters
ParameterType
idstring
atDate
Returns

Promise<void>

Implementation of

ApiKeyStorage.markRevoked

rotate()
ts
rotate(input): Promise<void>;

Defined in: src/storage/in-memory-storage.ts:68

Parameters
ParameterType
inputRotateApiKeyStorageInput
Returns

Promise<void>

Implementation of

ApiKeyStorage.rotate

touchLastUsed()
ts
touchLastUsed(id, at): Promise<void>;

Defined in: src/storage/in-memory-storage.ts:59

Parameters
ParameterType
idstring
atDate
Returns

Promise<void>

Implementation of

ApiKeyStorage.touchLastUsed


PrismaApiKeyStorage

Defined in: src/storage/prisma-storage.ts:18

Implements

Constructors

Constructor
ts
new PrismaApiKeyStorage(prisma): PrismaApiKeyStorage;

Defined in: src/storage/prisma-storage.ts:19

Parameters
ParameterType
prismaPrismaLike
Returns

PrismaApiKeyStorage

Methods

findById()
ts
findById(id): Promise<ApiKeyRecord | null>;

Defined in: src/storage/prisma-storage.ts:25

Parameters
ParameterType
idstring
Returns

Promise<ApiKeyRecord | null>

Implementation of

ApiKeyStorage.findById

findByPrefix()
ts
findByPrefix(prefix): Promise<ApiKeyRecord | null>;

Defined in: src/storage/prisma-storage.ts:35

Parameters
ParameterType
prefixstring
Returns

Promise<ApiKeyRecord | null>

Implementation of

ApiKeyStorage.findByPrefix

insert()
ts
insert(record): Promise<void>;

Defined in: src/storage/prisma-storage.ts:21

Parameters
ParameterType
recordApiKeyRecord
Returns

Promise<void>

Implementation of

ApiKeyStorage.insert

listByTenant()
ts
listByTenant(tenantId, opts?): Promise<ApiKeyRecord[]>;

Defined in: src/storage/prisma-storage.ts:45

Parameters
ParameterType
tenantIdstring
optsListApiKeysOptions
Returns

Promise<ApiKeyRecord[]>

Implementation of

ApiKeyStorage.listByTenant

markRevoked()
ts
markRevoked(id, at): Promise<void>;

Defined in: src/storage/prisma-storage.ts:62

Parameters
ParameterType
idstring
atDate
Returns

Promise<void>

Implementation of

ApiKeyStorage.markRevoked

rotate()
ts
rotate(input): Promise<void>;

Defined in: src/storage/prisma-storage.ts:70

Parameters
ParameterType
inputRotateApiKeyStorageInput
Returns

Promise<void>

Implementation of

ApiKeyStorage.rotate

touchLastUsed()
ts
touchLastUsed(id, at): Promise<void>;

Defined in: src/storage/prisma-storage.ts:66

Parameters
ParameterType
idstring
atDate
Returns

Promise<void>

Implementation of

ApiKeyStorage.touchLastUsed


Sha256Hasher

Defined in: src/hasher.ts:13

Constructors

Constructor
ts
new Sha256Hasher(options): Sha256Hasher;

Defined in: src/hasher.ts:14

Parameters
ParameterType
optionsHasherOptions
Returns

Sha256Hasher

Methods

dummyVerify()
ts
dummyVerify(secret): boolean;

Defined in: src/hasher.ts:40

Parameters
ParameterType
secretstring
Returns

boolean

hash()
ts
hash(secret): HashedSecret;

Defined in: src/hasher.ts:20

Parameters
ParameterType
secretstring
Returns

HashedSecret

verify()
ts
verify(
   secret,
   expectedHash,
   pepperVersion): boolean;

Defined in: src/hasher.ts:30

Parameters
ParameterType
secretstring
expectedHashstring
pepperVersionnumber
Returns

boolean

Interfaces

ApiKeyAuthFailedEvent

Defined in: src/types.ts:105

Extends

Properties

at
ts
at: Date;

Defined in: src/types.ts:71

Inherited from

ApiKeyEventBase.at

code
ts
code: ApiKeyErrorCode;

Defined in: src/types.ts:108

environment?
ts
optional environment?: Environment;

Defined in: src/types.ts:111

keyId?
ts
optional keyId?: string;

Defined in: src/types.ts:110

prefix
ts
prefix: string | null;

Defined in: src/types.ts:107

tenantId?
ts
optional tenantId?: string;

Defined in: src/types.ts:109

type
ts
type: "api_key.auth_failed";

Defined in: src/types.ts:106


ApiKeyContext

Defined in: src/types.ts:31

Properties

allowedIpCidrs?
ts
optional allowedIpCidrs?: string[];

Defined in: src/types.ts:37

environment
ts
environment: Environment;

Defined in: src/types.ts:34

keyId
ts
keyId: string;

Defined in: src/types.ts:32

prefix
ts
prefix: string;

Defined in: src/types.ts:36

scopes
ts
scopes: string[];

Defined in: src/types.ts:35

tenantId
ts
tenantId: string;

Defined in: src/types.ts:33


ApiKeyCreatedEvent

Defined in: src/types.ts:74

Extends

Properties

at
ts
at: Date;

Defined in: src/types.ts:71

Inherited from

ApiKeyEventBase.at

createdBy
ts
createdBy: string | null;

Defined in: src/types.ts:81

environment
ts
environment: Environment;

Defined in: src/types.ts:79

keyId
ts
keyId: string;

Defined in: src/types.ts:76

prefix
ts
prefix: string;

Defined in: src/types.ts:78

scopes
ts
scopes: string[];

Defined in: src/types.ts:80

tenantId
ts
tenantId: string;

Defined in: src/types.ts:77

type
ts
type: "api_key.created";

Defined in: src/types.ts:75


ApiKeyEventBase

Defined in: src/types.ts:70

Extended by

Properties

at
ts
at: Date;

Defined in: src/types.ts:71


ApiKeyRecord

Defined in: src/types.ts:12

Properties

allowedIpCidrs?
ts
optional allowedIpCidrs?: string[];

Defined in: src/types.ts:21

createdAt
ts
createdAt: Date;

Defined in: src/types.ts:28

createdBy
ts
createdBy: string | null;

Defined in: src/types.ts:27

environment
ts
environment: Environment;

Defined in: src/types.ts:16

expiresAt
ts
expiresAt: Date | null;

Defined in: src/types.ts:23

hash
ts
hash: string;

Defined in: src/types.ts:18

id
ts
id: string;

Defined in: src/types.ts:13

lastUsedAt
ts
lastUsedAt: Date | null;

Defined in: src/types.ts:22

name
ts
name: string;

Defined in: src/types.ts:15

pepperVersion
ts
pepperVersion: number;

Defined in: src/types.ts:19

prefix
ts
prefix: string;

Defined in: src/types.ts:17

replacedByKeyId
ts
replacedByKeyId: string | null;

Defined in: src/types.ts:26

revokedAt
ts
revokedAt: Date | null;

Defined in: src/types.ts:24

rotatedAt
ts
rotatedAt: Date | null;

Defined in: src/types.ts:25

scopes
ts
scopes: string[];

Defined in: src/types.ts:20

tenantId
ts
tenantId: string;

Defined in: src/types.ts:14


ApiKeyRevokedEvent

Defined in: src/types.ts:84

Extends

Properties

at
ts
at: Date;

Defined in: src/types.ts:71

Inherited from

ApiKeyEventBase.at

environment
ts
environment: Environment;

Defined in: src/types.ts:89

keyId
ts
keyId: string;

Defined in: src/types.ts:86

prefix
ts
prefix: string;

Defined in: src/types.ts:88

tenantId
ts
tenantId: string;

Defined in: src/types.ts:87

type
ts
type: "api_key.revoked";

Defined in: src/types.ts:85


ApiKeyRotatedEvent

Defined in: src/types.ts:92

Extends

Properties

at
ts
at: Date;

Defined in: src/types.ts:71

Inherited from

ApiKeyEventBase.at

createdBy
ts
createdBy: string | null;

Defined in: src/types.ts:102

environment
ts
environment: Environment;

Defined in: src/types.ts:99

graceExpiresAt
ts
graceExpiresAt: Date;

Defined in: src/types.ts:101

newKeyId
ts
newKeyId: string;

Defined in: src/types.ts:97

newPrefix
ts
newPrefix: string;

Defined in: src/types.ts:98

oldKeyId
ts
oldKeyId: string;

Defined in: src/types.ts:95

oldPrefix
ts
oldPrefix: string;

Defined in: src/types.ts:96

scopes
ts
scopes: string[];

Defined in: src/types.ts:100

tenantId
ts
tenantId: string;

Defined in: src/types.ts:94

type
ts
type: "api_key.rotated";

Defined in: src/types.ts:93


ApiKeysModuleOptions

Defined in: src/api-keys.module.ts:20

Properties

clientIpResolver?
ts
optional clientIpResolver?: ApiKeyClientIpResolver;

Defined in: src/api-keys.module.ts:34

contextWriter?
ts
optional contextWriter?: ApiKeyContextWriter;

Defined in: src/api-keys.module.ts:33

currentPepperVersion?
ts
optional currentPepperVersion?: number;

Defined in: src/api-keys.module.ts:23

debounceMs?
ts
optional debounceMs?: number;

Defined in: src/api-keys.module.ts:24

emitUsageEvents?
ts
optional emitUsageEvents?: boolean;

Defined in: src/api-keys.module.ts:31

namespace?
ts
optional namespace?: string;

Defined in: src/api-keys.module.ts:21

onAuthFailed?
ts
optional onAuthFailed?: (prefix, code) => void;

Defined in: src/api-keys.module.ts:26

Parameters
ParameterType
prefixstring | null
codestring
Returns

void

onEvent?
ts
optional onEvent?: ApiKeyEventSink;

Defined in: src/api-keys.module.ts:27

onEventError?
ts
optional onEventError?: (error, event) => void;

Defined in: src/api-keys.module.ts:28

Parameters
ParameterType
errorunknown
eventApiKeyEvent
Returns

void

onMetric?
ts
optional onMetric?: ApiKeyMetricSink;

Defined in: src/api-keys.module.ts:29

onMetricError?
ts
optional onMetricError?: (error, metric) => void;

Defined in: src/api-keys.module.ts:30

Parameters
ParameterType
errorunknown
metricApiKeyVerificationMetric
Returns

void

peppers
ts
peppers: Record<number, string>;

Defined in: src/api-keys.module.ts:22

storage
ts
storage: ApiKeyStorage;

Defined in: src/api-keys.module.ts:25

ttlPolicy?
ts
optional ttlPolicy?: ApiKeyTtlPolicy;

Defined in: src/api-keys.module.ts:32


ApiKeysServiceDeps

Defined in: src/api-keys.service.ts:29

Properties

clock?
ts
optional clock?: () => Date;

Defined in: src/api-keys.service.ts:34

Returns

Date

debounceMs?
ts
optional debounceMs?: number;

Defined in: src/api-keys.service.ts:35

emitUsageEvents?
ts
optional emitUsageEvents?: boolean;

Defined in: src/api-keys.service.ts:41

hasher
ts
hasher: Sha256Hasher;

Defined in: src/api-keys.service.ts:31

idFactory?
ts
optional idFactory?: () => string;

Defined in: src/api-keys.service.ts:33

Returns

string

monotonicClock?
ts
optional monotonicClock?: () => number;

Defined in: src/api-keys.service.ts:43

Returns

number

namespace
ts
namespace: string;

Defined in: src/api-keys.service.ts:32

onAuthFailed?
ts
optional onAuthFailed?: (prefix, code) => void;

Defined in: src/api-keys.service.ts:36

Parameters
ParameterType
prefixstring | null
codestring
Returns

void

onEvent?
ts
optional onEvent?: ApiKeyEventSink;

Defined in: src/api-keys.service.ts:37

onEventError?
ts
optional onEventError?: (error, event) => void;

Defined in: src/api-keys.service.ts:38

Parameters
ParameterType
errorunknown
eventApiKeyEvent
Returns

void

onMetric?
ts
optional onMetric?: ApiKeyMetricSink;

Defined in: src/api-keys.service.ts:39

onMetricError?
ts
optional onMetricError?: (error, metric) => void;

Defined in: src/api-keys.service.ts:40

Parameters
ParameterType
errorunknown
metricApiKeyVerificationMetric
Returns

void

storage
ts
storage: ApiKeyStorage;

Defined in: src/api-keys.service.ts:30

ttlPolicy?
ts
optional ttlPolicy?: ApiKeyTtlPolicy;

Defined in: src/api-keys.service.ts:42


ApiKeyStorage

Defined in: src/storage/api-key-storage.interface.ts:14

Methods

findById()
ts
findById(id): Promise<ApiKeyRecord | null>;

Defined in: src/storage/api-key-storage.interface.ts:16

Parameters
ParameterType
idstring
Returns

Promise<ApiKeyRecord | null>

findByPrefix()
ts
findByPrefix(prefix): Promise<ApiKeyRecord | null>;

Defined in: src/storage/api-key-storage.interface.ts:17

Parameters
ParameterType
prefixstring
Returns

Promise<ApiKeyRecord | null>

insert()
ts
insert(record): Promise<void>;

Defined in: src/storage/api-key-storage.interface.ts:15

Parameters
ParameterType
recordApiKeyRecord
Returns

Promise<void>

listByTenant()
ts
listByTenant(tenantId, opts?): Promise<ApiKeyRecord[]>;

Defined in: src/storage/api-key-storage.interface.ts:18

Parameters
ParameterType
tenantIdstring
opts?ListApiKeysOptions
Returns

Promise<ApiKeyRecord[]>

markRevoked()
ts
markRevoked(id, at): Promise<void>;

Defined in: src/storage/api-key-storage.interface.ts:19

Parameters
ParameterType
idstring
atDate
Returns

Promise<void>

rotate()
ts
rotate(input): Promise<void>;

Defined in: src/storage/api-key-storage.interface.ts:21

Parameters
ParameterType
inputRotateApiKeyStorageInput
Returns

Promise<void>

touchLastUsed()
ts
touchLastUsed(id, at): Promise<void>;

Defined in: src/storage/api-key-storage.interface.ts:20

Parameters
ParameterType
idstring
atDate
Returns

Promise<void>


ApiKeyTtlPolicy

Defined in: src/types.ts:149

Properties

allowNeverExpires?
ts
optional allowNeverExpires?: boolean;

Defined in: src/types.ts:152

defaultExpiresInMs?
ts
optional defaultExpiresInMs?: number;

Defined in: src/types.ts:150

maxExpiresInMs?
ts
optional maxExpiresInMs?: number;

Defined in: src/types.ts:151


ApiKeyUsedEvent

Defined in: src/types.ts:114

Extends

Properties

at
ts
at: Date;

Defined in: src/types.ts:71

Inherited from

ApiKeyEventBase.at

environment
ts
environment: Environment;

Defined in: src/types.ts:119

keyId
ts
keyId: string;

Defined in: src/types.ts:116

prefix
ts
prefix: string;

Defined in: src/types.ts:118

scopes
ts
scopes: string[];

Defined in: src/types.ts:120

tenantId
ts
tenantId: string;

Defined in: src/types.ts:117

type
ts
type: "api_key.used";

Defined in: src/types.ts:115


ApiKeyVerificationMetric

Defined in: src/types.ts:140

Properties

durationMs
ts
durationMs: number;

Defined in: src/types.ts:143

environment?
ts
optional environment?: Environment;

Defined in: src/types.ts:144

outcome
ts
outcome: ApiKeyVerificationOutcome;

Defined in: src/types.ts:142

type
ts
type: "api_key.verification";

Defined in: src/types.ts:141


CreateApiKeyInput

Defined in: src/types.ts:40

Properties

allowedIpCidrs?
ts
optional allowedIpCidrs?: string[];

Defined in: src/types.ts:47

createdBy?
ts
optional createdBy?: string;

Defined in: src/types.ts:46

environment?
ts
optional environment?: Environment;

Defined in: src/types.ts:43

expiresAt?
ts
optional expiresAt?: Date;

Defined in: src/types.ts:45

name
ts
name: string;

Defined in: src/types.ts:42

scopes
ts
scopes: Scope[];

Defined in: src/types.ts:44

tenantId
ts
tenantId: string;

Defined in: src/types.ts:41


CreateApiKeyResult

Defined in: src/types.ts:50

Properties

id
ts
id: string;

Defined in: src/types.ts:51

key
ts
key: string;

Defined in: src/types.ts:52


CreateTestKeyOptions

Defined in: src/testing.ts:4

Properties

allowedIpCidrs?
ts
optional allowedIpCidrs?: string[];

Defined in: src/testing.ts:11

createdBy?
ts
optional createdBy?: string;

Defined in: src/testing.ts:10

environment?
ts
optional environment?: Environment;

Defined in: src/testing.ts:7

expiresAt?
ts
optional expiresAt?: Date;

Defined in: src/testing.ts:9

name?
ts
optional name?: string;

Defined in: src/testing.ts:6

scopes?
ts
optional scopes?: Scope[];

Defined in: src/testing.ts:8

tenantId?
ts
optional tenantId?: string;

Defined in: src/testing.ts:5


HashedSecret

Defined in: src/hasher.ts:3

Properties

hash
ts
hash: string;

Defined in: src/hasher.ts:4

pepperVersion
ts
pepperVersion: number;

Defined in: src/hasher.ts:5


HasherOptions

Defined in: src/hasher.ts:8

Properties

currentVersion
ts
currentVersion: number;

Defined in: src/hasher.ts:10

peppers
ts
peppers: Record<number, string>;

Defined in: src/hasher.ts:9


ListApiKeysOptions

Defined in: src/storage/api-key-storage.interface.ts:3

Properties

includeRevoked?
ts
optional includeRevoked?: boolean;

Defined in: src/storage/api-key-storage.interface.ts:4


PrismaLike

Defined in: src/storage/prisma-storage.ts:8

Properties

apiKey
ts
apiKey: {
  create: Promise<unknown>;
  findMany: Promise<unknown[]>;
  findUnique: Promise<unknown>;
  update: Promise<unknown>;
};

Defined in: src/storage/prisma-storage.ts:9

create()
ts
create(args): Promise<unknown>;
Parameters
ParameterType
args{ data: unknown; }
args.dataunknown
Returns

Promise<unknown>

findMany()
ts
findMany(args): Promise<unknown[]>;
Parameters
ParameterType
args{ orderBy?: unknown; where: unknown; }
args.orderBy?unknown
args.whereunknown
Returns

Promise<unknown[]>

findUnique()
ts
findUnique(args): Promise<unknown>;
Parameters
ParameterType
args{ where: | { prefix: string; } | { id: string; }; }
args.where| { prefix: string; } | { id: string; }
Returns

Promise<unknown>

update()
ts
update(args): Promise<unknown>;
Parameters
ParameterType
args{ data: unknown; where: { id: string; }; }
args.dataunknown
args.where{ id: string; }
args.where.idstring
Returns

Promise<unknown>

Methods

$transaction()?
ts
optional $transaction<T>(operations): Promise<T[]>;

Defined in: src/storage/prisma-storage.ts:15

Type Parameters
Type Parameter
T
Parameters
ParameterType
operationsPromise<T>[]
Returns

Promise<T[]>


RequiredScope

Defined in: src/decorators/require-scope.decorator.ts:6

Properties

level
ts
level: ScopeLevel;

Defined in: src/decorators/require-scope.decorator.ts:8

resource
ts
resource: string;

Defined in: src/decorators/require-scope.decorator.ts:7


RotateApiKeyInput

Defined in: src/types.ts:55

Properties

allowedIpCidrs?
ts
optional allowedIpCidrs?: string[];

Defined in: src/types.ts:60

createdBy?
ts
optional createdBy?: string;

Defined in: src/types.ts:58

expiresAt?
ts
optional expiresAt?: Date | null;

Defined in: src/types.ts:59

gracePeriodMs?
ts
optional gracePeriodMs?: number;

Defined in: src/types.ts:56

name?
ts
optional name?: string;

Defined in: src/types.ts:57


RotateApiKeyResult

Defined in: src/types.ts:63

Properties

graceExpiresAt
ts
graceExpiresAt: Date;

Defined in: src/types.ts:67

id
ts
id: string;

Defined in: src/types.ts:64

key
ts
key: string;

Defined in: src/types.ts:65

replacedKeyId
ts
replacedKeyId: string;

Defined in: src/types.ts:66


RotateApiKeyStorageInput

Defined in: src/storage/api-key-storage.interface.ts:7

Properties

newRecord
ts
newRecord: ApiKeyRecord;

Defined in: src/storage/api-key-storage.interface.ts:9

oldExpiresAt
ts
oldExpiresAt: Date;

Defined in: src/storage/api-key-storage.interface.ts:10

oldKeyId
ts
oldKeyId: string;

Defined in: src/storage/api-key-storage.interface.ts:8

rotatedAt
ts
rotatedAt: Date;

Defined in: src/storage/api-key-storage.interface.ts:11


Scope

Defined in: src/types.ts:7

Properties

level
ts
level: ScopeLevel;

Defined in: src/types.ts:9

resource
ts
resource: string;

Defined in: src/types.ts:8

Type Aliases

ApiKeyClientIpResolver

ts
type ApiKeyClientIpResolver = (request) => string | undefined | Promise<string | undefined>;

Defined in: src/ip-allowlist.ts:5

Parameters

ParameterType
requestunknown

Returns

string | undefined | Promise<string | undefined>


ApiKeyContextWriter

ts
type ApiKeyContextWriter = (apiKey, request) => void | Promise<void>;

Defined in: src/context.ts:6

Parameters

ParameterType
apiKeyApiKeyContext
requestunknown

Returns

void | Promise<void>


ApiKeyErrorCode

ts
type ApiKeyErrorCode = typeof ApiKeyErrorCode[keyof typeof ApiKeyErrorCode];

Defined in: src/errors.ts:1


ApiKeyEvent

ts
type ApiKeyEvent =
  | ApiKeyCreatedEvent
  | ApiKeyRevokedEvent
  | ApiKeyRotatedEvent
  | ApiKeyAuthFailedEvent
  | ApiKeyUsedEvent;

Defined in: src/types.ts:123


ApiKeyEventSink

ts
type ApiKeyEventSink = (event) => void | Promise<void>;

Defined in: src/types.ts:130

Parameters

ParameterType
eventApiKeyEvent

Returns

void | Promise<void>


ApiKeyMetricSink

ts
type ApiKeyMetricSink = (metric) => void | Promise<void>;

Defined in: src/types.ts:147

Parameters

ParameterType
metricApiKeyVerificationMetric

Returns

void | Promise<void>


ApiKeyOperationErrorCode

ts
type ApiKeyOperationErrorCode = typeof ApiKeyOperationErrorCode[keyof typeof ApiKeyOperationErrorCode];

Defined in: src/errors.ts:37


ApiKeyVerificationOutcome

ts
type ApiKeyVerificationOutcome = "success" | "malformed" | "invalid" | "revoked" | "expired" | "error";

Defined in: src/types.ts:132


Environment

ts
type Environment = "live" | "test";

Defined in: src/types.ts:3


ScopeLevel

ts
type ScopeLevel = "read" | "write";

Defined in: src/types.ts:5

Variables

API_KEY_CLIENT_IP_RESOLVER

ts
const API_KEY_CLIENT_IP_RESOLVER: typeof API_KEY_CLIENT_IP_RESOLVER;

Defined in: src/ip-allowlist.ts:3


API_KEY_CONTEXT_PROPERTY

ts
const API_KEY_CONTEXT_PROPERTY: "apiKey" = 'apiKey';

Defined in: src/context.ts:3


API_KEY_CONTEXT_WRITER

ts
const API_KEY_CONTEXT_WRITER: typeof API_KEY_CONTEXT_WRITER;

Defined in: src/context.ts:4


API_KEY_REDACT_REGEX

ts
const API_KEY_REDACT_REGEX: RegExp;

Defined in: src/key-format.ts:77


API_KEYS_OPTIONS

ts
const API_KEYS_OPTIONS: typeof API_KEYS_OPTIONS;

Defined in: src/api-keys.module.ts:17


API_KEYS_STORAGE

ts
const API_KEYS_STORAGE: typeof API_KEYS_STORAGE;

Defined in: src/api-keys.module.ts:18


ApiKeyErrorCode

ts
const ApiKeyErrorCode: {
  EnvironmentMismatch: "api_key_environment_mismatch";
  Expired: "api_key_expired";
  Invalid: "api_key_invalid";
  IpNotAllowed: "api_key_ip_not_allowed";
  Malformed: "api_key_malformed";
  Missing: "api_key_missing";
  Revoked: "api_key_revoked";
  ScopeInsufficient: "api_key_scope_insufficient";
};

Defined in: src/errors.ts:1

Type Declaration

NameTypeDefault valueDefined in
EnvironmentMismatch"api_key_environment_mismatch"'api_key_environment_mismatch'src/errors.ts:7
Expired"api_key_expired"'api_key_expired'src/errors.ts:6
Invalid"api_key_invalid"'api_key_invalid'src/errors.ts:4
IpNotAllowed"api_key_ip_not_allowed"'api_key_ip_not_allowed'src/errors.ts:9
Malformed"api_key_malformed"'api_key_malformed'src/errors.ts:3
Missing"api_key_missing"'api_key_missing'src/errors.ts:2
Revoked"api_key_revoked"'api_key_revoked'src/errors.ts:5
ScopeInsufficient"api_key_scope_insufficient"'api_key_scope_insufficient'src/errors.ts:8

ApiKeyOperationErrorCode

ts
const ApiKeyOperationErrorCode: {
  NotFound: "api_key_record_not_found";
  NotRotatable: "api_key_not_rotatable";
};

Defined in: src/errors.ts:37

Type Declaration

NameTypeDefault valueDefined in
NotFound"api_key_record_not_found"'api_key_record_not_found'src/errors.ts:38
NotRotatable"api_key_not_rotatable"'api_key_not_rotatable'src/errors.ts:39

CurrentApiKey

ts
const CurrentApiKey: (...dataOrPipes) => ParameterDecorator;

Defined in: src/decorators/current-api-key.decorator.ts:4

Parameters

ParameterType
...dataOrPipesunknown[]

Returns

ParameterDecorator


defaultApiKeyClientIpResolver

ts
const defaultApiKeyClientIpResolver: ApiKeyClientIpResolver;

Defined in: src/ip-allowlist.ts:9


ENVIRONMENT_METADATA

ts
const ENVIRONMENT_METADATA: "nestarc:api-keys:environment" = 'nestarc:api-keys:environment';

Defined in: src/decorators/require-environment.decorator.ts:4


SCOPE_METADATA

ts
const SCOPE_METADATA: "nestarc:api-keys:scope" = 'nestarc:api-keys:scope';

Defined in: src/decorators/require-scope.decorator.ts:4

Functions

createTestKey()

ts
function createTestKey(service, options?): Promise<CreateApiKeyResult & {
  context: ApiKeyContext;
}>;

Defined in: src/testing.ts:14

Parameters

ParameterType
serviceApiKeysService
optionsCreateTestKeyOptions

Returns

Promise<CreateApiKeyResult & { context: ApiKeyContext; }>


flattenScopes()

ts
function flattenScopes(scopes): string[];

Defined in: src/scope-matcher.ts:3

Parameters

ParameterType
scopesScope[]

Returns

string[]


generateKey()

ts
function generateKey(options): GeneratedKey;

Defined in: src/key-format.ts:29

Parameters

ParameterType
options{ environment: Environment; namespace: string; }
options.environmentEnvironment
options.namespacestring

Returns

GeneratedKey


getApiKeyContext()

ts
function getApiKeyContext(request): ApiKeyContext | undefined;

Defined in: src/context.ts:11

Parameters

ParameterType
requestunknown

Returns

ApiKeyContext | undefined


isIpAllowed()

ts
function isIpAllowed(clientIp, allowedIpCidrs): boolean;

Defined in: src/ip-allowlist.ts:22

Parameters

ParameterType
clientIpstring | undefined
allowedIpCidrsreadonly string[]

Returns

boolean


normalizeAllowedIpCidrs()

ts
function normalizeAllowedIpCidrs(entries?): string[];

Defined in: src/ip-allowlist.ts:18

Parameters

ParameterTypeDefault value
entriesreadonly string[][]

Returns

string[]


parseKey()

ts
function parseKey(raw): ParsedKey;

Defined in: src/key-format.ts:53

Parameters

ParameterType
rawstring

Returns

ParsedKey


RequireEnvironment()

ts
function RequireEnvironment(environment): CustomDecorator<string>;

Defined in: src/decorators/require-environment.decorator.ts:6

Parameters

ParameterType
environmentEnvironment

Returns

CustomDecorator<string>


RequireScope()

ts
function RequireScope(resource, level): CustomDecorator<string>;

Defined in: src/decorators/require-scope.decorator.ts:11

Parameters

ParameterType
resourcestring
levelScopeLevel

Returns

CustomDecorator<string>


scopeSatisfies()

ts
function scopeSatisfies(
   granted,
   resource,
   required): boolean;

Defined in: src/scope-matcher.ts:11

Parameters

ParameterType
grantedstring[]
resourcestring
requiredScopeLevel

Returns

boolean

Released under the MIT License.