Skip to content

Hmac for generating materials #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions wrongsecrets-balancer/src/teams/teams.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const express = require('express');
const bcrypt = require('bcryptjs');
const crypto = require('crypto');
const cryptoRandomString = require('crypto-random-string');

const Joi = require('@hapi/joi');
Expand Down Expand Up @@ -84,6 +85,28 @@ async function interceptAdminLogin(req, res, next) {
return next();
}

/**
* @param {import("express").Request} req
* @param {import("express").Response} res
* @param {import("express").NextFunction} next
*/
async function validateHMAC(req, res, next) {
try {
const { team } = req.params;
const { hmacvalue } = req.body;
const validationValue = crypto
.createHmac('sha256', 'hardcodedkey')
.update(`${team}`, 'utf-8')
.digest('hex');
if (validationValue === hmacvalue) {
return next();
}
res.status(403).send({ message: 'Invalid validation, please stop doing this!' });
} catch (error) {
res.status(500).send({ message: 'Invalid validation, please stop doing this!' });
}
}

/**
* @param {import("express").Request} req
* @param {import("express").Response} res
Expand Down Expand Up @@ -506,6 +529,7 @@ const paramsSchema = Joi.object({
.regex(/^[a-z0-9]([-a-z0-9])+[a-z0-9]$/),
});
const bodySchema = Joi.object({
hmacvalue: Joi.string().hex().length(64),
passcode: Joi.string().alphanum().uppercase().length(8),
});

Expand All @@ -518,6 +542,7 @@ router.post(
interceptAdminLogin,
joinIfTeamAlreadyExists,
checkIfMaxJuiceShopInstancesIsReached,
validateHMAC,
createTeam
);

Expand Down
3 changes: 2 additions & 1 deletion wrongsecrets-balancer/src/teams/teams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ test('create team creates a instance for team via k8s service', async () => {

await request(app)
.post('/balancer/teams/team42/join')
// .expect(200)
.send({ hmacvalue: '4c8dd1f1306727c537aa96f0c59968b719740f2a30ccda92044ea59622565564' })
.expect(200)
.then(({ body }) => {
expect(body.message).toBe('Created Instance');
expect(body.passcode).toMatch(/[a-zA-Z0-9]{7}/);
Expand Down
11 changes: 11 additions & 0 deletions wrongsecrets-balancer/ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions wrongsecrets-balancer/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dependencies": {
"@formatjs/intl-utils": "^3.8.4",
"axios": "^0.27.2",
"crypto-js": "^4.1.1",
"promise-retry": "^2.0.1",
"react": "^18.2.0",
"react-data-table-component": "^7.5.3",
Expand Down
10 changes: 7 additions & 3 deletions wrongsecrets-balancer/ui/src/pages/JoinPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { useNavigate, useLocation } from 'react-router-dom';
import { FormattedMessage, defineMessages, injectIntl } from 'react-intl';
import cryptoJS from 'crypto-js';

import styled from 'styled-components';

Expand Down Expand Up @@ -45,12 +46,15 @@ export const JoinPage = injectIntl(({ intl }) => {
const { formatMessage } = intl;

async function sendJoinRequest() {
if (window.confirm('Are you ready?')){
if (window.confirm('Are you ready?')) {
try {
const hmacvalue = cryptoJS
.HmacSHA256(`${teamname}`, 'hardcodedkey')
.toString(cryptoJS.enc.Hex);
const { data } = await axios.post(`/balancer/teams/${teamname}/join`, {
passcode,
hmacvalue,
});

navigate(`/teams/${teamname}/joined/`, { state: { passcode: data.passcode } });
} catch (error) {
if (
Expand All @@ -62,7 +66,7 @@ export const JoinPage = injectIntl(({ intl }) => {
setFailed(true);
}
}
}
}
}

function onSubmit(event) {
Expand Down