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')
|
|
|
|
|
|
|
|
// const getFieldNames = require('graphql-list-fields')
|
2017-10-01 03:47:14 +00:00
|
|
|
|
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-06-17 15:12:11 +00:00
|
|
|
let strategies = await WIKI.db.authentication.getEnabledStrategies()
|
2018-05-28 23:36:35 +00:00
|
|
|
strategies = strategies.map(stg => ({
|
|
|
|
...stg,
|
|
|
|
config: _.transform(stg.config, (res, value, key) => {
|
|
|
|
res.push({ key, value })
|
|
|
|
}, [])
|
2018-03-09 05:33:43 +00:00
|
|
|
}))
|
2018-05-28 23:36:35 +00:00
|
|
|
if (args.filter) { strategies = graphHelper.filter(strategies, args.filter) }
|
|
|
|
if (args.orderBy) { strategies = graphHelper.orderBy(strategies, args.orderBy) }
|
|
|
|
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-05-19 20:40:07 +00:00
|
|
|
let authResult = await WIKI.db.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-05-19 20:40:07 +00:00
|
|
|
let authResult = await WIKI.db.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-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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|