Skip to content

Commit

Permalink
modify getServiceTokenData to return single json
Browse files Browse the repository at this point in the history
  • Loading branch information
maidul98 committed Jan 5, 2023
1 parent d75d9ec commit 5428766
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions backend/src/controllers/v2/serviceTokenDataController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import {
* @param res
* @returns
*/
export const getServiceTokenData = async (req: Request, res: Response) => res.status(200).send({
serviceTokenData: req.serviceTokenData
});
export const getServiceTokenData = async (req: Request, res: Response) => res.status(200).json(req.serviceTokenData);

/**
* Create new service token data for workspace with id [workspaceId] and
Expand All @@ -29,9 +27,9 @@ export const getServiceTokenData = async (req: Request, res: Response) => res.st
export const createServiceTokenData = async (req: Request, res: Response) => {
let serviceToken, serviceTokenData;
try {
const {
const {
name,
workspaceId,
workspaceId,
environment,
encryptedKey,
iv,
Expand All @@ -41,10 +39,10 @@ export const createServiceTokenData = async (req: Request, res: Response) => {

const secret = crypto.randomBytes(16).toString('hex');
const secretHash = await bcrypt.hash(secret, SALT_ROUNDS);
const expiresAt = new Date();
expiresAt.setSeconds(expiresAt.getSeconds() + expiresIn);

const expiresAt = new Date();
expiresAt.setSeconds(expiresAt.getSeconds() + expiresIn);

serviceTokenData = await new ServiceTokenData({
name,
workspace: workspaceId,
Expand All @@ -56,12 +54,12 @@ export const createServiceTokenData = async (req: Request, res: Response) => {
iv,
tag
}).save();

// return service token data without sensitive data
serviceTokenData = await ServiceTokenData.findById(serviceTokenData._id);

if (!serviceTokenData) throw new Error('Failed to find service token data');

serviceToken = `st.${serviceTokenData._id.toString()}.${secret}`;

} catch (err) {
Expand Down Expand Up @@ -90,15 +88,15 @@ export const deleteServiceTokenData = async (req: Request, res: Response) => {
const { serviceTokenDataId } = req.params;

serviceTokenData = await ServiceTokenData.findByIdAndDelete(serviceTokenDataId);

} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: 'Failed to delete service token data'
});
}

return res.status(200).send({
serviceTokenData
});
Expand Down

1 comment on commit 5428766

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report for backend

St.
Category Percentage Covered / Total
🟡 Statements 71.01% 49/69
🔴 Branches 0% 0/5
🔴 Functions 50% 1/2
🟡 Lines 72.06% 49/68

Test suite run success

1 tests passing in 1 suite.

Report generated by 🧪jest coverage report action from 5428766

Please sign in to comment.