2017-10-01 03:47:14 +00:00
|
|
|
const _ = require('lodash')
|
|
|
|
const fs = require('fs-extra')
|
|
|
|
const path = require('path')
|
2018-03-09 05:33:43 +00:00
|
|
|
const graphHelper = require('../../helpers/graph')
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-10-01 03:47:14 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Query: {
|
2018-03-05 20:49:36 +00:00
|
|
|
async authentication() { return {} }
|
|
|
|
},
|
|
|
|
Mutation: {
|
|
|
|
async authentication() { return {} }
|
|
|
|
},
|
|
|
|
AuthenticationQuery: {
|
2018-06-04 04:41:29 +00:00
|
|
|
async strategies(obj, args, context, info) {
|
2018-10-08 04:17:31 +00:00
|
|
|
let strategies = await WIKI.models.authentication.getStrategies(args.isEnabled)
|
2018-08-04 21:27:55 +00:00
|
|
|
strategies = strategies.map(stg => {
|
|
|
|
const strategyInfo = _.find(WIKI.data.authentication, ['key', stg.key]) || {}
|
|
|
|
return {
|
|
|
|
...strategyInfo,
|
|
|
|
...stg,
|
|
|
|
config: _.sortBy(_.transform(stg.config, (res, value, key) => {
|
|
|
|
const configData = _.get(strategyInfo.props, key, {})
|
|
|
|
res.push({
|
|
|
|
key,
|
|
|
|
value: JSON.stringify({
|
|
|
|
...configData,
|
|
|
|
value
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}, []), 'key')
|
|
|
|
}
|
|
|
|
})
|
2018-05-28 23:36:35 +00:00
|
|
|
return strategies
|
2017-10-01 03:47:14 +00:00
|
|
|
}
|
|
|
|
},
|
2018-03-10 05:58:04 +00:00
|
|
|
AuthenticationMutation: {
|
|
|
|
async login(obj, args, context) {
|
|
|
|
try {
|
2018-12-17 05:51:52 +00:00
|
|
|
const authResult = await WIKI.models.users.login(args, context)
|
2018-03-10 05:58:04 +00:00
|
|
|
return {
|
|
|
|
...authResult,
|
2018-03-25 02:35:47 +00:00
|
|
|
responseResult: graphHelper.generateSuccess('Login success')
|
2018-03-10 05:58:04 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async loginTFA(obj, args, context) {
|
|
|
|
try {
|
2018-12-17 05:51:52 +00:00
|
|
|
const authResult = await WIKI.models.users.loginTFA(args, context)
|
2018-03-10 05:58:04 +00:00
|
|
|
return {
|
|
|
|
...authResult,
|
2018-03-25 02:35:47 +00:00
|
|
|
responseResult: graphHelper.generateSuccess('TFA success')
|
2018-03-10 05:58:04 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2018-06-26 00:55:00 +00:00
|
|
|
},
|
2018-12-17 05:51:52 +00:00
|
|
|
async register(obj, args, context) {
|
|
|
|
try {
|
2019-01-01 06:40:31 +00:00
|
|
|
await WIKI.models.users.register({...args, verify: true }, context)
|
2018-12-17 05:51:52 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Registration success')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
|
|
|
},
|
2018-06-26 00:55:00 +00:00
|
|
|
async updateStrategies(obj, args, context) {
|
|
|
|
try {
|
2019-01-07 03:03:34 +00:00
|
|
|
WIKI.config.auth = {
|
|
|
|
audience: _.get(args, 'config.audience', WIKI.config.auth.audience),
|
|
|
|
tokenExpiration: _.get(args, 'config.tokenExpiration', WIKI.config.auth.tokenExpiration),
|
|
|
|
tokenRenewal: _.get(args, 'config.tokenRenewal', WIKI.config.auth.tokenRenewal)
|
|
|
|
}
|
|
|
|
await WIKI.configSvc.saveToDb(['auth'])
|
|
|
|
|
2018-06-26 00:55:00 +00:00
|
|
|
for (let str of args.strategies) {
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.authentication.query().patch({
|
2018-06-26 00:55:00 +00:00
|
|
|
isEnabled: str.isEnabled,
|
|
|
|
config: _.reduce(str.config, (result, value, key) => {
|
2018-09-30 18:20:26 +00:00
|
|
|
_.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
|
2018-06-26 00:55:00 +00:00
|
|
|
return result
|
|
|
|
}, {}),
|
|
|
|
selfRegistration: str.selfRegistration,
|
|
|
|
domainWhitelist: { v: str.domainWhitelist },
|
|
|
|
autoEnrollGroups: { v: str.autoEnrollGroups }
|
|
|
|
}).where('key', str.key)
|
|
|
|
}
|
2018-10-13 03:14:11 +00:00
|
|
|
await WIKI.auth.activateStrategies()
|
2018-06-26 00:55:00 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Strategies updated successfully')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2018-03-10 05:58:04 +00:00
|
|
|
}
|
|
|
|
},
|
2018-06-04 04:41:29 +00:00
|
|
|
AuthenticationStrategy: {
|
2017-10-01 03:47:14 +00:00
|
|
|
icon (ap, args) {
|
2018-03-06 01:53:24 +00:00
|
|
|
return fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${ap.key}.svg`), 'utf8').catch(err => {
|
2017-10-01 03:47:14 +00:00
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|