Store encrypted SDK in prompt session

This commit is contained in:
Dominik Korsa 2021-01-22 21:41:24 +01:00
parent 3992429236
commit 9aa8ee6105
No known key found for this signature in database
GPG key ID: 546F986F71A6FE6E
3 changed files with 42 additions and 3 deletions

View file

@ -5,6 +5,7 @@ import _ from 'lodash';
import {
Arg, Ctx, Mutation, Resolver,
} from 'type-graphql';
import type { SerializedSDK } from '../../../types';
import {
encryptSymmetrical, encryptWithPublicKey, generatePrivatePublicPair, isObject, verifyCaptchaResponse,
} from '../../../utils';
@ -44,15 +45,22 @@ export default class LoginResolver {
prompt.promptSecret,
);
const encryptedPassword = encryptWithPublicKey(password, publicKey);
console.log(diaryList.map((e) => e.serialized.info));
const students = _.toPairs(_.groupBy(diaryList.map((e) => e.serialized.info), 'studentId'))
const diaryStudents = _.groupBy(diaryList.map((e) => e.serialized.info), 'studentId');
const students = _.toPairs(diaryStudents)
.map(([, diaryInfoList]: [string, DiaryInfo[]]) => diaryInfoList[0])
.map<LoginResultStudent>((diaryInfo) => ({
name: `${diaryInfo.studentFirstName} ${diaryInfo.studentSurname}`,
studentId: diaryInfo.studentId,
}));
const serializedSDK: SerializedSDK = {
client: client.serialize(),
diaries: diaryList.map(({ serialized }) => serialized),
};
const encryptedSDK = encryptWithPublicKey(JSON.stringify(serializedSDK), publicKey);
prompt.loginInfo = {
encryptedPassword,
encryptedSDK,
publicKey,
host,
username,
availableStudentIds: students.map(({ studentId }) => studentId),

View file

@ -1,3 +1,5 @@
import type { SerializedClient } from '@wulkanowy/sdk/dist/diary/interfaces/serialized-client';
import type { SerializedDiary } from '@wulkanowy/sdk/dist/diary/interfaces/serialized-diary';
import type {
FastifyInstance,
FastifyReply,
@ -34,6 +36,8 @@ export interface Prompt {
host: string;
username: string;
encryptedPassword: string;
encryptedSDK: string;
publicKey: string;
availableStudentIds: number[];
};
}
@ -46,6 +50,11 @@ export interface Session {
data?: SessionData;
}
export interface SerializedSDK {
client: SerializedClient;
diaries: SerializedDiary[];
}
export type MyFastifyInstance = FastifyInstance<RawServerDefault, RawRequestDefaultExpression<RawServerDefault>, RawReplyDefaultExpression<RawServerDefault>>;
export interface ApolloContext {

View file

@ -1,8 +1,10 @@
import wulkanowy from '@wulkanowy/sdk';
import type { GetCredentialsFunction } from '@wulkanowy/sdk/dist/client/types';
import got from 'got';
import _ from 'lodash';
import { ParamError } from '../errors';
import SessionData from '../session-data';
import type { Session } from '../types';
import type { SerializedSDK, Session } from '../types';
export * from './crypto';
@ -76,3 +78,23 @@ export async function verifyCaptchaResponse(response: string): Promise<boolean>
console.log(body);
return body.success;
}
export function sdkToJSON(client: wulkanowy.Client, diaries: wulkanowy.Diary[]): string {
const serialized: SerializedSDK = {
client: client.serialize(),
diaries: diaries.map((diary) => diary.serialize()),
};
return JSON.stringify(serialized);
}
export function sdkFromJSON(json: string, getCredentials: GetCredentialsFunction): {
client: wulkanowy.Client,
diaries: wulkanowy.Diary[]
} {
const serialized = JSON.parse(json) as SerializedSDK;
const client = wulkanowy.Client.deserialize(serialized.client, getCredentials);
return {
client,
diaries: serialized.diaries.map((serializedDiary) => new wulkanowy.Diary(client, serializedDiary)),
};
}