2022-11-05 18:55:59 +01:00
|
|
|
import { hash, hashPassword, randomString, verifyPassword } from '../utils/crypto.mjs'
|
|
|
|
import { clean, asJson } from '../utils/index.mjs'
|
|
|
|
import { log } from '../utils/log.mjs'
|
|
|
|
import { ApikeyModel } from '../models/apikey.mjs'
|
|
|
|
import { UserModel } from '../models/user.mjs'
|
|
|
|
|
|
|
|
export function ApikeyController() {}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create API key
|
|
|
|
*
|
|
|
|
* This is the endpoint that handles creation of API keys/tokens
|
|
|
|
* See: https://freesewing.dev/reference/backend/api
|
|
|
|
*/
|
|
|
|
ApikeyController.prototype.create = async (req, res, tools) => {
|
|
|
|
const Apikey = new ApikeyModel(tools)
|
|
|
|
await Apikey.create(req)
|
|
|
|
|
|
|
|
return Apikey.sendResponse(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read API key
|
|
|
|
*
|
|
|
|
* This is the endpoint that handles reading of API keys/tokens
|
|
|
|
* See: https://freesewing.dev/reference/backend/api
|
|
|
|
*/
|
|
|
|
ApikeyController.prototype.whoami = async (req, res, tools) => {
|
|
|
|
const User = new UserModel(tools)
|
|
|
|
const Apikey = new ApikeyModel(tools)
|
|
|
|
|
|
|
|
// Load user making the call
|
|
|
|
await User.loadAuthenticatedUser(req.user)
|
|
|
|
|
2022-11-05 22:02:51 +01:00
|
|
|
const key = User.authenticatedUser.apikeys.filter((key) => key.id === req.user.id)
|
2022-11-05 18:55:59 +01:00
|
|
|
|
|
|
|
if (key.length === 1)
|
|
|
|
Apikey.setResponse(200, 'success', {
|
|
|
|
apikey: {
|
|
|
|
key: key[0].id,
|
|
|
|
level: key[0].level,
|
|
|
|
expiresAt: key[0].expiresAt,
|
|
|
|
name: key[0].name,
|
|
|
|
userId: key[0].userId,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
else Apikey.setResponse(404, 'notFound')
|
|
|
|
|
|
|
|
return Apikey.sendResponse(res)
|
|
|
|
}
|