167
src/Config.js
167
src/Config.js
@@ -11,7 +11,7 @@ function removeTrailingSlash(str) {
|
||||
if (!str) {
|
||||
return str;
|
||||
}
|
||||
if (str.endsWith("/")) {
|
||||
if (str.endsWith('/')) {
|
||||
str = str.substr(0, str.length - 1);
|
||||
}
|
||||
return str;
|
||||
@@ -25,19 +25,28 @@ export class Config {
|
||||
}
|
||||
const config = new Config();
|
||||
config.applicationId = applicationId;
|
||||
Object.keys(cacheInfo).forEach((key) => {
|
||||
Object.keys(cacheInfo).forEach(key => {
|
||||
if (key == 'databaseController') {
|
||||
const schemaCache = new SchemaCache(cacheInfo.cacheController,
|
||||
const schemaCache = new SchemaCache(
|
||||
cacheInfo.cacheController,
|
||||
cacheInfo.schemaCacheTTL,
|
||||
cacheInfo.enableSingleSchemaCache);
|
||||
config.database = new DatabaseController(cacheInfo.databaseController.adapter, schemaCache);
|
||||
cacheInfo.enableSingleSchemaCache
|
||||
);
|
||||
config.database = new DatabaseController(
|
||||
cacheInfo.databaseController.adapter,
|
||||
schemaCache
|
||||
);
|
||||
} else {
|
||||
config[key] = cacheInfo[key];
|
||||
}
|
||||
});
|
||||
config.mount = removeTrailingSlash(mount);
|
||||
config.generateSessionExpiresAt = config.generateSessionExpiresAt.bind(config);
|
||||
config.generateEmailVerifyTokenExpiresAt = config.generateEmailVerifyTokenExpiresAt.bind(config);
|
||||
config.generateSessionExpiresAt = config.generateSessionExpiresAt.bind(
|
||||
config
|
||||
);
|
||||
config.generateEmailVerifyTokenExpiresAt = config.generateEmailVerifyTokenExpiresAt.bind(
|
||||
config
|
||||
);
|
||||
return config;
|
||||
}
|
||||
|
||||
@@ -64,14 +73,18 @@ export class Config {
|
||||
masterKey,
|
||||
readOnlyMasterKey,
|
||||
}) {
|
||||
|
||||
if (masterKey === readOnlyMasterKey) {
|
||||
throw new Error('masterKey and readOnlyMasterKey should be different');
|
||||
}
|
||||
|
||||
const emailAdapter = userController.adapter;
|
||||
if (verifyUserEmails) {
|
||||
this.validateEmailConfiguration({emailAdapter, appName, publicServerURL, emailVerifyTokenValidityDuration});
|
||||
this.validateEmailConfiguration({
|
||||
emailAdapter,
|
||||
appName,
|
||||
publicServerURL,
|
||||
emailVerifyTokenValidityDuration,
|
||||
});
|
||||
}
|
||||
|
||||
this.validateAccountLockoutPolicy(accountLockout);
|
||||
@@ -83,8 +96,11 @@ export class Config {
|
||||
}
|
||||
|
||||
if (publicServerURL) {
|
||||
if (!publicServerURL.startsWith("http://") && !publicServerURL.startsWith("https://")) {
|
||||
throw "publicServerURL should be a valid HTTPS URL starting with https://"
|
||||
if (
|
||||
!publicServerURL.startsWith('http://') &&
|
||||
!publicServerURL.startsWith('https://')
|
||||
) {
|
||||
throw 'publicServerURL should be a valid HTTPS URL starting with https://';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,11 +113,19 @@ export class Config {
|
||||
|
||||
static validateAccountLockoutPolicy(accountLockout) {
|
||||
if (accountLockout) {
|
||||
if (typeof accountLockout.duration !== 'number' || accountLockout.duration <= 0 || accountLockout.duration > 99999) {
|
||||
if (
|
||||
typeof accountLockout.duration !== 'number' ||
|
||||
accountLockout.duration <= 0 ||
|
||||
accountLockout.duration > 99999
|
||||
) {
|
||||
throw 'Account lockout duration should be greater than 0 and less than 100000';
|
||||
}
|
||||
|
||||
if (!Number.isInteger(accountLockout.threshold) || accountLockout.threshold < 1 || accountLockout.threshold > 999) {
|
||||
if (
|
||||
!Number.isInteger(accountLockout.threshold) ||
|
||||
accountLockout.threshold < 1 ||
|
||||
accountLockout.threshold > 999
|
||||
) {
|
||||
throw 'Account lockout threshold should be an integer greater than 0 and less than 1000';
|
||||
}
|
||||
}
|
||||
@@ -109,33 +133,52 @@ export class Config {
|
||||
|
||||
static validatePasswordPolicy(passwordPolicy) {
|
||||
if (passwordPolicy) {
|
||||
if (passwordPolicy.maxPasswordAge !== undefined && (typeof passwordPolicy.maxPasswordAge !== 'number' || passwordPolicy.maxPasswordAge < 0)) {
|
||||
if (
|
||||
passwordPolicy.maxPasswordAge !== undefined &&
|
||||
(typeof passwordPolicy.maxPasswordAge !== 'number' ||
|
||||
passwordPolicy.maxPasswordAge < 0)
|
||||
) {
|
||||
throw 'passwordPolicy.maxPasswordAge must be a positive number';
|
||||
}
|
||||
|
||||
if (passwordPolicy.resetTokenValidityDuration !== undefined && (typeof passwordPolicy.resetTokenValidityDuration !== 'number' || passwordPolicy.resetTokenValidityDuration <= 0)) {
|
||||
if (
|
||||
passwordPolicy.resetTokenValidityDuration !== undefined &&
|
||||
(typeof passwordPolicy.resetTokenValidityDuration !== 'number' ||
|
||||
passwordPolicy.resetTokenValidityDuration <= 0)
|
||||
) {
|
||||
throw 'passwordPolicy.resetTokenValidityDuration must be a positive number';
|
||||
}
|
||||
|
||||
if(passwordPolicy.validatorPattern){
|
||||
if(typeof(passwordPolicy.validatorPattern) === 'string') {
|
||||
passwordPolicy.validatorPattern = new RegExp(passwordPolicy.validatorPattern);
|
||||
}
|
||||
else if(!(passwordPolicy.validatorPattern instanceof RegExp)){
|
||||
if (passwordPolicy.validatorPattern) {
|
||||
if (typeof passwordPolicy.validatorPattern === 'string') {
|
||||
passwordPolicy.validatorPattern = new RegExp(
|
||||
passwordPolicy.validatorPattern
|
||||
);
|
||||
} else if (!(passwordPolicy.validatorPattern instanceof RegExp)) {
|
||||
throw 'passwordPolicy.validatorPattern must be a regex string or RegExp object.';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(passwordPolicy.validatorCallback && typeof passwordPolicy.validatorCallback !== 'function') {
|
||||
if (
|
||||
passwordPolicy.validatorCallback &&
|
||||
typeof passwordPolicy.validatorCallback !== 'function'
|
||||
) {
|
||||
throw 'passwordPolicy.validatorCallback must be a function.';
|
||||
}
|
||||
|
||||
if(passwordPolicy.doNotAllowUsername && typeof passwordPolicy.doNotAllowUsername !== 'boolean') {
|
||||
if (
|
||||
passwordPolicy.doNotAllowUsername &&
|
||||
typeof passwordPolicy.doNotAllowUsername !== 'boolean'
|
||||
) {
|
||||
throw 'passwordPolicy.doNotAllowUsername must be a boolean value.';
|
||||
}
|
||||
|
||||
if (passwordPolicy.maxPasswordHistory && (!Number.isInteger(passwordPolicy.maxPasswordHistory) || passwordPolicy.maxPasswordHistory <= 0 || passwordPolicy.maxPasswordHistory > 20)) {
|
||||
if (
|
||||
passwordPolicy.maxPasswordHistory &&
|
||||
(!Number.isInteger(passwordPolicy.maxPasswordHistory) ||
|
||||
passwordPolicy.maxPasswordHistory <= 0 ||
|
||||
passwordPolicy.maxPasswordHistory > 20)
|
||||
) {
|
||||
throw 'passwordPolicy.maxPasswordHistory must be an integer ranging 0 - 20';
|
||||
}
|
||||
}
|
||||
@@ -144,13 +187,18 @@ export class Config {
|
||||
// if the passwordPolicy.validatorPattern is configured then setup a callback to process the pattern
|
||||
static setupPasswordValidator(passwordPolicy) {
|
||||
if (passwordPolicy && passwordPolicy.validatorPattern) {
|
||||
passwordPolicy.patternValidator = (value) => {
|
||||
passwordPolicy.patternValidator = value => {
|
||||
return passwordPolicy.validatorPattern.test(value);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
static validateEmailConfiguration({emailAdapter, appName, publicServerURL, emailVerifyTokenValidityDuration}) {
|
||||
static validateEmailConfiguration({
|
||||
emailAdapter,
|
||||
appName,
|
||||
publicServerURL,
|
||||
emailVerifyTokenValidityDuration,
|
||||
}) {
|
||||
if (!emailAdapter) {
|
||||
throw 'An emailAdapter is required for e-mail verification and password resets.';
|
||||
}
|
||||
@@ -164,14 +212,14 @@ export class Config {
|
||||
if (isNaN(emailVerifyTokenValidityDuration)) {
|
||||
throw 'Email verify token validity duration must be a valid number.';
|
||||
} else if (emailVerifyTokenValidityDuration <= 0) {
|
||||
throw 'Email verify token validity duration must be a value greater than 0.'
|
||||
throw 'Email verify token validity duration must be a value greater than 0.';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static validateMasterKeyIps(masterKeyIps) {
|
||||
for (const ip of masterKeyIps) {
|
||||
if(!net.isIP(ip)){
|
||||
if (!net.isIP(ip)) {
|
||||
throw `Invalid ip in masterKeyIps: ${ip}`;
|
||||
}
|
||||
}
|
||||
@@ -193,16 +241,15 @@ export class Config {
|
||||
if (expireInactiveSessions) {
|
||||
if (isNaN(sessionLength)) {
|
||||
throw 'Session length must be a valid number.';
|
||||
}
|
||||
else if (sessionLength <= 0) {
|
||||
throw 'Session length must be a value greater than 0.'
|
||||
} else if (sessionLength <= 0) {
|
||||
throw 'Session length must be a value greater than 0.';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static validateMaxLimit(maxLimit) {
|
||||
if (maxLimit <= 0) {
|
||||
throw 'Max limit must be a value greater than 0.'
|
||||
throw 'Max limit must be a value greater than 0.';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,15 +258,22 @@ export class Config {
|
||||
return undefined;
|
||||
}
|
||||
var now = new Date();
|
||||
return new Date(now.getTime() + (this.emailVerifyTokenValidityDuration * 1000));
|
||||
return new Date(
|
||||
now.getTime() + this.emailVerifyTokenValidityDuration * 1000
|
||||
);
|
||||
}
|
||||
|
||||
generatePasswordResetTokenExpiresAt() {
|
||||
if (!this.passwordPolicy || !this.passwordPolicy.resetTokenValidityDuration) {
|
||||
if (
|
||||
!this.passwordPolicy ||
|
||||
!this.passwordPolicy.resetTokenValidityDuration
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
const now = new Date();
|
||||
return new Date(now.getTime() + (this.passwordPolicy.resetTokenValidityDuration * 1000));
|
||||
return new Date(
|
||||
now.getTime() + this.passwordPolicy.resetTokenValidityDuration * 1000
|
||||
);
|
||||
}
|
||||
|
||||
generateSessionExpiresAt() {
|
||||
@@ -227,39 +281,62 @@ export class Config {
|
||||
return undefined;
|
||||
}
|
||||
var now = new Date();
|
||||
return new Date(now.getTime() + (this.sessionLength * 1000));
|
||||
return new Date(now.getTime() + this.sessionLength * 1000);
|
||||
}
|
||||
|
||||
get invalidLinkURL() {
|
||||
return this.customPages.invalidLink || `${this.publicServerURL}/apps/invalid_link.html`;
|
||||
return (
|
||||
this.customPages.invalidLink ||
|
||||
`${this.publicServerURL}/apps/invalid_link.html`
|
||||
);
|
||||
}
|
||||
|
||||
get invalidVerificationLinkURL() {
|
||||
return this.customPages.invalidVerificationLink || `${this.publicServerURL}/apps/invalid_verification_link.html`;
|
||||
return (
|
||||
this.customPages.invalidVerificationLink ||
|
||||
`${this.publicServerURL}/apps/invalid_verification_link.html`
|
||||
);
|
||||
}
|
||||
|
||||
get linkSendSuccessURL() {
|
||||
return this.customPages.linkSendSuccess || `${this.publicServerURL}/apps/link_send_success.html`
|
||||
return (
|
||||
this.customPages.linkSendSuccess ||
|
||||
`${this.publicServerURL}/apps/link_send_success.html`
|
||||
);
|
||||
}
|
||||
|
||||
get linkSendFailURL() {
|
||||
return this.customPages.linkSendFail || `${this.publicServerURL}/apps/link_send_fail.html`
|
||||
return (
|
||||
this.customPages.linkSendFail ||
|
||||
`${this.publicServerURL}/apps/link_send_fail.html`
|
||||
);
|
||||
}
|
||||
|
||||
get verifyEmailSuccessURL() {
|
||||
return this.customPages.verifyEmailSuccess || `${this.publicServerURL}/apps/verify_email_success.html`;
|
||||
return (
|
||||
this.customPages.verifyEmailSuccess ||
|
||||
`${this.publicServerURL}/apps/verify_email_success.html`
|
||||
);
|
||||
}
|
||||
|
||||
get choosePasswordURL() {
|
||||
return this.customPages.choosePassword || `${this.publicServerURL}/apps/choose_password`;
|
||||
return (
|
||||
this.customPages.choosePassword ||
|
||||
`${this.publicServerURL}/apps/choose_password`
|
||||
);
|
||||
}
|
||||
|
||||
get requestResetPasswordURL() {
|
||||
return `${this.publicServerURL}/apps/${this.applicationId}/request_password_reset`;
|
||||
return `${this.publicServerURL}/apps/${
|
||||
this.applicationId
|
||||
}/request_password_reset`;
|
||||
}
|
||||
|
||||
get passwordResetSuccessURL() {
|
||||
return this.customPages.passwordResetSuccess || `${this.publicServerURL}/apps/password_reset_success.html`;
|
||||
return (
|
||||
this.customPages.passwordResetSuccess ||
|
||||
`${this.publicServerURL}/apps/password_reset_success.html`
|
||||
);
|
||||
}
|
||||
|
||||
get parseFrameURL() {
|
||||
|
||||
Reference in New Issue
Block a user