mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
268 lines
7.6 KiB
268 lines
7.6 KiB
2 years ago
|
import _ from 'lodash-es'
|
||
|
import { generateError, generateSuccess } from '../../helpers/graph.mjs'
|
||
7 years ago
|
|
||
2 years ago
|
export default {
|
||
7 years ago
|
Query: {
|
||
5 years ago
|
/**
|
||
|
* List of API Keys
|
||
|
*/
|
||
|
async apiKeys (obj, args, context) {
|
||
2 years ago
|
const keys = await WIKI.db.apiKeys.query().orderBy(['isRevoked', 'name'])
|
||
5 years ago
|
return keys.map(k => ({
|
||
|
id: k.id,
|
||
|
name: k.name,
|
||
|
keyShort: '...' + k.key.substring(k.key.length - 20),
|
||
|
isRevoked: k.isRevoked,
|
||
|
expiration: k.expiration,
|
||
|
createdAt: k.createdAt,
|
||
|
updatedAt: k.updatedAt
|
||
|
}))
|
||
|
},
|
||
|
/**
|
||
|
* Current API State
|
||
|
*/
|
||
|
apiState () {
|
||
|
return WIKI.config.api.isEnabled
|
||
|
},
|
||
2 years ago
|
/**
|
||
|
* Fetch authentication strategies
|
||
|
*/
|
||
3 years ago
|
async authStrategies () {
|
||
4 years ago
|
return WIKI.data.authentication.map(stg => ({
|
||
|
...stg,
|
||
2 years ago
|
isAvailable: stg.isAvailable === true
|
||
4 years ago
|
}))
|
||
2 years ago
|
},
|
||
|
/**
|
||
|
* Fetch active authentication strategies
|
||
|
*/
|
||
2 years ago
|
async authActiveStrategies (obj, args, context) {
|
||
2 years ago
|
return WIKI.db.authentication.getStrategies({ enabledOnly: args.enabledOnly })
|
||
2 years ago
|
},
|
||
|
/**
|
||
|
* Fetch site authentication strategies
|
||
|
*/
|
||
2 years ago
|
async authSiteStrategies (obj, args, context, info) {
|
||
2 years ago
|
const site = await WIKI.db.sites.query().findById(args.siteId)
|
||
|
const activeStrategies = await WIKI.db.authentication.getStrategies({ enabledOnly: true })
|
||
2 years ago
|
return activeStrategies.map(str => {
|
||
|
const siteAuth = _.find(site.config.authStrategies, ['id', str.id]) || {}
|
||
2 years ago
|
return {
|
||
2 years ago
|
id: str.id,
|
||
|
activeStrategy: str,
|
||
|
order: siteAuth.order ?? 0,
|
||
|
isVisible: siteAuth.isVisible ?? false
|
||
2 years ago
|
}
|
||
|
})
|
||
7 years ago
|
}
|
||
|
},
|
||
3 years ago
|
Mutation: {
|
||
5 years ago
|
/**
|
||
|
* Create New API Key
|
||
|
*/
|
||
|
async createApiKey (obj, args, context) {
|
||
|
try {
|
||
2 years ago
|
const key = await WIKI.db.apiKeys.createNewKey(args)
|
||
5 years ago
|
await WIKI.auth.reloadApiKeys()
|
||
|
WIKI.events.outbound.emit('reloadApiKeys')
|
||
5 years ago
|
return {
|
||
5 years ago
|
key,
|
||
2 years ago
|
operation: generateSuccess('API Key created successfully')
|
||
5 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
WIKI.logger.warn(err)
|
||
2 years ago
|
return generateError(err)
|
||
5 years ago
|
}
|
||
|
},
|
||
5 years ago
|
/**
|
||
|
* Perform Login
|
||
|
*/
|
||
5 years ago
|
async login (obj, args, context) {
|
||
7 years ago
|
try {
|
||
2 years ago
|
const authResult = await WIKI.db.users.login(args, context)
|
||
7 years ago
|
return {
|
||
|
...authResult,
|
||
2 years ago
|
operation: generateSuccess('Login success')
|
||
7 years ago
|
}
|
||
|
} catch (err) {
|
||
6 years ago
|
// LDAP Debug Flag
|
||
|
if (args.strategy === 'ldap' && WIKI.config.flags.ldapdebug) {
|
||
|
WIKI.logger.warn('LDAP LOGIN ERROR (c1): ', err)
|
||
|
}
|
||
2 years ago
|
console.error(err)
|
||
6 years ago
|
|
||
2 years ago
|
return generateError(err)
|
||
7 years ago
|
}
|
||
|
},
|
||
5 years ago
|
/**
|
||
|
* Perform 2FA Login
|
||
|
*/
|
||
5 years ago
|
async loginTFA (obj, args, context) {
|
||
7 years ago
|
try {
|
||
2 years ago
|
const authResult = await WIKI.db.users.loginTFA(args, context)
|
||
7 years ago
|
return {
|
||
|
...authResult,
|
||
2 years ago
|
responseResult: generateSuccess('TFA success')
|
||
7 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
7 years ago
|
}
|
||
7 years ago
|
},
|
||
5 years ago
|
/**
|
||
2 years ago
|
* Perform Password Change
|
||
5 years ago
|
*/
|
||
2 years ago
|
async changePassword (obj, args, context) {
|
||
5 years ago
|
try {
|
||
2 years ago
|
const authResult = await WIKI.db.users.loginChangePassword(args, context)
|
||
5 years ago
|
return {
|
||
|
...authResult,
|
||
2 years ago
|
responseResult: generateSuccess('Password changed successfully')
|
||
5 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
5 years ago
|
}
|
||
|
},
|
||
4 years ago
|
/**
|
||
2 years ago
|
* Perform Forget Password
|
||
4 years ago
|
*/
|
||
|
async forgotPassword (obj, args, context) {
|
||
|
try {
|
||
2 years ago
|
await WIKI.db.users.loginForgotPassword(args, context)
|
||
4 years ago
|
return {
|
||
2 years ago
|
responseResult: generateSuccess('Password reset request processed.')
|
||
4 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
4 years ago
|
}
|
||
|
},
|
||
5 years ago
|
/**
|
||
|
* Register a new account
|
||
|
*/
|
||
5 years ago
|
async register (obj, args, context) {
|
||
6 years ago
|
try {
|
||
2 years ago
|
await WIKI.db.users.register({ ...args, verify: true }, context)
|
||
6 years ago
|
return {
|
||
2 years ago
|
responseResult: generateSuccess('Registration success')
|
||
6 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
6 years ago
|
}
|
||
|
},
|
||
5 years ago
|
/**
|
||
|
* Set API state
|
||
|
*/
|
||
|
async setApiState (obj, args, context) {
|
||
|
try {
|
||
|
WIKI.config.api.isEnabled = args.enabled
|
||
|
await WIKI.configSvc.saveToDb(['api'])
|
||
|
return {
|
||
2 years ago
|
operation: generateSuccess('API State changed successfully')
|
||
5 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
5 years ago
|
}
|
||
|
},
|
||
|
/**
|
||
|
* Revoke an API key
|
||
|
*/
|
||
|
async revokeApiKey (obj, args, context) {
|
||
|
try {
|
||
2 years ago
|
await WIKI.db.apiKeys.query().findById(args.id).patch({
|
||
5 years ago
|
isRevoked: true
|
||
|
})
|
||
|
await WIKI.auth.reloadApiKeys()
|
||
5 years ago
|
WIKI.events.outbound.emit('reloadApiKeys')
|
||
5 years ago
|
return {
|
||
2 years ago
|
operation: generateSuccess('API Key revoked successfully')
|
||
5 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
5 years ago
|
}
|
||
|
},
|
||
5 years ago
|
/**
|
||
|
* Update Authentication Strategies
|
||
|
*/
|
||
3 years ago
|
async updateAuthStrategies (obj, args, context) {
|
||
7 years ago
|
try {
|
||
2 years ago
|
const previousStrategies = await WIKI.db.authentication.getStrategies()
|
||
4 years ago
|
for (const str of args.strategies) {
|
||
|
const newStr = {
|
||
|
displayName: str.displayName,
|
||
|
order: str.order,
|
||
4 years ago
|
isEnabled: str.isEnabled,
|
||
7 years ago
|
config: _.reduce(str.config, (result, value, key) => {
|
||
6 years ago
|
_.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
|
||
7 years ago
|
return result
|
||
|
}, {}),
|
||
|
selfRegistration: str.selfRegistration,
|
||
|
domainWhitelist: { v: str.domainWhitelist },
|
||
|
autoEnrollGroups: { v: str.autoEnrollGroups }
|
||
4 years ago
|
}
|
||
|
|
||
|
if (_.some(previousStrategies, ['key', str.key])) {
|
||
2 years ago
|
await WIKI.db.authentication.query().patch({
|
||
4 years ago
|
key: str.key,
|
||
|
strategyKey: str.strategyKey,
|
||
|
...newStr
|
||
|
}).where('key', str.key)
|
||
|
} else {
|
||
2 years ago
|
await WIKI.db.authentication.query().insert({
|
||
4 years ago
|
key: str.key,
|
||
|
strategyKey: str.strategyKey,
|
||
|
...newStr
|
||
|
})
|
||
|
}
|
||
7 years ago
|
}
|
||
4 years ago
|
|
||
|
for (const str of _.differenceBy(previousStrategies, args.strategies, 'key')) {
|
||
2 years ago
|
const hasUsers = await WIKI.db.users.query().count('* as total').where({ providerKey: str.key }).first()
|
||
4 years ago
|
if (_.toSafeInteger(hasUsers.total) > 0) {
|
||
|
throw new Error(`Cannot delete ${str.displayName} as 1 or more users are still using it.`)
|
||
|
} else {
|
||
2 years ago
|
await WIKI.db.authentication.query().delete().where('key', str.key)
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
await WIKI.auth.activateStrategies()
|
||
5 years ago
|
WIKI.events.outbound.emit('reloadAuthStrategies')
|
||
7 years ago
|
return {
|
||
2 years ago
|
responseResult: generateSuccess('Strategies updated successfully')
|
||
7 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
7 years ago
|
}
|
||
5 years ago
|
},
|
||
|
/**
|
||
|
* Generate New Authentication Public / Private Key Certificates
|
||
|
*/
|
||
5 years ago
|
async regenerateCertificates (obj, args, context) {
|
||
5 years ago
|
try {
|
||
|
await WIKI.auth.regenerateCertificates()
|
||
|
return {
|
||
2 years ago
|
responseResult: generateSuccess('Certificates have been regenerated successfully.')
|
||
5 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
5 years ago
|
}
|
||
|
},
|
||
|
/**
|
||
|
* Reset Guest User
|
||
|
*/
|
||
5 years ago
|
async resetGuestUser (obj, args, context) {
|
||
5 years ago
|
try {
|
||
|
await WIKI.auth.resetGuestUser()
|
||
|
return {
|
||
2 years ago
|
responseResult: generateSuccess('Guest user has been reset successfully.')
|
||
5 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
5 years ago
|
}
|
||
7 years ago
|
}
|
||
|
},
|
||
2 years ago
|
AuthenticationActiveStrategy: {
|
||
|
strategy (obj, args, context) {
|
||
|
return _.find(WIKI.data.authentication, ['key', obj.module])
|
||
7 years ago
|
}
|
||
|
}
|
||
|
}
|