authenzify
Version:
server to manage authentication authorization of users and more
147 lines (138 loc) • 4.56 kB
JavaScript
import * as fs from 'fs'
import * as path from 'path'
import { ACTIVATE_USER_BY } from '../../src/constant.js'
const cleanQuotes = (value) => {
const [start, ...excludeStart] = `${value}`
if (start !== '"') {
return value
}
const [end, ...excludeStartEnd] = excludeStart.reverse()
return excludeStartEnd.reverse().join('')
}
const parseEnv = (env) => {
return env
.split('\n')
.map((keyValueString) => keyValueString.split('='))
.reduce(
(config, [key, value]) => ({ ...config, [key]: cleanQuotes(value) }),
{},
)
}
export const getConfig = async (configOption) => {
const __dirname = path.dirname(new URL(import.meta.url).pathname)
const privateKey = fs.readFileSync(
path.join(__dirname, '../keys/ec-private-key.pem'),
{
encoding: 'ascii',
},
)
const publicKey = fs.readFileSync(
path.join(__dirname, '../keys/ec-public-key.pem'),
{
encoding: 'ascii',
},
)
const env = fs.readFileSync(path.join(__dirname, '../../.env'), {
encoding: 'ascii',
})
const envParsed = parseEnv(env)
Object.entries(envParsed).forEach(([key, value]) => {
process.env[key] = process.env[key] || value
})
const templatesBasePath = path.join(__dirname, './templates/email')
const { GMAIL_PASSWORD, GMAIL_USER } = envParsed
const config = {
onSignUpFirstBasePermissions: [
'sign-up-editor',
'sign-in-editor',
'change-password-editor',
'change-password-viewer',
],
clientDomain: 'http://localhost:9090',
applicationName: 'Authenzify',
activationVerificationRoute:
'http://localhost:9090/users/verify/:id/activation',
domain: 'http://localhost:9090',
activateUserBy: ACTIVATE_USER_BY.AUTO,
passwordPolicy: '^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$',
usernamePolicy: '',
storage: {
type: 'mongodb',
uri: `mongodb://bongo-username:bongo-p2S4W0rD@localhost:27020`,
options: {
dbName: 'users-management',
},
},
saltLength: 32,
passwordPrivateKey: 'your-private-key',
privateKey,
publicKey,
jwtOptions: {
issuer: 'Authenzify corp',
subject: 'admin@authenzify.com',
audience: 'http://authenzify.com',
expiresIn: '12h',
algorithm: 'ES256',
},
authorizationCookieKey: 'Authorization',
...configOption,
setCookieOnSignIn: true,
emailProvider: {
provider: 'nodemailer',
settings: {
from: 'haim@tictuk.com',
host: 'gmail',
port: 587,
secure: false,
auth: {
user: GMAIL_USER,
pass: GMAIL_PASSWORD,
},
},
emailTemplates: {
activation: {
from: path.join(templatesBasePath, '/activation', '/from.ejs'),
html: path.join(templatesBasePath, '/activation', '/body.html'),
subject: path.join(templatesBasePath, '/activation', '/subject.ejs'),
},
forgotPassword: {
from: path.join(templatesBasePath, '/forgot-password', '/from.ejs'),
html: path.join(templatesBasePath, '/forgot-password', '/body.html'),
subject: path.join(
templatesBasePath,
'/forgot-password',
'/subject.ejs',
),
},
onVerification: {
from: path.join(templatesBasePath, '/on-verification', '/from.ejs'),
html: path.join(templatesBasePath, '/on-verification', '/body.html'),
subject: path.join(
templatesBasePath,
'/on-verification',
'/subject.ejs',
),
},
permissionsRequest: {
from: path.join(templatesBasePath, '/activation', '/from.ejs'),
html: path.join(templatesBasePath, '/activation', '/body.html'),
subject: path.join(templatesBasePath, '/activation', '/subject.ejs'),
},
permissionsApprovedToUser: {
from: path.join(templatesBasePath, '/activation', '/from.ejs'),
html: path.join(templatesBasePath, '/activation', '/body.html'),
subject: path.join(templatesBasePath, '/activation', '/subject.ejs'),
},
},
},
logger: false,
resetPasswordRoute: 'http://localhost:3003/users/verify/:id/reset-password',
didNotAskedToResetPasswordRoute:
'http://localhost:3003/users/verify/:id/did-not-asked-reset-password',
otpGenerator: function generate4DigitCode() {
return Math.floor(1000 + Math.random() * 9000).toString()
},
googleSignInClientId: '<test>',
}
return config
}