2017-10-25 13:49:37 +00:00
2017-04-12 17:57:55 +00:00
'use strict'
2018-03-25 22:30:17 +00:00
const crypto = require ( 'crypto' )
2017-04-12 17:57:55 +00:00
const fs = require ( 'fs' )
const path = require ( 'path' )
const { merge } = require ( 'lodash' )
const deepFreeze = require ( 'deep-freeze' )
const { Environment , Permission } = require ( './enum' )
2018-03-18 01:14:50 +00:00
const logger = require ( '../logger' )
2017-04-12 17:57:55 +00:00
const appRootPath = path . join ( _ _dirname , '../../' )
const env = process . env . NODE _ENV || Environment . development
const debugConfig = {
debug : ( env === Environment . development )
}
2018-01-26 13:00:48 +00:00
// Get version string from package.json
2018-01-19 12:23:30 +00:00
const { version } = require ( path . join ( appRootPath , 'package.json' ) )
2017-04-12 17:57:55 +00:00
const packageConfig = {
2018-01-19 12:23:30 +00:00
version : version ,
2017-04-12 17:57:55 +00:00
minimumCompatibleVersion : '0.5.0'
}
2018-01-26 13:00:48 +00:00
const configFilePath = path . join ( appRootPath , 'config.json' )
2017-04-12 17:57:55 +00:00
const fileConfig = fs . existsSync ( configFilePath ) ? require ( configFilePath ) [ env ] : undefined
let config = require ( './default' )
merge ( config , require ( './defaultSSL' ) )
2018-03-07 14:17:35 +00:00
merge ( config , require ( './oldDefault' ) )
2017-04-12 17:57:55 +00:00
merge ( config , debugConfig )
merge ( config , packageConfig )
merge ( config , fileConfig )
merge ( config , require ( './oldEnvironment' ) )
merge ( config , require ( './environment' ) )
merge ( config , require ( './dockerSecret' ) )
// load LDAP CA
if ( config . ldap . tlsca ) {
let ca = config . ldap . tlsca . split ( ',' )
let caContent = [ ]
for ( let i of ca ) {
2017-06-01 10:58:55 +00:00
if ( fs . existsSync ( i ) ) {
caContent . push ( fs . readFileSync ( i , 'utf8' ) )
2017-04-12 17:57:55 +00:00
}
}
let tlsOptions = {
ca : caContent
}
config . ldap . tlsOptions = config . ldap . tlsOptions ? Object . assign ( config . ldap . tlsOptions , tlsOptions ) : tlsOptions
}
// Permission
config . permission = Permission
2018-04-10 12:38:39 +00:00
if ( ! config . allowAnonymous && ! config . allowAnonymousEdits ) {
2017-04-12 17:57:55 +00:00
delete config . permission . freely
}
2018-03-07 14:17:35 +00:00
if ( ! ( config . defaultPermission in config . permission ) ) {
config . defaultPermission = config . permission . editable
2017-04-12 17:57:55 +00:00
}
// cache result, cannot change config in runtime!!!
config . isStandardHTTPsPort = ( function isStandardHTTPsPort ( ) {
2018-03-07 14:17:35 +00:00
return config . useSSL && config . port === 443
2017-04-12 17:57:55 +00:00
} ) ( )
config . isStandardHTTPPort = ( function isStandardHTTPPort ( ) {
2018-03-07 14:17:35 +00:00
return ! config . useSSL && config . port === 80
2017-04-12 17:57:55 +00:00
} ) ( )
// cache serverURL
2018-03-07 14:17:35 +00:00
config . serverURL = ( function getserverurl ( ) {
2017-04-12 17:57:55 +00:00
var url = ''
if ( config . domain ) {
2018-03-07 14:17:35 +00:00
var protocol = config . protocolUseSSL ? 'https://' : 'http://'
2017-04-12 17:57:55 +00:00
url = protocol + config . domain
2018-03-07 14:17:35 +00:00
if ( config . urlAddPort ) {
2017-04-12 17:57:55 +00:00
if ( ! config . isStandardHTTPPort || ! config . isStandardHTTPsPort ) {
url += ':' + config . port
}
}
}
2018-03-07 14:17:35 +00:00
if ( config . urlPath ) {
url += '/' + config . urlPath
2017-04-12 17:57:55 +00:00
}
return url
} ) ( )
config . Environment = Environment
// auth method
config . isFacebookEnable = config . facebook . clientID && config . facebook . clientSecret
config . isGoogleEnable = config . google . clientID && config . google . clientSecret
config . isDropboxEnable = config . dropbox . clientID && config . dropbox . clientSecret
config . isTwitterEnable = config . twitter . consumerKey && config . twitter . consumerSecret
config . isEmailEnable = config . email
config . isGitHubEnable = config . github . clientID && config . github . clientSecret
config . isGitLabEnable = config . gitlab . clientID && config . gitlab . clientSecret
2017-10-29 10:16:40 +00:00
config . isMattermostEnable = config . mattermost . clientID && config . mattermost . clientSecret
2017-04-12 17:57:55 +00:00
config . isLDAPEnable = config . ldap . url
2017-11-28 03:46:58 +00:00
config . isSAMLEnable = config . saml . idpSsoUrl
2018-03-07 14:17:35 +00:00
config . isPDFExportEnable = config . allowPDFExport
2017-04-12 17:57:55 +00:00
2018-01-26 13:00:48 +00:00
// merge legacy values
2018-03-07 14:17:35 +00:00
let keys = Object . keys ( config )
const uppercase = /[A-Z]/
for ( let i = keys . length ; i -- ; ) {
let lowercaseKey = keys [ i ] . toLowerCase ( )
// if the config contains uppercase letters
// and a lowercase version of this setting exists
// and the config with uppercase is not set
// we set the new config using the old key.
if ( uppercase . test ( keys [ i ] ) &&
2018-03-26 18:49:24 +00:00
config [ lowercaseKey ] !== undefined &&
fileConfig [ keys [ i ] ] === undefined ) {
2018-03-07 14:17:35 +00:00
logger . warn ( 'config.js contains deprecated lowercase setting for ' + keys [ i ] + '. Please change your config.js file to replace ' + lowercaseKey + ' with ' + keys [ i ] )
config [ keys [ i ] ] = config [ lowercaseKey ]
}
2018-01-26 13:00:48 +00:00
}
2018-03-25 22:30:17 +00:00
// Generate session secret if it stays on default values
if ( config . sessionSecret === 'secret' ) {
logger . warn ( 'Session secret not set. Using random generated one. Please set `sessionSecret` in your config.js file. All users will be logged out.' )
config . sessionSecret = crypto . randomBytes ( Math . ceil ( config . sessionSecretLen / 2 ) ) // generate crypto graphic random number
. toString ( 'hex' ) // convert to hexadecimal format
. slice ( 0 , config . sessionSecretLen ) // return required number of characters
}
2018-03-18 01:14:50 +00:00
// Validate upload upload providers
2018-03-07 14:17:35 +00:00
if ( [ 'filesystem' , 's3' , 'minio' , 'imgur' ] . indexOf ( config . imageUploadType ) === - 1 ) {
2018-03-18 01:14:50 +00:00
logger . error ( '"imageuploadtype" is not correctly set. Please use "filesystem", "s3", "minio" or "imgur". Defaulting to "imgur"' )
2018-03-07 14:17:35 +00:00
config . imageUploadType = 'imgur'
2018-03-18 01:14:50 +00:00
}
2018-01-20 14:08:31 +00:00
// figure out mime types for image uploads
2018-03-07 14:17:35 +00:00
switch ( config . imageUploadType ) {
2018-01-20 14:08:31 +00:00
case 'imgur' :
config . allowedUploadMimeTypes = [
'image/jpeg' ,
'image/png' ,
'image/jpg' ,
'image/gif'
]
break
default :
config . allowedUploadMimeTypes = [
'image/jpeg' ,
'image/png' ,
'image/jpg' ,
'image/gif' ,
'image/svg+xml'
]
}
2017-04-12 17:57:55 +00:00
// generate correct path
2018-03-07 14:17:35 +00:00
config . sslCAPath . forEach ( function ( capath , i , array ) {
2017-12-22 11:25:13 +00:00
array [ i ] = path . resolve ( appRootPath , capath )
2017-12-22 11:19:19 +00:00
} )
2017-11-28 13:23:50 +00:00
2018-03-07 14:17:35 +00:00
config . sslCertPath = path . join ( appRootPath , config . sslCertPath )
config . sslKeyPath = path . join ( appRootPath , config . sslKeyPath )
config . dhParamPath = path . join ( appRootPath , config . dhParamPath )
config . tmpPath = path . join ( appRootPath , config . tmpPath )
config . defaultNotePath = path . join ( appRootPath , config . defaultNotePath )
config . docsPath = path . join ( appRootPath , config . docsPath )
config . indexPath = path . join ( appRootPath , config . indexPath )
config . hackmdPath = path . join ( appRootPath , config . hackmdPath )
config . errorPath = path . join ( appRootPath , config . errorPath )
config . prettyPath = path . join ( appRootPath , config . prettyPath )
config . slidePath = path . join ( appRootPath , config . slidePath )
2017-04-12 17:57:55 +00:00
2017-10-25 13:49:37 +00:00
// make config readonly
2017-04-12 17:57:55 +00:00
config = deepFreeze ( config )
module . exports = config