-
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.
- Loading branch information
1 parent
fc61849
commit 6f054d8
Showing
7 changed files
with
80 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
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,50 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { UnauthorizedRequestError, SecretNotFoundError } from '../utils/errors'; | ||
import { Secret } from '../models'; | ||
import { | ||
validateMembership | ||
} from '../helpers/membership'; | ||
|
||
/** | ||
* Validate if user on request has proper membership to modify secret. | ||
* @param {Object} obj | ||
* @param {String[]} obj.acceptedRoles - accepted workspace roles | ||
* @param {String[]} obj.acceptedStatuses - accepted workspace statuses | ||
* @param {String[]} obj.location - location of [workspaceId] on request (e.g. params, body) for parsing | ||
*/ | ||
const requireSecretAuth = ({ | ||
acceptedRoles, | ||
acceptedStatuses | ||
}: { | ||
acceptedRoles: string[]; | ||
acceptedStatuses: string[]; | ||
}) => { | ||
return async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const { secretId } = req.params; | ||
|
||
const secret = await Secret.findById(secretId); | ||
|
||
if (!secret) { | ||
return next(SecretNotFoundError({ | ||
message: 'Failed to find secret' | ||
})); | ||
} | ||
|
||
await validateMembership({ | ||
userId: req.user._id.toString(), | ||
workspaceId: secret.workspace.toString(), | ||
acceptedRoles, | ||
acceptedStatuses | ||
}); | ||
|
||
req.secret = secret as any; | ||
|
||
next(); | ||
} catch (err) { | ||
return next(UnauthorizedRequestError({ message: 'Unable to authenticate secret' })); | ||
} | ||
} | ||
} | ||
|
||
export default requireSecretAuth; |
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