2023-10-22 22:53:05 +02:00
|
|
|
const packageJson = require('../../../package.json')
|
2022-10-22 16:01:00 +02:00
|
|
|
const { BookshelfView } = require('../../utils/constants')
|
2022-04-15 00:15:52 +02:00
|
|
|
const Logger = require('../../Logger')
|
2021-09-22 03:57:33 +02:00
|
|
|
|
2021-09-05 02:58:39 +02:00
|
|
|
class ServerSettings {
|
|
|
|
constructor(settings) {
|
|
|
|
this.id = 'server-settings'
|
2022-07-19 00:19:16 +02:00
|
|
|
this.tokenSecret = null
|
2021-09-29 17:16:38 +02:00
|
|
|
|
2021-10-09 00:30:20 +02:00
|
|
|
// Scanner
|
2021-09-05 02:58:39 +02:00
|
|
|
this.scannerParseSubtitle = false
|
2021-10-05 05:11:42 +02:00
|
|
|
this.scannerFindCovers = false
|
2022-01-09 00:03:33 +01:00
|
|
|
this.scannerCoverProvider = 'google'
|
2022-05-23 04:56:51 +02:00
|
|
|
this.scannerPreferMatchedMetadata = false
|
2022-06-17 10:26:10 +02:00
|
|
|
this.scannerDisableWatcher = false
|
2021-10-09 00:30:20 +02:00
|
|
|
|
2022-04-12 23:05:16 +02:00
|
|
|
// Metadata - choose to store inside users library item folder
|
|
|
|
this.storeCoverWithItem = false
|
|
|
|
this.storeMetadataWithItem = false
|
2023-05-16 01:23:31 +02:00
|
|
|
this.metadataFileFormat = 'json'
|
2021-10-09 00:30:20 +02:00
|
|
|
|
|
|
|
// Security/Rate limits
|
2021-09-29 17:16:38 +02:00
|
|
|
this.rateLimitLoginRequests = 10
|
|
|
|
this.rateLimitLoginWindow = 10 * 60 * 1000 // 10 Minutes
|
2021-10-09 00:30:20 +02:00
|
|
|
|
|
|
|
// Backups
|
2022-10-22 16:01:00 +02:00
|
|
|
this.backupSchedule = false // If false then auto-backups are disabled
|
2021-10-09 00:30:20 +02:00
|
|
|
this.backupsToKeep = 2
|
2022-04-23 19:19:31 +02:00
|
|
|
this.maxBackupSize = 1
|
2021-10-09 00:30:20 +02:00
|
|
|
|
2021-10-31 23:55:28 +01:00
|
|
|
// Logger
|
|
|
|
this.loggerDailyLogsToKeep = 7
|
|
|
|
this.loggerScannerLogsToKeep = 2
|
|
|
|
|
2022-08-14 01:18:42 +02:00
|
|
|
// Bookshelf Display
|
2023-01-28 23:17:31 +01:00
|
|
|
this.homeBookshelfView = BookshelfView.DETAIL
|
2022-10-22 16:13:20 +02:00
|
|
|
this.bookshelfView = BookshelfView.DETAIL
|
2021-12-02 22:49:03 +01:00
|
|
|
|
2022-03-26 17:59:34 +01:00
|
|
|
// Podcasts
|
|
|
|
this.podcastEpisodeSchedule = '0 * * * *' // Every hour
|
|
|
|
|
2022-05-20 23:34:51 +02:00
|
|
|
// Sorting
|
2022-02-13 22:00:59 +01:00
|
|
|
this.sortingIgnorePrefix = false
|
2023-09-08 19:32:30 +02:00
|
|
|
this.sortingPrefixes = ['the', 'a']
|
2022-03-31 22:07:50 +02:00
|
|
|
|
2022-05-20 23:34:51 +02:00
|
|
|
// Misc Flags
|
2022-02-23 00:33:55 +01:00
|
|
|
this.chromecastEnabled = false
|
2022-06-18 02:09:03 +02:00
|
|
|
this.dateFormat = 'MM/dd/yyyy'
|
2023-02-27 19:04:26 +01:00
|
|
|
this.timeFormat = 'HH:mm'
|
2022-11-09 01:09:07 +01:00
|
|
|
this.language = 'en-us'
|
2022-05-20 23:34:51 +02:00
|
|
|
|
2021-10-01 01:52:32 +02:00
|
|
|
this.logLevel = Logger.logLevel
|
2022-05-20 23:34:51 +02:00
|
|
|
|
2023-10-22 22:53:05 +02:00
|
|
|
this.version = packageJson.version
|
|
|
|
this.buildNumber = packageJson.buildNumber
|
2021-09-05 02:58:39 +02:00
|
|
|
|
2023-03-24 18:21:25 +01:00
|
|
|
// Auth settings
|
|
|
|
// Active auth methodes
|
|
|
|
this.authActiveAuthMethods = ['local']
|
|
|
|
|
2023-09-24 22:36:35 +02:00
|
|
|
// openid settings
|
2023-11-10 23:11:51 +01:00
|
|
|
this.authOpenIDIssuerURL = null
|
|
|
|
this.authOpenIDAuthorizationURL = null
|
|
|
|
this.authOpenIDTokenURL = null
|
|
|
|
this.authOpenIDUserInfoURL = null
|
|
|
|
this.authOpenIDJwksURL = null
|
|
|
|
this.authOpenIDLogoutURL = null
|
|
|
|
this.authOpenIDClientID = null
|
|
|
|
this.authOpenIDClientSecret = null
|
2023-11-02 19:55:01 +01:00
|
|
|
this.authOpenIDButtonText = 'Login with OpenId'
|
|
|
|
this.authOpenIDAutoLaunch = false
|
2023-11-08 21:45:29 +01:00
|
|
|
this.authOpenIDAutoRegister = false
|
|
|
|
this.authOpenIDMatchExistingBy = null
|
2023-03-24 18:21:25 +01:00
|
|
|
|
2021-09-05 02:58:39 +02:00
|
|
|
if (settings) {
|
|
|
|
this.construct(settings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(settings) {
|
2022-07-19 00:19:16 +02:00
|
|
|
this.tokenSecret = settings.tokenSecret
|
2021-10-05 05:11:42 +02:00
|
|
|
this.scannerFindCovers = !!settings.scannerFindCovers
|
2022-01-09 00:03:33 +01:00
|
|
|
this.scannerCoverProvider = settings.scannerCoverProvider || 'google'
|
2021-09-05 02:58:39 +02:00
|
|
|
this.scannerParseSubtitle = settings.scannerParseSubtitle
|
2022-05-23 04:56:51 +02:00
|
|
|
this.scannerPreferMatchedMetadata = !!settings.scannerPreferMatchedMetadata
|
2022-02-24 00:52:21 +01:00
|
|
|
this.scannerDisableWatcher = !!settings.scannerDisableWatcher
|
2021-11-26 01:39:02 +01:00
|
|
|
|
2022-04-12 23:05:16 +02:00
|
|
|
this.storeCoverWithItem = !!settings.storeCoverWithItem
|
|
|
|
this.storeMetadataWithItem = !!settings.storeMetadataWithItem
|
2023-05-16 01:23:31 +02:00
|
|
|
this.metadataFileFormat = settings.metadataFileFormat || 'json'
|
2022-02-27 19:47:56 +01:00
|
|
|
|
2021-09-29 17:16:38 +02:00
|
|
|
this.rateLimitLoginRequests = !isNaN(settings.rateLimitLoginRequests) ? Number(settings.rateLimitLoginRequests) : 10
|
|
|
|
this.rateLimitLoginWindow = !isNaN(settings.rateLimitLoginWindow) ? Number(settings.rateLimitLoginWindow) : 10 * 60 * 1000 // 10 Minutes
|
2021-10-09 00:30:20 +02:00
|
|
|
|
|
|
|
this.backupSchedule = settings.backupSchedule || false
|
|
|
|
this.backupsToKeep = settings.backupsToKeep || 2
|
2022-04-29 23:42:40 +02:00
|
|
|
this.maxBackupSize = settings.maxBackupSize || 1
|
2021-10-09 00:30:20 +02:00
|
|
|
|
2021-10-31 23:55:28 +01:00
|
|
|
this.loggerDailyLogsToKeep = settings.loggerDailyLogsToKeep || 7
|
|
|
|
this.loggerScannerLogsToKeep = settings.loggerScannerLogsToKeep || 2
|
|
|
|
|
2022-08-14 01:18:42 +02:00
|
|
|
this.homeBookshelfView = settings.homeBookshelfView || BookshelfView.STANDARD
|
2021-12-28 22:50:17 +01:00
|
|
|
this.bookshelfView = settings.bookshelfView || BookshelfView.STANDARD
|
2021-12-02 22:49:03 +01:00
|
|
|
|
2022-02-13 22:00:59 +01:00
|
|
|
this.sortingIgnorePrefix = !!settings.sortingIgnorePrefix
|
2022-10-28 00:50:56 +02:00
|
|
|
this.sortingPrefixes = settings.sortingPrefixes || ['the']
|
2022-02-23 00:33:55 +01:00
|
|
|
this.chromecastEnabled = !!settings.chromecastEnabled
|
2022-06-18 02:09:03 +02:00
|
|
|
this.dateFormat = settings.dateFormat || 'MM/dd/yyyy'
|
2023-02-27 19:04:26 +01:00
|
|
|
this.timeFormat = settings.timeFormat || 'HH:mm'
|
2022-11-09 01:09:07 +01:00
|
|
|
this.language = settings.language || 'en-us'
|
2021-10-01 01:52:32 +02:00
|
|
|
this.logLevel = settings.logLevel || Logger.logLevel
|
2021-11-16 03:09:42 +01:00
|
|
|
this.version = settings.version || null
|
2023-10-22 22:53:05 +02:00
|
|
|
this.buildNumber = settings.buildNumber || 0 // Added v2.4.5
|
2021-10-01 01:52:32 +02:00
|
|
|
|
2023-03-24 18:21:25 +01:00
|
|
|
this.authActiveAuthMethods = settings.authActiveAuthMethods || ['local']
|
2023-11-10 23:11:51 +01:00
|
|
|
|
|
|
|
this.authOpenIDIssuerURL = settings.authOpenIDIssuerURL || null
|
|
|
|
this.authOpenIDAuthorizationURL = settings.authOpenIDAuthorizationURL || null
|
|
|
|
this.authOpenIDTokenURL = settings.authOpenIDTokenURL || null
|
|
|
|
this.authOpenIDUserInfoURL = settings.authOpenIDUserInfoURL || null
|
|
|
|
this.authOpenIDJwksURL = settings.authOpenIDJwksURL || null
|
|
|
|
this.authOpenIDLogoutURL = settings.authOpenIDLogoutURL || null
|
|
|
|
this.authOpenIDClientID = settings.authOpenIDClientID || null
|
|
|
|
this.authOpenIDClientSecret = settings.authOpenIDClientSecret || null
|
2023-11-02 19:55:01 +01:00
|
|
|
this.authOpenIDButtonText = settings.authOpenIDButtonText || 'Login with OpenId'
|
|
|
|
this.authOpenIDAutoLaunch = !!settings.authOpenIDAutoLaunch
|
2023-11-08 21:45:29 +01:00
|
|
|
this.authOpenIDAutoRegister = !!settings.authOpenIDAutoRegister
|
|
|
|
this.authOpenIDMatchExistingBy = settings.authOpenIDMatchExistingBy || null
|
2023-04-14 20:26:29 +02:00
|
|
|
|
2023-03-24 18:21:25 +01:00
|
|
|
if (!Array.isArray(this.authActiveAuthMethods)) {
|
|
|
|
this.authActiveAuthMethods = ['local']
|
|
|
|
}
|
|
|
|
|
2023-11-02 19:55:01 +01:00
|
|
|
// remove uninitialized methods
|
2023-04-14 20:26:29 +02:00
|
|
|
// OpenID
|
2023-09-24 22:36:35 +02:00
|
|
|
if (this.authActiveAuthMethods.includes('openid') && (
|
2023-11-10 23:11:51 +01:00
|
|
|
!this.authOpenIDIssuerURL ||
|
|
|
|
!this.authOpenIDAuthorizationURL ||
|
|
|
|
!this.authOpenIDTokenURL ||
|
|
|
|
!this.authOpenIDUserInfoURL ||
|
|
|
|
!this.authOpenIDJwksURL ||
|
|
|
|
!this.authOpenIDClientID ||
|
|
|
|
!this.authOpenIDClientSecret
|
2023-04-14 20:26:29 +02:00
|
|
|
)) {
|
2023-09-24 22:36:35 +02:00
|
|
|
this.authActiveAuthMethods.splice(this.authActiveAuthMethods.indexOf('openid', 0), 1)
|
2023-04-14 20:26:29 +02:00
|
|
|
}
|
|
|
|
|
2023-03-24 18:21:25 +01:00
|
|
|
// fallback to local
|
|
|
|
if (!Array.isArray(this.authActiveAuthMethods) || this.authActiveAuthMethods.length == 0) {
|
|
|
|
this.authActiveAuthMethods = ['local']
|
|
|
|
}
|
|
|
|
|
2022-08-14 01:18:42 +02:00
|
|
|
// Migrations
|
|
|
|
if (settings.storeCoverWithBook != undefined) { // storeCoverWithBook was renamed to storeCoverWithItem in 2.0.0
|
|
|
|
this.storeCoverWithItem = !!settings.storeCoverWithBook
|
|
|
|
}
|
|
|
|
if (settings.storeMetadataWithBook != undefined) { // storeMetadataWithBook was renamed to storeMetadataWithItem in 2.0.0
|
|
|
|
this.storeMetadataWithItem = !!settings.storeMetadataWithBook
|
|
|
|
}
|
|
|
|
if (settings.homeBookshelfView == undefined) { // homeBookshelfView was added in 2.1.3
|
|
|
|
this.homeBookshelfView = settings.bookshelfView
|
|
|
|
}
|
2023-05-16 01:23:31 +02:00
|
|
|
if (settings.metadataFileFormat == undefined) { // metadataFileFormat was added in 2.2.21
|
|
|
|
// All users using old settings will stay abs until changed
|
|
|
|
this.metadataFileFormat = 'abs'
|
|
|
|
}
|
|
|
|
|
2023-10-22 22:53:05 +02:00
|
|
|
// As of v2.4.5 only json is supported
|
|
|
|
if (this.metadataFileFormat !== 'json') {
|
|
|
|
Logger.warn(`[ServerSettings] Invalid metadataFileFormat ${this.metadataFileFormat} (as of v2.4.5 only json is supported)`)
|
2023-05-16 01:23:31 +02:00
|
|
|
this.metadataFileFormat = 'json'
|
|
|
|
}
|
2022-08-14 01:18:42 +02:00
|
|
|
|
2021-10-01 01:52:32 +02:00
|
|
|
if (this.logLevel !== Logger.logLevel) {
|
|
|
|
Logger.setLogLevel(this.logLevel)
|
|
|
|
}
|
2021-09-05 02:58:39 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 00:19:16 +02:00
|
|
|
toJSON() { // Use toJSONForBrowser if sending to client
|
2021-09-05 02:58:39 +02:00
|
|
|
return {
|
|
|
|
id: this.id,
|
2022-07-19 00:19:16 +02:00
|
|
|
tokenSecret: this.tokenSecret, // Do not return to client
|
2021-10-05 05:11:42 +02:00
|
|
|
scannerFindCovers: this.scannerFindCovers,
|
2022-01-09 00:03:33 +01:00
|
|
|
scannerCoverProvider: this.scannerCoverProvider,
|
2021-09-22 03:57:33 +02:00
|
|
|
scannerParseSubtitle: this.scannerParseSubtitle,
|
2022-05-23 04:56:51 +02:00
|
|
|
scannerPreferMatchedMetadata: this.scannerPreferMatchedMetadata,
|
2022-02-24 00:52:21 +01:00
|
|
|
scannerDisableWatcher: this.scannerDisableWatcher,
|
2022-04-12 23:05:16 +02:00
|
|
|
storeCoverWithItem: this.storeCoverWithItem,
|
|
|
|
storeMetadataWithItem: this.storeMetadataWithItem,
|
2023-05-16 01:23:31 +02:00
|
|
|
metadataFileFormat: this.metadataFileFormat,
|
2021-09-29 17:16:38 +02:00
|
|
|
rateLimitLoginRequests: this.rateLimitLoginRequests,
|
2021-10-01 01:52:32 +02:00
|
|
|
rateLimitLoginWindow: this.rateLimitLoginWindow,
|
2021-10-09 00:30:20 +02:00
|
|
|
backupSchedule: this.backupSchedule,
|
|
|
|
backupsToKeep: this.backupsToKeep,
|
2022-04-23 19:19:31 +02:00
|
|
|
maxBackupSize: this.maxBackupSize,
|
2021-10-31 23:55:28 +01:00
|
|
|
loggerDailyLogsToKeep: this.loggerDailyLogsToKeep,
|
|
|
|
loggerScannerLogsToKeep: this.loggerScannerLogsToKeep,
|
2022-08-14 01:18:42 +02:00
|
|
|
homeBookshelfView: this.homeBookshelfView,
|
2021-12-28 22:50:17 +01:00
|
|
|
bookshelfView: this.bookshelfView,
|
2023-09-29 21:52:04 +02:00
|
|
|
podcastEpisodeSchedule: this.podcastEpisodeSchedule,
|
2022-02-13 22:00:59 +01:00
|
|
|
sortingIgnorePrefix: this.sortingIgnorePrefix,
|
2022-03-31 22:07:50 +02:00
|
|
|
sortingPrefixes: [...this.sortingPrefixes],
|
2022-02-23 00:33:55 +01:00
|
|
|
chromecastEnabled: this.chromecastEnabled,
|
2022-06-17 10:26:10 +02:00
|
|
|
dateFormat: this.dateFormat,
|
2023-02-27 19:04:26 +01:00
|
|
|
timeFormat: this.timeFormat,
|
2022-11-09 01:09:07 +01:00
|
|
|
language: this.language,
|
2021-11-16 03:09:42 +01:00
|
|
|
logLevel: this.logLevel,
|
2023-03-24 18:21:25 +01:00
|
|
|
version: this.version,
|
2023-11-01 14:58:48 +01:00
|
|
|
buildNumber: this.buildNumber,
|
2023-03-24 18:21:25 +01:00
|
|
|
authActiveAuthMethods: this.authActiveAuthMethods,
|
2023-04-14 20:26:29 +02:00
|
|
|
authOpenIDIssuerURL: this.authOpenIDIssuerURL,
|
|
|
|
authOpenIDAuthorizationURL: this.authOpenIDAuthorizationURL,
|
|
|
|
authOpenIDTokenURL: this.authOpenIDTokenURL,
|
|
|
|
authOpenIDUserInfoURL: this.authOpenIDUserInfoURL,
|
2023-11-04 21:36:43 +01:00
|
|
|
authOpenIDJwksURL: this.authOpenIDJwksURL,
|
|
|
|
authOpenIDLogoutURL: this.authOpenIDLogoutURL,
|
2023-04-14 20:26:29 +02:00
|
|
|
authOpenIDClientID: this.authOpenIDClientID, // Do not return to client
|
2023-11-02 19:55:01 +01:00
|
|
|
authOpenIDClientSecret: this.authOpenIDClientSecret, // Do not return to client
|
|
|
|
authOpenIDButtonText: this.authOpenIDButtonText,
|
2023-11-08 21:45:29 +01:00
|
|
|
authOpenIDAutoLaunch: this.authOpenIDAutoLaunch,
|
|
|
|
authOpenIDAutoRegister: this.authOpenIDAutoRegister,
|
|
|
|
authOpenIDMatchExistingBy: this.authOpenIDMatchExistingBy
|
2021-09-05 02:58:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 00:19:16 +02:00
|
|
|
toJSONForBrowser() {
|
|
|
|
const json = this.toJSON()
|
|
|
|
delete json.tokenSecret
|
2023-04-14 20:26:29 +02:00
|
|
|
delete json.authOpenIDClientID
|
|
|
|
delete json.authOpenIDClientSecret
|
2022-07-19 00:19:16 +02:00
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2023-11-10 23:11:51 +01:00
|
|
|
get supportedAuthMethods() {
|
|
|
|
return ['local', 'openid']
|
|
|
|
}
|
|
|
|
|
2023-09-24 22:36:35 +02:00
|
|
|
get authenticationSettings() {
|
|
|
|
return {
|
|
|
|
authActiveAuthMethods: this.authActiveAuthMethods,
|
|
|
|
authOpenIDIssuerURL: this.authOpenIDIssuerURL,
|
|
|
|
authOpenIDAuthorizationURL: this.authOpenIDAuthorizationURL,
|
|
|
|
authOpenIDTokenURL: this.authOpenIDTokenURL,
|
|
|
|
authOpenIDUserInfoURL: this.authOpenIDUserInfoURL,
|
2023-11-04 21:36:43 +01:00
|
|
|
authOpenIDJwksURL: this.authOpenIDJwksURL,
|
|
|
|
authOpenIDLogoutURL: this.authOpenIDLogoutURL,
|
2023-09-24 22:36:35 +02:00
|
|
|
authOpenIDClientID: this.authOpenIDClientID, // Do not return to client
|
2023-11-02 19:55:01 +01:00
|
|
|
authOpenIDClientSecret: this.authOpenIDClientSecret, // Do not return to client
|
|
|
|
authOpenIDButtonText: this.authOpenIDButtonText,
|
2023-11-08 21:45:29 +01:00
|
|
|
authOpenIDAutoLaunch: this.authOpenIDAutoLaunch,
|
|
|
|
authOpenIDAutoRegister: this.authOpenIDAutoRegister,
|
|
|
|
authOpenIDMatchExistingBy: this.authOpenIDMatchExistingBy
|
2023-09-24 22:36:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 19:55:01 +01:00
|
|
|
get authFormData() {
|
|
|
|
const clientFormData = {}
|
|
|
|
if (this.authActiveAuthMethods.includes('openid')) {
|
|
|
|
clientFormData.authOpenIDButtonText = this.authOpenIDButtonText
|
|
|
|
clientFormData.authOpenIDAutoLaunch = this.authOpenIDAutoLaunch
|
|
|
|
}
|
|
|
|
return clientFormData
|
|
|
|
}
|
|
|
|
|
2023-09-24 22:36:35 +02:00
|
|
|
/**
|
|
|
|
* Update server settings
|
|
|
|
*
|
|
|
|
* @param {Object} payload
|
|
|
|
* @returns {boolean} true if updates were made
|
|
|
|
*/
|
2021-09-05 02:58:39 +02:00
|
|
|
update(payload) {
|
2023-04-16 17:08:13 +02:00
|
|
|
let hasUpdates = false
|
2021-09-05 02:58:39 +02:00
|
|
|
for (const key in payload) {
|
2023-09-24 22:36:35 +02:00
|
|
|
if (key === 'sortingPrefixes') {
|
|
|
|
// Sorting prefixes are updated with the /api/sorting-prefixes endpoint
|
|
|
|
continue
|
|
|
|
} else if (key === 'authActiveAuthMethods') {
|
|
|
|
if (!payload[key]?.length) {
|
|
|
|
Logger.error(`[ServerSettings] Invalid authActiveAuthMethods`, payload[key])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
this.authActiveAuthMethods.sort()
|
|
|
|
payload[key].sort()
|
|
|
|
if (payload[key].join() !== this.authActiveAuthMethods.join()) {
|
|
|
|
this.authActiveAuthMethods = payload[key]
|
2022-03-31 22:07:50 +02:00
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
} else if (this[key] !== payload[key]) {
|
2021-10-01 01:52:32 +02:00
|
|
|
if (key === 'logLevel') {
|
|
|
|
Logger.setLogLevel(payload[key])
|
|
|
|
}
|
2021-09-05 02:58:39 +02:00
|
|
|
this[key] = payload[key]
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
}
|
2023-02-27 19:04:26 +01:00
|
|
|
module.exports = ServerSettings
|