Skip to content
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

feat(iam): encrypt password at the client before sending the HTTP request #28

Merged
merged 7 commits into from
May 18, 2023
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
3 changes: 2 additions & 1 deletion .github/workflows/build-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ jobs:
NEXT_PUBLIC_SITE_KEY=${{ secrets.NEXT_PUBLIC_SITE_KEY }}
NEXT_PUBLIC_PHOTO_API=${{ secrets.NEXT_PUBLIC_PHOTO_API }}
NEXT_PUBLIC_PHOTO_API_KEY=${{ secrets.NEXT_PUBLIC_PHOTO_API_KEY }}
NEXT_PUBLIC_CRYPTO_KEY=${{ secrets.NEXT_PUBLIC_CRYPTO_KEY }}
# To improve build speeds, for each branch we push an additional image to the registry,
# to be used as the caching layer, using the `max` caching mode.
#
Expand All @@ -106,4 +107,4 @@ jobs:
type=registry,ref=${{ inputs.registry }}/${{ inputs.app_name }}:${{ env.GITHUB_REF_SLUG_URL }}-cache
type=registry,ref=${{ inputs.registry }}/${{ inputs.app_name }}:${{ github.event.repository.default_branch }}-cache
cache-to: |
type=registry,ref=${{ inputs.registry }}/${{ inputs.app_name }}:${{ env.GITHUB_REF_SLUG_URL }}-cache,mode=max
type=registry,ref=${{ inputs.registry }}/${{ inputs.app_name }}:${{ env.GITHUB_REF_SLUG_URL }}-cache,mode=min
3 changes: 2 additions & 1 deletion .github/workflows/cloudrun-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:

deploy:
name: Deploy to Cloud Run
needs: ["versioning"]
needs: ['versioning']
timeout-minutes: 10
runs-on: ubuntu-latest
permissions:
Expand Down Expand Up @@ -73,6 +73,7 @@ jobs:
NEXT_PUBLIC_PHOTO_API=${{ secrets.NEXT_PUBLIC_PHOTO_API }},
NEXT_PUBLIC_PHOTO_API_KEY=${{ secrets.NEXT_PUBLIC_PHOTO_API_KEY }},
NEXT_PUBLIC_COOKIE_KEY=${{ secrets.NEXT_PUBLIC_COOKIE_KEY }},
NEXT_PUBLIC_CRYPTO_KEY=${{ secrets.NEXT_PUBLIC_CRYPTO_KEY }},

- name: Allow unauthenticated calls to the service
run: |
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ ENV NEXT_PUBLIC_CEDULA_API_KEY=${NEXT_PUBLIC_CEDULA_API_KEY}
ARG NEXT_PUBLIC_SITE_KEY
ENV NEXT_PUBLIC_SITE_KEY=${NEXT_PUBLIC_SITE_KEY}

ARG NEXT_PUBLIC_CRYPTO_KEY
ENV NEXT_PUBLIC_CRYPTO_KEY=${NEXT_PUBLIC_CRYPTO_KEY}

# ===================== Install Deps =====================
FROM base as deps

Expand Down
3 changes: 2 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const nextConfig = {
NEXT_PUBLIC_CEDULA_API: process.env.NEXT_PUBLIC_CEDULA_API,
NEXT_PUBLIC_CEDULA_API_KEY: process.env.NEXT_PUBLIC_CEDULA_API_KEY,
NEXT_PUBLIC_SITE_KEY: process.env.NEXT_PUBLIC_SITE_KEY,
NEXT_PUBLIC_COOKIE_KEY: process.env.NEXT_PUBLIC_COOKIE_KEY
NEXT_PUBLIC_COOKIE_KEY: process.env.NEXT_PUBLIC_COOKIE_KEY,
NEXT_PUBLIC_CRYPTO_KEY: process.env.NEXT_PUBLIC_CRYPTO_KEY
},
reactStrictMode: false,
output: 'standalone'
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"axios": "^1.4.0",
"check-password-strength": "^2.0.7",
"cookie": "^0.5.0",
"cryptr": "^6.2.0",
"eslint": "^8.40.0",
"eslint-config-next": "13.2.3",
"next": "13.2.3",
Expand Down
22 changes: 22 additions & 0 deletions src/helpers/cryptr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import getConfig from 'next/config';
import Cryptr from 'cryptr';

const DEFAULT_KEY = 'sf_d23424bdsdfkj@f42e@wdn5fgsdfdm**2asbn!d';
const { publicRuntimeConfig } = getConfig();
const NEXT_PUBLIC_CRYPTO_KEY =
publicRuntimeConfig.NEXT_PUBLIC_CRYPTO_KEY || DEFAULT_KEY;

const cryptr = new Cryptr(NEXT_PUBLIC_CRYPTO_KEY, {
pbkdf2Iterations: 10000,
saltLength: 10,
});

export class Crypto {
static encrypt(str: string): string {
return cryptr.encrypt(str);
}

static decrypt(token: string): string {
return cryptr.decrypt(token);
}
}
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { getRekognitionClient } from './rekognition';
export { Crypto } from './cryptr';
4 changes: 3 additions & 1 deletion src/pages/api/iam/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CitizensBasicInformationResponse,
VerifyIamUserResponse,
} from '../types';
import { Crypto } from '@/helpers';

export default async function handler(
req: NextApiRequest,
Expand All @@ -29,7 +30,8 @@ export default async function handler(
return res.status(200).json(data);
} else if (req.method === 'POST') {
const { body } = req;
const { username, email, password } = body;
const { username, email } = body;
const password = Crypto.decrypt(body.password);

let success = true;
let statusCode = 201;
Expand Down
9 changes: 6 additions & 3 deletions src/pages/register/stepper/step3.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { passwordStrength } from 'check-password-strength';
import { yupResolver } from '@hookform/resolvers/yup';
import { useForm } from 'react-hook-form';
import { useState } from 'react';
import * as yup from 'yup';
import axios from 'axios';
import { passwordStrength } from 'check-password-strength';

import { AlertError, AlertWarning } from '@/components/elements/alert';
import { GridContainer, GridItem } from '@/components/elements/grid';
import LoadingBackdrop from '@/components/elements/loadingBackdrop';
import PasswordLevel from '@/components/elements/passwordLevel';
import { TextBody } from '@/components/elements/typography';
import { FormControlApp } from '@/components/form/input';
import { ButtonApp } from '@/components/elements/button';
import { InputApp } from '@/themes/form/input';
import { labels } from '@/constants/labels';
import PasswordLevel from '@/components/elements/passwordLevel';
import { Crypto } from '@/helpers';

interface IFormInputs {
email: string;
Expand Down Expand Up @@ -65,11 +66,13 @@ export default function Step3({ handleNext, infoCedula }: any) {
if (passwordLevel.id !== 3) return;
setLoading(true);

const password = Crypto.encrypt(data.password);

axios
.post('/api/iam', {
email: data.email,
username: infoCedula.id,
password: data.password,
password,
})
.then(() => {
handleNext();
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7294,6 +7294,11 @@ cross-spawn@^7.0.2, cross-spawn@^7.0.3:
shebang-command "^2.0.0"
which "^2.0.1"

cryptr@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/cryptr/-/cryptr-6.2.0.tgz#30dd4754447f9f5d18d5ea8a8dfbae08f00e5cbb"
integrity sha512-jYi8SxvOFebTT7EYOABiPpHKY6lwWaP9IVcvT/aIVJUVoFdzTgi0ySPCL78q1ig8w2kwfXFCZACXoCXaye57aw==

csscolorparser@~1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/csscolorparser/-/csscolorparser-1.0.3.tgz#b34f391eea4da8f3e98231e2ccd8df9c041f171b"
Expand Down