Generate separate private key encrypt keys for each prompt

This commit is contained in:
Dominik Korsa 2021-01-19 20:56:57 +01:00
parent c1c6812180
commit cc10ff441f
No known key found for this signature in database
GPG key ID: 546F986F71A6FE6E
5 changed files with 11 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import { ParamError, ScopeError } from '../../errors';
import type { MyFastifyInstance, StudentsMode } from '../../types'; import type { MyFastifyInstance, StudentsMode } from '../../types';
import { import {
createKey,
getSessionData, isObject, parseScopeParam, validateOptionalParam, validateParam, getSessionData, isObject, parseScopeParam, validateOptionalParam, validateParam,
} from '../../utils'; } from '../../utils';
@ -79,6 +80,7 @@ export default function registerAuthorize(server: MyFastifyInstance): void {
value: request.query.code_challenge, value: request.query.code_challenge,
}, },
studentsMode, studentsMode,
promptSecret: createKey(),
}); });
await reply.redirect(urlJoin(websitePrefix, `/authenticate-prompt?prompt_id=${promptId}`)); await reply.redirect(urlJoin(websitePrefix, `/authenticate-prompt?prompt_id=${promptId}`));

View file

@ -6,7 +6,7 @@ import {
Arg, Ctx, Mutation, Resolver, Arg, Ctx, Mutation, Resolver,
} from 'type-graphql'; } from 'type-graphql';
import { import {
encryptSymmetrical, encryptWithPublicKey, generatePrivatePublicPair, isObject, requireEnvHex, encryptSymmetrical, encryptWithPublicKey, generatePrivatePublicPair, isObject,
} from '../../../utils'; } from '../../../utils';
import { InvalidVulcanCredentialsError, UnknownPromptError } from '../errors'; import { InvalidVulcanCredentialsError, UnknownPromptError } from '../errors';
import LoginResult from '../models/login-result'; import LoginResult from '../models/login-result';
@ -39,7 +39,7 @@ export default class LoginResolver {
const { privateKey, publicKey } = await generatePrivatePublicPair(); const { privateKey, publicKey } = await generatePrivatePublicPair();
const encryptedPrivateKey = encryptSymmetrical( const encryptedPrivateKey = encryptSymmetrical(
privateKey, privateKey,
requireEnvHex('CREDENTIALS_PRIVATE_KEY_ENCRYPT_KEY'), prompt.promptSecret,
); );
const encryptedPassword = encryptWithPublicKey(password, publicKey); const encryptedPassword = encryptWithPublicKey(password, publicKey);
console.log(diaryList.map((e) => e.serialized.info)); console.log(diaryList.map((e) => e.serialized.info));

View file

@ -24,6 +24,7 @@ export interface Prompt {
method: 'plain' | 'S256'; method: 'plain' | 'S256';
}; };
studentsMode: StudentsMode; studentsMode: StudentsMode;
promptSecret: Buffer;
loginInfo?: { loginInfo?: {
host: string; host: string;
username: string; username: string;

View file

@ -18,6 +18,10 @@ export function generatePrivatePublicPair(): Promise<{
}); });
} }
export function createKey(): Buffer {
return crypto.randomBytes(32);
}
export function encryptSymmetrical(value: string, key: Buffer): string { export function encryptSymmetrical(value: string, key: Buffer): string {
const ivBuffer = crypto.randomBytes(16); const ivBuffer = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-cbc', key, ivBuffer); const cipher = crypto.createCipheriv('aes-256-cbc', key, ivBuffer);

View file

@ -11,8 +11,8 @@ export function requireEnv(name: string): string {
return value; return value;
} }
export function requireEnvHex(name: string): Buffer { export function requireEnvBase64(name: string): Buffer {
return Buffer.from(requireEnv(name), 'hex'); return Buffer.from(requireEnv(name), 'base64');
} }
export function parseIntStrict(value: string, radix = 10): number { export function parseIntStrict(value: string, radix = 10): number {