authentication.js 8.46 KB
Newer Older
1 2 3
const _ = require('lodash')
const fs = require('fs-extra')
const path = require('path')
4 5
const graphHelper = require('../../helpers/graph')

6
/* global WIKI */
7 8 9

module.exports = {
  Query: {
10
    async authentication () { return {} }
11 12
  },
  Mutation: {
13
    async authentication () { return {} }
14 15
  },
  AuthenticationQuery: {
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    /**
     * List of API Keys
     */
    async apiKeys (obj, args, context) {
      const keys = await WIKI.models.apiKeys.query().orderBy(['isRevoked', 'name'])
      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
    },
37 38 39 40 41 42 43 44 45 46 47 48
    async strategies () {
      return WIKI.data.authentication.map(stg => ({
        ...stg,
        isAvailable: stg.isAvailable === true,
        props: _.sortBy(_.transform(stg.props, (res, value, key) => {
          res.push({
            key,
            value: JSON.stringify(value)
          })
        }, []), 'key')
      }))
    },
49 50 51
    /**
     * Fetch active authentication strategies
     */
52 53
    async activeStrategies (obj, args, context, info) {
      let strategies = await WIKI.models.authentication.getStrategies()
54
      strategies = strategies.map(stg => {
55
        const strategyInfo = _.find(WIKI.data.authentication, ['key', stg.strategyKey]) || {}
56 57
        return {
          ...stg,
58
          strategy: strategyInfo,
59
          config: _.sortBy(_.transform(stg.config, (res, value, key) => {
60 61 62 63 64 65 66 67
            const configData = _.get(strategyInfo.props, key, false)
            if (configData) {
              res.push({
                key,
                value: JSON.stringify({
                  ...configData,
                  value
                })
68
              })
69
            }
70 71 72
          }, []), 'key')
        }
      })
73
      return args.enabledOnly ? _.filter(strategies, 'isEnabled') : strategies
74 75
    }
  },
76
  AuthenticationMutation: {
77 78 79 80 81
    /**
     * Create New API Key
     */
    async createApiKey (obj, args, context) {
      try {
82 83 84
        const key = await WIKI.models.apiKeys.createNewKey(args)
        await WIKI.auth.reloadApiKeys()
        WIKI.events.outbound.emit('reloadApiKeys')
85
        return {
86
          key,
87 88 89 90 91 92
          responseResult: graphHelper.generateSuccess('API Key created successfully')
        }
      } catch (err) {
        return graphHelper.generateError(err)
      }
    },
93 94 95
    /**
     * Perform Login
     */
96
    async login (obj, args, context) {
97
      try {
98
        const authResult = await WIKI.models.users.login(args, context)
99 100
        return {
          ...authResult,
101
          responseResult: graphHelper.generateSuccess('Login success')
102 103
        }
      } catch (err) {
Nick's avatar
Nick committed
104 105 106 107 108
        // LDAP Debug Flag
        if (args.strategy === 'ldap' && WIKI.config.flags.ldapdebug) {
          WIKI.logger.warn('LDAP LOGIN ERROR (c1): ', err)
        }

109 110 111
        return graphHelper.generateError(err)
      }
    },
112 113 114
    /**
     * Perform 2FA Login
     */
115
    async loginTFA (obj, args, context) {
116
      try {
117
        const authResult = await WIKI.models.users.loginTFA(args, context)
118 119
        return {
          ...authResult,
120
          responseResult: graphHelper.generateSuccess('TFA success')
121 122 123 124
        }
      } catch (err) {
        return graphHelper.generateError(err)
      }
125
    },
126 127 128 129 130 131 132 133 134 135 136 137 138 139
    /**
     * Perform Mandatory Password Change after Login
     */
    async loginChangePassword (obj, args, context) {
      try {
        const authResult = await WIKI.models.users.loginChangePassword(args, context)
        return {
          ...authResult,
          responseResult: graphHelper.generateSuccess('Password changed successfully')
        }
      } catch (err) {
        return graphHelper.generateError(err)
      }
    },
NGPixel's avatar
NGPixel committed
140 141 142 143 144 145 146 147 148 149 150 151 152
    /**
     * Perform Mandatory Password Change after Login
     */
    async forgotPassword (obj, args, context) {
      try {
        await WIKI.models.users.loginForgotPassword(args, context)
        return {
          responseResult: graphHelper.generateSuccess('Password reset request processed.')
        }
      } catch (err) {
        return graphHelper.generateError(err)
      }
    },
153 154 155
    /**
     * Register a new account
     */
156
    async register (obj, args, context) {
157
      try {
158
        await WIKI.models.users.register({ ...args, verify: true }, context)
159 160 161 162 163 164 165
        return {
          responseResult: graphHelper.generateSuccess('Registration success')
        }
      } catch (err) {
        return graphHelper.generateError(err)
      }
    },
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    /**
     * Set API state
     */
    async setApiState (obj, args, context) {
      try {
        WIKI.config.api.isEnabled = args.enabled
        await WIKI.configSvc.saveToDb(['api'])
        return {
          responseResult: graphHelper.generateSuccess('API State changed successfully')
        }
      } catch (err) {
        return graphHelper.generateError(err)
      }
    },
    /**
     * Revoke an API key
     */
    async revokeApiKey (obj, args, context) {
      try {
        await WIKI.models.apiKeys.query().findById(args.id).patch({
          isRevoked: true
        })
        await WIKI.auth.reloadApiKeys()
189
        WIKI.events.outbound.emit('reloadApiKeys')
190 191 192 193 194 195 196
        return {
          responseResult: graphHelper.generateSuccess('API Key revoked successfully')
        }
      } catch (err) {
        return graphHelper.generateError(err)
      }
    },
197 198 199
    /**
     * Update Authentication Strategies
     */
200
    async updateStrategies (obj, args, context) {
201
      try {
202 203 204 205 206
        const previousStrategies = await WIKI.models.authentication.getStrategies()
        for (const str of args.strategies) {
          const newStr = {
            displayName: str.displayName,
            order: str.order,
207
            isEnabled: str.isEnabled,
208
            config: _.reduce(str.config, (result, value, key) => {
209
              _.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
210 211 212 213 214
              return result
            }, {}),
            selfRegistration: str.selfRegistration,
            domainWhitelist: { v: str.domainWhitelist },
            autoEnrollGroups: { v: str.autoEnrollGroups }
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
          }

          if (_.some(previousStrategies, ['key', str.key])) {
            await WIKI.models.authentication.query().patch({
              key: str.key,
              strategyKey: str.strategyKey,
              ...newStr
            }).where('key', str.key)
          } else {
            await WIKI.models.authentication.query().insert({
              key: str.key,
              strategyKey: str.strategyKey,
              ...newStr
            })
          }
230
        }
231 232 233 234 235 236 237 238 239 240

        for (const str of _.differenceBy(previousStrategies, args.strategies, 'key')) {
          const hasUsers = await WIKI.models.users.query().count('* as total').where({ providerKey: str.key }).first()
          if (_.toSafeInteger(hasUsers.total) > 0) {
            throw new Error(`Cannot delete ${str.displayName} as 1 or more users are still using it.`)
          } else {
            await WIKI.models.authentication.query().delete().where('key', str.key)
          }
        }

241
        await WIKI.auth.activateStrategies()
242
        WIKI.events.outbound.emit('reloadAuthStrategies')
243 244 245 246 247 248
        return {
          responseResult: graphHelper.generateSuccess('Strategies updated successfully')
        }
      } catch (err) {
        return graphHelper.generateError(err)
      }
249 250 251 252
    },
    /**
     * Generate New Authentication Public / Private Key Certificates
     */
253
    async regenerateCertificates (obj, args, context) {
254 255 256 257 258 259 260 261 262 263 264 265
      try {
        await WIKI.auth.regenerateCertificates()
        return {
          responseResult: graphHelper.generateSuccess('Certificates have been regenerated successfully.')
        }
      } catch (err) {
        return graphHelper.generateError(err)
      }
    },
    /**
     * Reset Guest User
     */
266
    async resetGuestUser (obj, args, context) {
267 268 269 270 271 272 273 274
      try {
        await WIKI.auth.resetGuestUser()
        return {
          responseResult: graphHelper.generateSuccess('Guest user has been reset successfully.')
        }
      } catch (err) {
        return graphHelper.generateError(err)
      }
275 276
    }
  },
NGPixel's avatar
NGPixel committed
277
  AuthenticationStrategy: {
278
    icon (ap, args) {
279
      return fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${ap.key}.svg`), 'utf8').catch(err => {
280 281 282 283 284 285 286 287
        if (err.code === 'ENOENT') {
          return null
        }
        throw err
      })
    }
  }
}