From 9b638c1c73dcda8766e3df86429da6e1e4fdd86a Mon Sep 17 00:00:00 2001 From: Dominik Korsa Date: Wed, 27 Jan 2021 09:23:27 +0100 Subject: [PATCH] Refactor code --- backend/src/routes/oauth2/authorize.ts | 2 +- backend/src/routes/website-api/allow.ts | 4 ++-- backend/src/routes/website-api/deny.ts | 4 ++-- backend/src/routes/website-api/index.ts | 8 ++++---- .../create-user-resolver.ts | 12 ++++++------ .../{ => authenticate-prompt}/login-resolver.ts | 10 +++++----- .../prompt-info-resolver.ts | 14 +++++++------- .../set-symbol-resolver.ts | 16 ++++++++-------- backend/src/session-data.ts | 4 ++-- backend/src/types.ts | 2 +- backend/src/utils/index.ts | 1 - 11 files changed, 38 insertions(+), 39 deletions(-) rename backend/src/routes/website-api/resolvers/{ => authenticate-prompt}/create-user-resolver.ts (81%) rename backend/src/routes/website-api/resolvers/{ => authenticate-prompt}/login-resolver.ts (91%) rename backend/src/routes/website-api/resolvers/{ => authenticate-prompt}/prompt-info-resolver.ts (75%) rename backend/src/routes/website-api/resolvers/{ => authenticate-prompt}/set-symbol-resolver.ts (88%) diff --git a/backend/src/routes/oauth2/authorize.ts b/backend/src/routes/oauth2/authorize.ts index 79552bf..0ba44bf 100644 --- a/backend/src/routes/oauth2/authorize.ts +++ b/backend/src/routes/oauth2/authorize.ts @@ -87,7 +87,7 @@ export default function registerAuthorize(server: MyFastifyInstance): void { const promptId = nanoid(12); const sessionData = getSessionData(request.session); - sessionData.prompts.set(promptId, { + sessionData.authPrompts.set(promptId, { clientId: request.query.client_id, redirectUri: request.query.redirect_uri, scopes: requestedScopes, diff --git a/backend/src/routes/website-api/allow.ts b/backend/src/routes/website-api/allow.ts index db78fb4..7a86f47 100644 --- a/backend/src/routes/website-api/allow.ts +++ b/backend/src/routes/website-api/allow.ts @@ -32,7 +32,7 @@ export default function registerAllow(server: MyFastifyInstance): void { if (!encryptedTokenKey) throw server.httpErrors.badRequest('Missing etk cookie'); const sessionData = getSessionData(request.session); - const prompt = sessionData.prompts.get(request.query.prompt_id); + const prompt = sessionData.authPrompts.get(request.query.prompt_id); if (!prompt) throw server.httpErrors.badRequest('Prompt data not found'); if (!prompt.loginInfo) throw server.httpErrors.badRequest('Login data not provided'); if (!prompt.loginInfo.symbolInfo) throw server.httpErrors.badRequest('Symbol not provided'); @@ -86,6 +86,6 @@ export default function registerAllow(server: MyFastifyInstance): void { redirectUri.searchParams.set('code', code); if (prompt.state) redirectUri.searchParams.set('state', prompt.state); await reply.redirect(redirectUri.toString()); - sessionData.prompts.delete(request.query.prompt_id); + sessionData.authPrompts.delete(request.query.prompt_id); }); } diff --git a/backend/src/routes/website-api/deny.ts b/backend/src/routes/website-api/deny.ts index 75762fc..491320d 100644 --- a/backend/src/routes/website-api/deny.ts +++ b/backend/src/routes/website-api/deny.ts @@ -27,9 +27,9 @@ export default function registerDeny(server: MyFastifyInstance): void { await new Promise((resolve) => setTimeout(resolve, 100)); const sessionData = getSessionData(request.session); - const prompt = sessionData.prompts.get(request.query.prompt_id); + const prompt = sessionData.authPrompts.get(request.query.prompt_id); if (!prompt) throw server.httpErrors.badRequest('Prompt data not found'); await reply.redirect(`${prompt.redirectUri}?error=access_denied&error_description=${encodeURIComponent('User denied')}`); - sessionData.prompts.delete(request.query.prompt_id); + sessionData.authPrompts.delete(request.query.prompt_id); }); } diff --git a/backend/src/routes/website-api/index.ts b/backend/src/routes/website-api/index.ts index 62f006b..b240ba9 100644 --- a/backend/src/routes/website-api/index.ts +++ b/backend/src/routes/website-api/index.ts @@ -4,10 +4,10 @@ import type { ApolloContext, MyFastifyInstance } from '../../types'; import { getSessionData } from '../../utils'; import registerAllow from './allow'; import registerDeny from './deny'; -import CreateUserResolver from './resolvers/create-user-resolver'; -import LoginResolver from './resolvers/login-resolver'; -import PromptInfoResolver from './resolvers/prompt-info-resolver'; -import SetSymbolResolver from './resolvers/set-symbol-resolver'; +import CreateUserResolver from './resolvers/authenticate-prompt/create-user-resolver'; +import LoginResolver from './resolvers/authenticate-prompt/login-resolver'; +import PromptInfoResolver from './resolvers/authenticate-prompt/prompt-info-resolver'; +import SetSymbolResolver from './resolvers/authenticate-prompt/set-symbol-resolver'; import type { WebsiteAPIContext } from './types'; export default async function registerWebsiteApi(server: MyFastifyInstance): Promise { diff --git a/backend/src/routes/website-api/resolvers/create-user-resolver.ts b/backend/src/routes/website-api/resolvers/authenticate-prompt/create-user-resolver.ts similarity index 81% rename from backend/src/routes/website-api/resolvers/create-user-resolver.ts rename to backend/src/routes/website-api/resolvers/authenticate-prompt/create-user-resolver.ts index 3525988..3d109f9 100644 --- a/backend/src/routes/website-api/resolvers/create-user-resolver.ts +++ b/backend/src/routes/website-api/resolvers/authenticate-prompt/create-user-resolver.ts @@ -3,11 +3,11 @@ import { UserInputError } from 'apollo-server-fastify'; import { Arg, Ctx, Mutation, Resolver, } from 'type-graphql'; -import database from '../../../database/database'; -import User from '../../../database/entities/user'; -import { UnknownPromptError } from '../errors'; -import CreateUserResult from '../models/create-user-result'; -import type { WebsiteAPIContext } from '../types'; +import database from '../../../../database/database'; +import User from '../../../../database/entities/user'; +import { UnknownPromptError } from '../../errors'; +import CreateUserResult from '../../models/create-user-result'; +import type { WebsiteAPIContext } from '../../types'; @Resolver() export default class CreateUserResolver { @@ -18,7 +18,7 @@ export default class CreateUserResolver { @Ctx() { sessionData }: WebsiteAPIContext, ): Promise { if (email !== email.trim()) throw new UserInputError('Email should be trimmed'); - const prompt = sessionData.prompts.get(promptId); + const prompt = sessionData.authPrompts.get(promptId); if (!prompt) throw new UnknownPromptError(); if (!prompt.loginInfo) throw new UserInputError('Login data not provided'); if (!prompt.loginInfo.symbolInfo) throw new UserInputError('Symbol not provided'); diff --git a/backend/src/routes/website-api/resolvers/login-resolver.ts b/backend/src/routes/website-api/resolvers/authenticate-prompt/login-resolver.ts similarity index 91% rename from backend/src/routes/website-api/resolvers/login-resolver.ts rename to backend/src/routes/website-api/resolvers/authenticate-prompt/login-resolver.ts index 7eacd2e..340cfe0 100644 --- a/backend/src/routes/website-api/resolvers/login-resolver.ts +++ b/backend/src/routes/website-api/resolvers/authenticate-prompt/login-resolver.ts @@ -7,10 +7,10 @@ import { import { createKey, encryptSymmetrical, encryptWithPublicKey, generatePrivatePublicPair, isObject, verifyCaptchaResponse, -} from '../../../utils'; -import { CaptchaError, InvalidVulcanCredentialsError, UnknownPromptError } from '../errors'; -import LoginResult from '../models/login-result'; -import type { WebsiteAPIContext } from '../types'; +} from '../../../../utils'; +import { CaptchaError, InvalidVulcanCredentialsError, UnknownPromptError } from '../../errors'; +import LoginResult from '../../models/login-result'; +import type { WebsiteAPIContext } from '../../types'; @Resolver() export default class LoginResolver { @@ -25,7 +25,7 @@ export default class LoginResolver { ): Promise { if (username !== username.trim()) throw new UserInputError('Username should be trimmed'); if (host !== host.trim()) throw new UserInputError('Host should be trimmed'); - const prompt = sessionData.prompts.get(promptId); + const prompt = sessionData.authPrompts.get(promptId); if (!prompt) throw new UnknownPromptError(); if (!await verifyCaptchaResponse(captchaResponse)) throw new CaptchaError(); const client = new Client(host, () => ({ diff --git a/backend/src/routes/website-api/resolvers/prompt-info-resolver.ts b/backend/src/routes/website-api/resolvers/authenticate-prompt/prompt-info-resolver.ts similarity index 75% rename from backend/src/routes/website-api/resolvers/prompt-info-resolver.ts rename to backend/src/routes/website-api/resolvers/authenticate-prompt/prompt-info-resolver.ts index 9390529..c11be1c 100644 --- a/backend/src/routes/website-api/resolvers/prompt-info-resolver.ts +++ b/backend/src/routes/website-api/resolvers/authenticate-prompt/prompt-info-resolver.ts @@ -3,12 +3,12 @@ import type { ResolverInterface } from 'type-graphql'; import { Arg, Ctx, FieldResolver, Query, Resolver, Root, } from 'type-graphql'; -import database from '../../../database/database'; -import { getUser } from '../../../graphql/github/sdk'; -import { UnknownPromptError } from '../errors'; -import PromptInfo from '../models/prompt-info'; -import type PromptInfoApplication from '../models/prompt-info-application'; -import type { WebsiteAPIContext } from '../types'; +import database from '../../../../database/database'; +import { getUser } from '../../../../graphql/github/sdk'; +import { UnknownPromptError } from '../../errors'; +import PromptInfo from '../../models/prompt-info'; +import type PromptInfoApplication from '../../models/prompt-info-application'; +import type { WebsiteAPIContext } from '../../types'; @Resolver(PromptInfo) export default class PromptInfoResolver implements ResolverInterface { @@ -17,7 +17,7 @@ export default class PromptInfoResolver implements ResolverInterface @Arg('promptId') promptId: string, @Ctx() { sessionData }: WebsiteAPIContext, ): Partial { - const prompt = sessionData.prompts.get(promptId); + const prompt = sessionData.authPrompts.get(promptId); if (!prompt) throw new UnknownPromptError(); return { id: promptId, diff --git a/backend/src/routes/website-api/resolvers/set-symbol-resolver.ts b/backend/src/routes/website-api/resolvers/authenticate-prompt/set-symbol-resolver.ts similarity index 88% rename from backend/src/routes/website-api/resolvers/set-symbol-resolver.ts rename to backend/src/routes/website-api/resolvers/authenticate-prompt/set-symbol-resolver.ts index 5e3764c..2d3b03a 100644 --- a/backend/src/routes/website-api/resolvers/set-symbol-resolver.ts +++ b/backend/src/routes/website-api/resolvers/authenticate-prompt/set-symbol-resolver.ts @@ -7,13 +7,13 @@ import _ from 'lodash'; import { Arg, Ctx, Mutation, Resolver, } from 'type-graphql'; -import database from '../../../database/database'; -import User from '../../../database/entities/user'; -import { decryptSymmetrical, decryptWithPrivateKey, encryptSymmetrical } from '../../../utils'; -import { InvalidSymbolError, UnknownPromptError } from '../errors'; -import type LoginStudent from '../models/login-student'; -import SetSymbolResult from '../models/set-symbol-result'; -import type { WebsiteAPIContext } from '../types'; +import database from '../../../../database/database'; +import User from '../../../../database/entities/user'; +import { decryptSymmetrical, decryptWithPrivateKey, encryptSymmetrical } from '../../../../utils'; +import { InvalidSymbolError, UnknownPromptError } from '../../errors'; +import type LoginStudent from '../../models/login-student'; +import SetSymbolResult from '../../models/set-symbol-result'; +import type { WebsiteAPIContext } from '../../types'; @Resolver() export default class SetSymbolResolver { @@ -25,7 +25,7 @@ export default class SetSymbolResolver { ): Promise { if (symbol !== symbol.trim()) throw new UserInputError('Symbol should be trimmed'); if (symbol !== symbol.toLowerCase()) throw new UserInputError('Symbol should be lowercase'); - const prompt = sessionData.prompts.get(promptId); + const prompt = sessionData.authPrompts.get(promptId); if (!prompt) throw new UnknownPromptError(); if (!prompt.loginInfo) throw new UserInputError('Login data not provided'); const { loginInfo } = prompt; diff --git a/backend/src/session-data.ts b/backend/src/session-data.ts index 3f93f8c..c749e8c 100644 --- a/backend/src/session-data.ts +++ b/backend/src/session-data.ts @@ -1,5 +1,5 @@ -import type { Prompt } from './types'; +import type { AuthPrompt } from './types'; export default class SessionData { - public prompts = new Map(); + public authPrompts = new Map(); } diff --git a/backend/src/types.ts b/backend/src/types.ts index 44d7d9c..4b70d0d 100644 --- a/backend/src/types.ts +++ b/backend/src/types.ts @@ -27,7 +27,7 @@ export interface CodeChallenge { method: 'plain' | 'S256'; } -export interface Prompt { +export interface AuthPrompt { clientId: string; redirectUri: string; scopes: string[], diff --git a/backend/src/utils/index.ts b/backend/src/utils/index.ts index 1254a71..e912437 100644 --- a/backend/src/utils/index.ts +++ b/backend/src/utils/index.ts @@ -5,7 +5,6 @@ import _ from 'lodash'; import { ParamError } from '../errors'; import SessionData from '../session-data'; import type { SerializedSDK, Session } from '../types'; -import { sha256 } from './crypto'; export * from './crypto';