diff --git a/backend/src/routes/website-api/resolvers/login-resolver.ts b/backend/src/routes/website-api/resolvers/login-resolver.ts index 361cc7a..13caac5 100644 --- a/backend/src/routes/website-api/resolvers/login-resolver.ts +++ b/backend/src/routes/website-api/resolvers/login-resolver.ts @@ -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((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), diff --git a/backend/src/types.ts b/backend/src/types.ts index 1840cd1..8101df6 100644 --- a/backend/src/types.ts +++ b/backend/src/types.ts @@ -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, RawReplyDefaultExpression>; export interface ApolloContext { diff --git a/backend/src/utils/index.ts b/backend/src/utils/index.ts index f902049..541915f 100644 --- a/backend/src/utils/index.ts +++ b/backend/src/utils/index.ts @@ -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 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)), + }; +}