-
Notifications
You must be signed in to change notification settings - Fork 988
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin api-key functionality on backend
- Loading branch information
1 parent
2513250
commit d869968
Showing
9 changed files
with
658 additions
and
35 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Schema, model, Types } from 'mongoose'; | ||
import { ENV_DEV, ENV_TESTING, ENV_STAGING, ENV_PROD } from '../variables'; | ||
|
||
// TODO: add scopes | ||
|
||
export interface IAPIKey { | ||
name: string; | ||
workspace: string; | ||
environment: string; | ||
expiresAt: Date; | ||
prefix: string; | ||
apiKeyHash: string; | ||
encryptedKey: string; | ||
iv: string; | ||
tag: string; | ||
} | ||
|
||
const apiKeySchema = new Schema<IAPIKey>( | ||
{ | ||
name: { | ||
type: String, | ||
required: true | ||
}, | ||
workspace: { | ||
type: String | ||
}, | ||
environment: { | ||
type: String, | ||
enum: [ENV_DEV, ENV_TESTING, ENV_STAGING, ENV_PROD] | ||
}, | ||
expiresAt: { | ||
type: Date | ||
}, | ||
prefix: { | ||
type: String, | ||
required: true | ||
}, | ||
apiKeyHash: { | ||
type: String, | ||
unique: true, | ||
required: true | ||
}, | ||
encryptedKey: { | ||
type: String, | ||
select: true | ||
}, | ||
iv: { | ||
type: String, | ||
select: true | ||
}, | ||
tag: { | ||
type: String, | ||
select: true | ||
} | ||
}, | ||
{ | ||
timestamps: true | ||
} | ||
); | ||
|
||
const APIKey = model<IAPIKey>('APIKey', apiKeySchema); | ||
|
||
export default APIKey; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters