-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
212ca72
commit ff0b053
Showing
7 changed files
with
123 additions
and
6 deletions.
There are no files selected for viewing
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,54 @@ | ||
import { Request, Response } from 'express'; | ||
import * as Sentry from '@sentry/node'; | ||
import crypto from 'crypto'; | ||
import bcrypt from 'bcrypt'; | ||
import { | ||
APIKeyData | ||
} from '../../models'; | ||
import { | ||
SALT_ROUNDS | ||
} from '../../config'; | ||
|
||
/** | ||
* Create new API key for user with id [req.user._id] | ||
* @param req | ||
* @param res | ||
*/ | ||
export const createAPIKey = async (req: Request, res: Response) => { | ||
let apiKey, apiKeyData; | ||
try { | ||
const { name, expiresIn } = req.body; | ||
|
||
const secret = crypto.randomBytes(16).toString('hex'); | ||
const secretHash = await bcrypt.hash(secret, SALT_ROUNDS); | ||
|
||
const expiresAt = new Date(); | ||
expiresAt.setSeconds(expiresAt.getSeconds() + expiresIn); | ||
|
||
apiKeyData = await new APIKeyData({ | ||
name, | ||
expiresAt, | ||
user: req.user._id, | ||
secretHash | ||
}); | ||
|
||
// return api key data without sensitive data | ||
apiKeyData = await APIKeyData.findById(apiKeyData._id); | ||
|
||
if (!apiKeyData) throw new Error('Failed to find API key data'); | ||
|
||
apiKey = `ak.${apiKeyData._id.toString()}.${secret}`; | ||
|
||
} catch (err) { | ||
Sentry.setUser({ email: req.user.email }); | ||
Sentry.captureException(err); | ||
return res.status(400).send({ | ||
message: 'Failed to create service token data' | ||
}); | ||
} | ||
|
||
return res.status(200).send({ | ||
apiKey, | ||
apiKeyData | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import * as workspaceController from './workspaceController'; | ||
import * as serviceTokenDataController from './serviceTokenDataController'; | ||
import * as apiKeyDataController from './apiKeyDataController'; | ||
|
||
export { | ||
workspaceController, | ||
serviceTokenDataController | ||
serviceTokenDataController, | ||
apiKeyDataController | ||
} |
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,37 @@ | ||
import { Schema, model, Types } from 'mongoose'; | ||
|
||
export interface IAPIKeyData { | ||
name: string; | ||
user: Types.ObjectId; | ||
expiresAt: Date; | ||
secretHash: string; | ||
} | ||
|
||
const apiKeyDataSchema = new Schema<IAPIKeyData>( | ||
{ | ||
name: { | ||
type: String, | ||
required: true | ||
}, | ||
user: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: true | ||
}, | ||
expiresAt: { | ||
type: Date | ||
}, | ||
secretHash: { | ||
type: String, | ||
required: true, | ||
select: false | ||
} | ||
}, | ||
{ | ||
timestamps: true | ||
} | ||
); | ||
|
||
const APIKeyData = model<IAPIKeyData>('APIKeyData', apiKeyDataSchema); | ||
|
||
export default APIKeyData; |
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,21 @@ | ||
import express from 'express'; | ||
const router = express.Router(); | ||
import { | ||
requireAuth, | ||
validateRequest | ||
} from '../../middleware'; | ||
import { body } from 'express-validator'; | ||
import { apiKeyDataController } from '../../controllers/v2'; | ||
|
||
router.post( | ||
'/', | ||
requireAuth({ | ||
acceptedAuthModes: ['jwt'] | ||
}), | ||
body('name').exists().trim(), | ||
body('expiresIn'), // measured in ms | ||
validateRequest, | ||
apiKeyDataController.createAPIKey | ||
); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import secret from './secret'; | ||
import workspace from './workspace'; | ||
import serviceTokenData from './serviceTokenData'; | ||
import apiKeyData from './apiKeyData'; | ||
|
||
export { | ||
secret, | ||
workspace, | ||
serviceTokenData | ||
serviceTokenData, | ||
apiKeyData | ||
} |