@thesinding/authentication-api-key
Version:
API Key authentication strategy for @feathers/authentication
87 lines (86 loc) • 2.97 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiKeyStrategy = void 0;
const InvalidAPIKeyError_1 = require("./InvalidAPIKeyError");
const errors_1 = require("@feathersjs/errors");
const authentication_1 = require("@feathersjs/authentication");
class ApiKeyStrategy extends authentication_1.AuthenticationBaseStrategy {
constructor() {
super();
this.serviceBased = false;
}
get configuration() {
const config = super.configuration || {};
return { entity: "api-key", ...config };
}
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`);
});
}
async findEntity(apiKey, params) {
const { entity } = this.configuration;
try {
const result = await this.entityService.find({
query: { [entity]: apiKey, $limit: 1 },
paginate: false,
});
if (result.length === 0) {
throw new InvalidAPIKeyError_1.InvalidAPIError();
}
return result[0];
}
catch (error) {
throw new InvalidAPIKeyError_1.InvalidAPIError();
}
}
async authenticate(authRequest, params) {
const { key, 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 InvalidAPIKeyError_1.InvalidAPIError();
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;
;