Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extract out auth config to separate file #30

Merged
merged 5 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
25 changes: 25 additions & 0 deletions app/config/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Joi = require('joi')

const schema = Joi.object({
enabled: Joi.boolean().default(false),
clientSecret: Joi.string().allow(''),
clientId: Joi.string().allow(''),
authority: Joi.string().allow(''),
redirectUrl: Joi.string().default('http://localhost:3002/authenticate')
})

const config = {
enabled: process.env.AADAR_ENABLED,
clientSecret: process.env.AADAR_CLIENT_SECRET,
clientId: process.env.AADAR_CLIENT_ID,
authority: `https://login.microsoftonline.com/${process.env.AADAR_TENANT_ID}`,
redirectUrl: process.env.AADAR_REDIRECT_URL
}

const { error, value } = schema.validate(config, { abortEarly: false })

if (error) {
throw new Error(`The auth config is invalid. ${error.message}`)
}

module.exports = value
18 changes: 4 additions & 14 deletions app/config/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Joi = require('joi')
const authConfig = require('./auth')

const schema = Joi.object({
cache: {
expiresIn: Joi.number().default(1000 * 3600 * 24 * 3), // 3 days
Expand Down Expand Up @@ -28,13 +30,6 @@ const schema = Joi.object({
path: Joi.string().default('/'),
ttl: Joi.number().default(1000 * 60 * 60 * 24 * 365) // 1 year
},
auth: {
enabled: Joi.boolean().default(false),
clientSecret: Joi.string().allow(''),
clientId: Joi.string().allow(''),
authority: Joi.string().allow(''),
redirectUrl: Joi.string().default('http://localhost:3002/authenticate')
},
env: Joi.string().valid('development', 'test', 'production').default('development'),
isDev: Joi.boolean().default(false),
isProd: Joi.boolean().default(false),
Expand Down Expand Up @@ -72,13 +67,6 @@ const config = {
isSecure: process.env.NODE_ENV === 'production',
password: process.env.COOKIE_PASSWORD
},
auth: {
enabled: process.env.AADAR_ENABLED,
clientSecret: process.env.AADAR_CLIENT_SECRET,
clientId: process.env.AADAR_CLIENT_ID,
authority: `https://login.microsoftonline.com/${process.env.AADAR_TENANT_ID}`,
redirectUrl: process.env.AADAR_REDIRECT_URL
},
env: process.env.NODE_ENV,
isDev: process.env.NODE_ENV === 'development',
isProd: process.env.NODE_ENV === 'production',
Expand All @@ -95,4 +83,6 @@ if (error) {
throw new Error(`The server config is invalid. ${error.message}`)
}

value.auth = authConfig

module.exports = value
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ffc-ahwr-backoffice",
"version": "1.8.2",
"version": "1.8.3",
"description": "Back office of the health and welfare of your livestock",
"homepage": "https://github.com/DEFRA/ffc-ahwr-backoffice",
"main": "app/index.js",
Expand Down
27 changes: 27 additions & 0 deletions test/unit/config/auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const authConfig = require('../../../app/config/auth')

describe('cache Config Test', () => {
const OLD_ENV = process.env

beforeEach(() => {
jest.resetModules()
process.env = { ...OLD_ENV }
})

afterAll(() => {
process.env = OLD_ENV
})
test('Should pass validation for all fields populated', async () => {
expect(authConfig).toBeDefined()
})

test('Invalid env var throws error', () => {
try {
process.env.AADAR_CLIENT_SECRET = null
process.env.AADAR_CLIENT_ID = null
require('../../../app/config/auth')
} catch (err) {
expect(err.message).toBe('The auth config is invalid. "clientSecret" must be a string. "clientId" must be a string')
}
})
})