feat: new login experience (#2139)
* feat: multiple auth instances * fix: auth setup + strategy initialization * feat: admin auth - add strategy * feat: redirect on login - group setting * feat: oauth2 generic - props definitions * feat: new login UI (wip) * feat: new login UI (wip) * feat: admin security login settings * feat: tabset editor indicators + print view improvements * fix: code styling
This commit is contained in:
@@ -17,11 +17,10 @@ module.exports = class Authentication extends Model {
|
||||
static get jsonSchema () {
|
||||
return {
|
||||
type: 'object',
|
||||
required: ['key', 'isEnabled'],
|
||||
required: ['key'],
|
||||
|
||||
properties: {
|
||||
key: {type: 'string'},
|
||||
isEnabled: {type: 'boolean'},
|
||||
selfRegistration: {type: 'boolean'}
|
||||
}
|
||||
}
|
||||
@@ -35,8 +34,8 @@ module.exports = class Authentication extends Model {
|
||||
return WIKI.models.authentication.query().findOne({ key })
|
||||
}
|
||||
|
||||
static async getStrategies(isEnabled) {
|
||||
const strategies = await WIKI.models.authentication.query().where(_.isBoolean(isEnabled) ? { isEnabled } : {})
|
||||
static async getStrategies() {
|
||||
const strategies = await WIKI.models.authentication.query().orderBy('order')
|
||||
return _.sortBy(strategies.map(str => ({
|
||||
...str,
|
||||
domainWhitelist: _.get(str.domainWhitelist, 'v', []),
|
||||
@@ -45,7 +44,7 @@ module.exports = class Authentication extends Model {
|
||||
}
|
||||
|
||||
static async getStrategiesForLegacyClient() {
|
||||
const strategies = await WIKI.models.authentication.query().select('key', 'selfRegistration').where({ isEnabled: true })
|
||||
const strategies = await WIKI.models.authentication.query().select('key', 'selfRegistration')
|
||||
let formStrategies = []
|
||||
let socialStrategies = []
|
||||
|
||||
@@ -77,64 +76,42 @@ module.exports = class Authentication extends Model {
|
||||
}
|
||||
|
||||
static async refreshStrategiesFromDisk() {
|
||||
let trx
|
||||
try {
|
||||
const dbStrategies = await WIKI.models.authentication.query()
|
||||
|
||||
// -> Fetch definitions from disk
|
||||
const authDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/authentication'))
|
||||
let diskStrategies = []
|
||||
WIKI.data.authentication = []
|
||||
for (let dir of authDirs) {
|
||||
const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/authentication', dir, 'definition.yml'), 'utf8')
|
||||
diskStrategies.push(yaml.safeLoad(def))
|
||||
const defRaw = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/authentication', dir, 'definition.yml'), 'utf8')
|
||||
const def = yaml.safeLoad(defRaw)
|
||||
WIKI.data.authentication.push({
|
||||
...def,
|
||||
props: commonHelper.parseModuleProps(def.props)
|
||||
})
|
||||
}
|
||||
WIKI.data.authentication = diskStrategies.map(strategy => ({
|
||||
...strategy,
|
||||
props: commonHelper.parseModuleProps(strategy.props)
|
||||
}))
|
||||
|
||||
let newStrategies = []
|
||||
for (let strategy of WIKI.data.authentication) {
|
||||
if (!_.some(dbStrategies, ['key', strategy.key])) {
|
||||
newStrategies.push({
|
||||
key: strategy.key,
|
||||
isEnabled: false,
|
||||
config: _.transform(strategy.props, (result, value, key) => {
|
||||
_.set(result, key, value.default)
|
||||
return result
|
||||
}, {}),
|
||||
selfRegistration: false,
|
||||
domainWhitelist: { v: [] },
|
||||
autoEnrollGroups: { v: [] }
|
||||
})
|
||||
} else {
|
||||
const strategyConfig = _.get(_.find(dbStrategies, ['key', strategy.key]), 'config', {})
|
||||
for (const strategy of dbStrategies) {
|
||||
const strategyDef = _.find(WIKI.data.authentication, ['key', strategy.strategyKey])
|
||||
strategy.config = _.transform(strategyDef.props, (result, value, key) => {
|
||||
if (!_.has(result, key)) {
|
||||
_.set(result, key, value.default)
|
||||
}
|
||||
return result
|
||||
}, strategy.config)
|
||||
|
||||
// Fix pre-2.5 strategies displayName
|
||||
if (!strategy.displayName) {
|
||||
await WIKI.models.authentication.query().patch({
|
||||
config: _.transform(strategy.props, (result, value, key) => {
|
||||
if (!_.has(result, key)) {
|
||||
_.set(result, key, value.default)
|
||||
}
|
||||
return result
|
||||
}, strategyConfig)
|
||||
displayName: strategyDef.title
|
||||
}).where('key', strategy.key)
|
||||
}
|
||||
}
|
||||
if (newStrategies.length > 0) {
|
||||
trx = await WIKI.models.Objection.transaction.start(WIKI.models.knex)
|
||||
for (let strategy of newStrategies) {
|
||||
await WIKI.models.authentication.query(trx).insert(strategy)
|
||||
}
|
||||
await trx.commit()
|
||||
WIKI.logger.info(`Loaded ${newStrategies.length} new authentication strategies: [ OK ]`)
|
||||
} else {
|
||||
WIKI.logger.info(`No new authentication strategies found: [ SKIPPED ]`)
|
||||
}
|
||||
|
||||
WIKI.logger.info(`Loaded ${WIKI.data.authentication.length} authentication strategies: [ OK ]`)
|
||||
} catch (err) {
|
||||
WIKI.logger.error(`Failed to scan or load new authentication providers: [ FAILED ]`)
|
||||
WIKI.logger.error(err)
|
||||
if (trx) {
|
||||
trx.rollback()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -14,6 +14,8 @@ module.exports = class Group extends Model {
|
||||
properties: {
|
||||
id: {type: 'integer'},
|
||||
name: {type: 'string'},
|
||||
isSystem: {type: 'boolean'},
|
||||
redirectOnLogin: {type: 'string'},
|
||||
createdAt: {type: 'string'},
|
||||
updatedAt: {type: 'string'}
|
||||
}
|
||||
|
Reference in New Issue
Block a user