1
0
Fork 0
freesewing/sites/backend/openapi/apikeys.mjs

194 lines
5.1 KiB
JavaScript
Raw Normal View History

2022-12-22 18:41:19 +01:00
import { jwt, key, fields, parameters, response, errorExamples, jsonResponse } from './lib.mjs'
const common = {
tags: ['API keys'],
security: [jwt, key],
}
2022-12-19 12:27:08 +01:00
const local = {
params: {
key: {
in: 'path',
name: 'key',
required: true,
description: "The API key's UUID (the part you use as username)",
schema: {
example: 'c00475bd-3002-4baa-80ad-0145cd6a646c',
type: 'string',
},
},
},
2022-12-19 12:27:08 +01:00
response: {
body: {
2022-12-22 18:41:19 +01:00
regular: {
...response.body.apikey,
properties: { ...response.body.apikey.properties },
2022-12-19 12:27:08 +01:00
},
},
},
}
2022-12-19 12:27:08 +01:00
delete local.response.body.regular.properties.secret
// Paths
export const paths = {}
2024-08-30 14:35:08 -07:00
// List API keys or Create an API key
2022-12-19 12:27:08 +01:00
paths['/apikeys/{auth}'] = {
get: {
...common,
summary: 'Retrieves list of API keys',
description: 'Returns a list of API keys for the user making the API request',
parameters: [parameters.auth],
responses: {
200: {
description:
'**Success - List of API keys returned**\n\n' +
'Status code `200` indicates that the resource was returned successfully.',
...jsonResponse({
result: {
...fields.result,
example: 'success',
},
apikeys: {
type: 'array',
items: local.response.body.regular,
},
}),
},
2024-08-30 14:35:08 -07:00
404: response.status['404'],
},
},
post: {
...common,
summary: 'Create a new API key',
description:
'Creates a new API key and returns it. ' +
'requires a `name`, `level`, and `expiresIn` field in the POST body.',
parameters: [parameters.auth],
requestBody: {
required: true,
content: {
'application/json': {
schema: {
type: 'object',
properties: {
expiresIn: {
description: `
mber of seconds the API key will remain valid before expiring.
n never be higher than the \`apikeys.maxExpirySeconds\` configuration setting.`,
type: 'number',
example: 3600,
},
level: fields.level,
2022-12-22 18:41:19 +01:00
name: {
description: `
Th e name of the API key exists solely to help you differentiate between your API keys.`,
type: 'string',
example: 'My first API key',
},
},
},
},
},
},
responses: {
201: {
2022-12-22 18:41:19 +01:00
...response.status['201'],
...jsonResponse({
result: fields.result,
apikey: response.body.apikey,
}),
},
400: {
2022-12-22 18:41:19 +01:00
...response.status['400'],
description:
2022-12-22 18:41:19 +01:00
response.status['400'].description +
errorExamples([
'postBodyMissing',
'nameMissing',
'levelMissing',
'expiresInMissing',
'levelNotNumeric',
'invalidLevel',
'expiresInNotNumeric',
'expiresInHigherThanMaximum',
'keyLevelExceedsRoleLevel',
]),
},
500: response.status['500'],
},
},
}
// Get/Remove API key
2022-12-19 12:27:08 +01:00
paths['/apikeys/{key}/{auth}'] = {
// Get API key
get: {
...common,
summary: 'Retrieve an API key',
description: 'Retrieves information about the API key `key`.',
2022-12-19 12:27:08 +01:00
parameters: [parameters.auth, local.params.key],
responses: {
200: {
description:
'**Success - API key returned**\n\n' +
'Status code `200` indicates that the resource was returned successfully.',
2022-12-22 18:41:19 +01:00
...jsonResponse({
result: {
...fields.result,
example: 'success',
},
2022-12-22 18:41:19 +01:00
apikey: local.response.body.regular,
}),
},
2022-12-22 18:41:19 +01:00
401: response.status['401'],
403: {
2022-12-22 18:41:19 +01:00
...response.status['403'],
description:
2022-12-22 18:41:19 +01:00
response.status['403'].description +
errorExamples(['accountStatusLacking', 'insufficientAccessLevel']),
},
2022-12-22 18:41:19 +01:00
404: response.status['404'],
500: response.status['500'],
},
},
// Remove API key
delete: {
...common,
summary: 'Remove an API key',
description: 'Removes the API key `key`.',
2022-12-19 12:27:08 +01:00
parameters: [parameters.auth, local.params.key],
responses: {
2022-12-22 18:41:19 +01:00
204: response.status['204'],
401: response.status['401'],
403: {
2022-12-22 18:41:19 +01:00
...response.status['403'],
description:
2022-12-22 18:41:19 +01:00
response.status['403'].description +
errorExamples(['accountStatusLacking', 'insufficientAccessLevel']),
},
2022-12-22 18:41:19 +01:00
404: response.status['404'],
500: response.status['500'],
},
},
}
// Get current API key
2022-12-19 12:27:08 +01:00
paths['/whoami/key'] = {
get: {
tags: ['Whoami', ...common.tags],
security: [key],
summary: 'Retrieve the API key used in the request',
description:
'Retrieves information about the API key that ' +
'was used to authenticate the request.\n\n' +
'**Note:** _See `GET /whoami/jwt` for the JWT equivalent._',
responses: {
2022-12-22 18:41:19 +01:00
200: paths['/apikeys/{key}/{auth}'].get.responses['200'],
401: response.status['401'],
2022-12-24 14:47:43 +01:00
403: paths['/apikeys/{key}/{auth}'].get.responses['403'],
2022-12-22 18:41:19 +01:00
500: response.status['500'],
},
},
}