@tc-libs/api-key
v3.9.0
Published
Gestione API key applicative con supporto guard, strategy Passport e scadenze.
Readme
@tc-libs/api-key
Gestione API key applicative con supporto guard, strategy Passport e scadenze.
Il package include:
ApiKeyModuleApiKeyService- decorator
ApiKeyProtected - guard/strategy
x-api-key - entity/repository/controller admin e public
Registrazione
Il modulo richiede anche un provider del servizio utente:
ApiKeyModule.register(
{
provide: USER_SERVICE,
useExisting: UserService,
},
{
env: 'PROD',
},
true,
);ApiKeyService
Metodi specifici:
checkIfUserIsCapped(userId)findOneByKey(key)findOneByActiveKey(key)createByUserId(userId, env, dto)createRaw(userId, dto)active(repository)inactive(repository)reset(repository, secret)validateHashApiKey(plainHeader, hash)createKey(env)createSecret()createHashApiKey(key, secret)update(repository, dto)inactiveManyByEndDate()
Come funziona la coppia key/secret
keyviene generata con prefisso ambientesecretviene restituito solo in fase creazione/reset- nel database viene salvato l'hash SHA256 di
key:secret - il client invia la chiave via header
x-api-key
Protezione endpoint
@Get('protected')
@ApiKeyProtected()
handler(@ApiKeyPayload() payload: any) {}Decorator utili:
ApiKeyProtected()ApiKeyPayload()GetApiKey()
Sviluppo
nx build api-key
nx test api-key