UNPKG

@tdc/feathers-authentication-api-key

Version:

API Key authentication strategy for @feathers/authentication

85 lines (84 loc) 3.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors_1 = require("@feathersjs/errors"); const authentication_1 = require("@feathersjs/authentication"); class ApiKeyStrategy extends authentication_1.AuthenticationBaseStrategy { constructor() { super(); this.serviceBased = false; } verifyConfiguration() { this.serviceBased = ["service", "entity"].every(prop => prop in this.configuration); if (!this.serviceBased) { if (!("key" in this.configuration)) { throw new Error(`A static key is missing, when strategy '${this.name}', is not service based`); } } ["headerField"].forEach(prop => { if (prop in this.configuration) return; throw new Error(`'${prop}' is missing from configuration`); }); } get configuration() { const config = super.configuration || {}; return { errorMessage: "Invalid API key", entity: "api-key", ...config }; } async findEntity(apiKey, params) { const { errorMessage, entity } = this.configuration; try { const result = await this.entityService.find({ query: { [entity]: apiKey, $limit: 1 }, paginate: false }); if (result.length === 0) { throw new errors_1.NotAuthenticated(errorMessage); } return result[0]; } catch (error) { throw new errors_1.NotAuthenticated(errorMessage); } } async authenticate(authRequest, params) { const { key, errorMessage, entity, revokedField, headerField } = this.configuration; const apiKey = authRequest[entity]; const response = { authentication: { strategy: this.name, [entity]: apiKey }, headers: { ...params.headers, [headerField]: apiKey }, apiKey: true, [entity]: {} }; if (!this.serviceBased) { if (key !== apiKey) throw new errors_1.NotAuthenticated(errorMessage); return response; } const apiKeyData = await this.findEntity(apiKey, params); if (revokedField in apiKeyData) { if (apiKeyData[revokedField]) { throw new errors_1.NotAuthenticated("API Key has been revoked"); } } response[entity] = apiKeyData; return response; } async parse(req, res) { const { headerField, entity } = this.configuration; const apiKey = req.headers[headerField]; if (apiKey) { return { strategy: this.name, [entity]: apiKey }; } return null; } } exports.ApiKeyStrategy = ApiKeyStrategy;