feat: manage / create API keys (#1516)
* fix: admin api UI update * feat: admin api - create dialog UI * feat: admin api - create + list keys * feat: admin api localization (wip) * feat: admin api localization * feat: admin api - toggle state * feat: process API keys + format gql request errors to json
This commit is contained in:
@@ -13,6 +13,27 @@ module.exports = {
|
||||
async authentication () { return {} }
|
||||
},
|
||||
AuthenticationQuery: {
|
||||
/**
|
||||
* 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
|
||||
},
|
||||
/**
|
||||
* Fetch active authentication strategies
|
||||
*/
|
||||
@@ -41,6 +62,19 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
AuthenticationMutation: {
|
||||
/**
|
||||
* Create New API Key
|
||||
*/
|
||||
async createApiKey (obj, args, context) {
|
||||
try {
|
||||
return {
|
||||
key: await WIKI.models.apiKeys.createNewKey(args),
|
||||
responseResult: graphHelper.generateSuccess('API Key created successfully')
|
||||
}
|
||||
} catch (err) {
|
||||
return graphHelper.generateError(err)
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Perform Login
|
||||
*/
|
||||
@@ -101,6 +135,36 @@ module.exports = {
|
||||
return graphHelper.generateError(err)
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 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()
|
||||
return {
|
||||
responseResult: graphHelper.generateSuccess('API Key revoked successfully')
|
||||
}
|
||||
} catch (err) {
|
||||
return graphHelper.generateError(err)
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Update Authentication Strategies
|
||||
*/
|
||||
|
Reference in New Issue
Block a user