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 { import {
Arg, Ctx, Mutation, Resolver, Arg, Ctx, Mutation, Resolver,
} from 'type-graphql'; } from 'type-graphql';
import type { SerializedSDK } from '../../../types';
import { import {
encryptSymmetrical, encryptWithPublicKey, generatePrivatePublicPair, isObject, verifyCaptchaResponse, encryptSymmetrical, encryptWithPublicKey, generatePrivatePublicPair, isObject, verifyCaptchaResponse,
} from '../../../utils'; } from '../../../utils';
@ -44,15 +45,22 @@ export default class LoginResolver {
prompt.promptSecret, prompt.promptSecret,
); );
const encryptedPassword = encryptWithPublicKey(password, publicKey); const encryptedPassword = encryptWithPublicKey(password, publicKey);
console.log(diaryList.map((e) => e.serialized.info)); const diaryStudents = _.groupBy(diaryList.map((e) => e.serialized.info), 'studentId');
const students = _.toPairs(_.groupBy(diaryList.map((e) => e.serialized.info), 'studentId')) const students = _.toPairs(diaryStudents)
.map(([, diaryInfoList]: [string, DiaryInfo[]]) => diaryInfoList[0]) .map(([, diaryInfoList]: [string, DiaryInfo[]]) => diaryInfoList[0])
.map<LoginResultStudent>((diaryInfo) => ({ .map<LoginResultStudent>((diaryInfo) => ({
name: `${diaryInfo.studentFirstName} ${diaryInfo.studentSurname}`, name: `${diaryInfo.studentFirstName} ${diaryInfo.studentSurname}`,
studentId: diaryInfo.studentId, studentId: diaryInfo.studentId,
})); }));
const serializedSDK: SerializedSDK = {
client: client.serialize(),
diaries: diaryList.map(({ serialized }) => serialized),
};
const encryptedSDK = encryptWithPublicKey(JSON.stringify(serializedSDK), publicKey);
prompt.loginInfo = { prompt.loginInfo = {
encryptedPassword, encryptedPassword,
encryptedSDK,
publicKey,
host, host,
username, username,
availableStudentIds: students.map(({ studentId }) => studentId), 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 { import type {
FastifyInstance, FastifyInstance,
FastifyReply, FastifyReply,
@ -34,6 +36,8 @@ export interface Prompt {
host: string; host: string;
username: string; username: string;
encryptedPassword: string; encryptedPassword: string;
encryptedSDK: string;
publicKey: string;
availableStudentIds: number[]; availableStudentIds: number[];
}; };
} }
@ -46,6 +50,11 @@ export interface Session {
data?: SessionData; data?: SessionData;
} }
export interface SerializedSDK {
client: SerializedClient;
diaries: SerializedDiary[];
}
export type MyFastifyInstance = FastifyInstance<RawServerDefault, RawRequestDefaultExpression<RawServerDefault>, RawReplyDefaultExpression<RawServerDefault>>; export type MyFastifyInstance = FastifyInstance<RawServerDefault, RawRequestDefaultExpression<RawServerDefault>, RawReplyDefaultExpression<RawServerDefault>>;
export interface ApolloContext { 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 got from 'got';
import _ from 'lodash'; import _ from 'lodash';
import { ParamError } from '../errors'; import { ParamError } from '../errors';
import SessionData from '../session-data'; import SessionData from '../session-data';
import type { Session } from '../types'; import type { SerializedSDK, Session } from '../types';
export * from './crypto'; export * from './crypto';
@ -76,3 +78,23 @@ export async function verifyCaptchaResponse(response: string): Promise<boolean>
console.log(body); console.log(body);
return body.success; 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)),
};
}