-
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.
Merge remote-tracking branch 'origin' into cleaning
- Loading branch information
Showing
13 changed files
with
273 additions
and
17 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
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,106 @@ | ||
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'; | ||
|
||
/** | ||
* Return API key data for user with id [req.user_id] | ||
* @param req | ||
* @param res | ||
* @returns | ||
*/ | ||
export const getAPIKeyData = async (req: Request, res: Response) => { | ||
let apiKeyData; | ||
try { | ||
apiKeyData = await APIKeyData.find({ | ||
user: req.user._id | ||
}); | ||
} catch (err) { | ||
Sentry.setUser({ email: req.user.email }); | ||
Sentry.captureException(err); | ||
return res.status(400).send({ | ||
message: 'Failed to get API key data' | ||
}); | ||
} | ||
|
||
return res.status(200).send({ | ||
apiKeyData | ||
}); | ||
} | ||
|
||
/** | ||
* Create new API key data for user with id [req.user._id] | ||
* @param req | ||
* @param res | ||
*/ | ||
export const createAPIKeyData = 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 | ||
}).save(); | ||
|
||
// 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) { | ||
console.error(err); | ||
Sentry.setUser({ email: req.user.email }); | ||
Sentry.captureException(err); | ||
return res.status(400).send({ | ||
message: 'Failed to API key data' | ||
}); | ||
} | ||
|
||
return res.status(200).send({ | ||
apiKey, | ||
apiKeyData | ||
}); | ||
} | ||
|
||
/** | ||
* Delete API key data with id [apiKeyDataId]. | ||
* @param req | ||
* @param res | ||
* @returns | ||
*/ | ||
export const deleteAPIKeyData = async (req: Request, res: Response) => { | ||
let apiKeyData; | ||
try { | ||
const { apiKeyDataId } = req.params; | ||
|
||
apiKeyData = await APIKeyData.findByIdAndDelete(apiKeyDataId); | ||
|
||
} catch (err) { | ||
Sentry.setUser({ email: req.user.email }); | ||
Sentry.captureException(err); | ||
return res.status(400).send({ | ||
message: 'Failed to delete API key data' | ||
}); | ||
} | ||
|
||
return res.status(200).send({ | ||
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
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,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,39 @@ | ||
import express from 'express'; | ||
const router = express.Router(); | ||
import { | ||
requireAuth, | ||
validateRequest | ||
} from '../../middleware'; | ||
import { param, body } from 'express-validator'; | ||
import { apiKeyDataController } from '../../controllers/v2'; | ||
|
||
router.get( | ||
'/', | ||
requireAuth({ | ||
acceptedAuthModes: ['jwt'] | ||
}), | ||
apiKeyDataController.getAPIKeyData | ||
); | ||
|
||
router.post( | ||
'/', | ||
requireAuth({ | ||
acceptedAuthModes: ['jwt'] | ||
}), | ||
body('name').exists().trim(), | ||
body('expiresIn'), // measured in ms | ||
validateRequest, | ||
apiKeyDataController.createAPIKeyData | ||
); | ||
|
||
router.delete( | ||
'/:apiKeyDataId', | ||
requireAuth({ | ||
acceptedAuthModes: ['jwt'] | ||
}), | ||
param('apiKeyDataId').exists().trim(), | ||
validateRequest, | ||
apiKeyDataController.deleteAPIKeyData | ||
); | ||
|
||
export default router; |
Oops, something went wrong.