Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { logger } = require('./helper/logging.helper');
const { FORCE_CONFIG, DOUBLE_CONFIG } = require('./errors/error.messages');
const validateConfig = require('./helper/validator.helper');

let authzConfig = {
debug: true,
Expand All @@ -14,7 +15,9 @@ function setConfig(config, force) {
if (force) {
logger.warn(FORCE_CONFIG);
}
// @TODO: validate format config

validateConfig(config);

authzConfig = {
set: true,
...config,
Expand Down
2 changes: 1 addition & 1 deletion lib/errors/error.messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = {
PERMISSION_CALL_FAILED: 'Failed to retrieve permissions.',
PERMISSION_ERROR: 'There was a permission error: ',
PERMISSION_FORMAT_INVALID: 'Permission service returned permissions in an unexpected format',
PERMISSION_MISSING: 'Missing permissions:',
PERMISSION_MISSING_ONEOF: 'Missing one of permissions:',
PERMISSION_MISSING: 'Missing permissions:',
SOURCE_INVALID: 'No valid datasource defined for permissions',
SOURCE_MISSING: 'No source defined for permissions',
TOKEN_MISSING: 'No authorization found in header.',
Expand Down
49 changes: 49 additions & 0 deletions lib/helper/validator.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const Joi = require('joi');

const configSchema = Joi.object({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vademo Please validate if Joi schema is correct 🙏

debug: Joi.boolean().optional(),
disabled: Joi.boolean().optional(),
cache: Joi.boolean().optional(),
tokenLocation: Joi.string().optional(),
source: Joi.string()
.valid('authzv2', 'meauthzv2', 'externalAuthz')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For backwards compatibility we also have meauthz which is an alias for meauthzv2 (not documented as it might be removed in the future).

.required(),
sources: Joi.when('source', {
switch: [
{
is: 'externalAuthz',
then: Joi.object({
externalAuthz: Joi.function().required(),
}),
},
{
is: 'authzv2',
then: Joi.object({
authzv2: Joi.object({
url: Joi.string().required(),
apiKey: Joi.string().required(),
applicationId: Joi.string().required(),
}).required(),
}),
},
{
is: 'meauthzv2',
then: Joi.object({
authzv2: Joi.object({
url: Joi.string().required(),
apiKey: Joi.string().required(),
applicationId: Joi.string().required(),
}).required(),
}),
},
],
}).required(),
}).required();

const validateConfig = (config) => {
const { error, value } = configSchema.validate(config);
if (error) throw error;
return value;
};

module.exports = validateConfig;
Loading