Begin api-key functionality on backend

This commit is contained in:
Tuan Dang
2022-12-25 12:45:43 -05:00
parent 2513250993
commit d869968f88
9 changed files with 658 additions and 35 deletions
+80
View File
@@ -0,0 +1,80 @@
import express from 'express';
const router = express.Router();
import {
requireAuth
} from '../middleware';
import {
APIKey
} from '../models';
import { body } from 'express-validator';
import crypto from 'crypto';
import bcrypt from 'bcrypt';
// import * as bcrypt from 'bcrypt';
// const bcrypt = require('bcrypt');
import * as Sentry from '@sentry/node';
// POST /api/v1/api-key
router.post(
'/',
requireAuth,
body('name').exists().trim(),
body('workspace'),
body('environment'),
body('encryptedKey'),
body('iv'),
body('tag'),
body('expiresAt'),
async (req, res) => {
let savedAPIKey;
try {
const {
name,
workspace,
environment,
encryptedKey,
iv,
tag,
expiresAt
} = req.body;
// api-key: 38 characters
// 6-char: prefix
// 32-char: remaining
const apiKey = crypto.randomBytes(19).toString('hex');
const saltRounds = 10; // config?
const apiKeyHash = await bcrypt.hash(apiKey, saltRounds);
savedAPIKey = await new APIKey({
name,
workspace,
environment,
expiresAt,
prefix: apiKey.substring(0, 6),
apiKeyHash,
encryptedKey,
iv,
tag
}).save();
// 1. generate api key
// 2. hash api key with bcrypt
// 3. store hash and api key info in db
// 4. return api key
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: 'xxx'
});
}
return res.status(200).send({
apiKey: savedAPIKey
});
}
);
// INFISICAL TOKEN = <API_KEY>.<KEY>
export default router;
+3 -1
View File
@@ -15,6 +15,7 @@ import password from './password';
import stripe from './stripe';
import integration from './integration';
import integrationAuth from './integrationAuth';
import apiKey from './apiKey';
export {
signup,
@@ -33,5 +34,6 @@ export {
password,
stripe,
integration,
integrationAuth
integrationAuth,
apiKey
};
+1 -1
View File
@@ -10,7 +10,7 @@ import { body } from 'express-validator';
import { ADMIN, MEMBER, GRANTED } from '../variables';
import { serviceTokenController } from '../controllers';
// TODO: revoke service token
// Note to devs: service-token to be deprecated in favor of api-key
router.get(
'/',