Refactor code

This commit is contained in:
Dominik Korsa 2021-01-27 09:23:27 +01:00
parent 43e86c70cd
commit 9b638c1c73
No known key found for this signature in database
GPG key ID: 546F986F71A6FE6E
11 changed files with 38 additions and 39 deletions

View file

@ -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,

View file

@ -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);
});
}

View file

@ -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);
});
}

View file

@ -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<void> {

View file

@ -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<CreateUserResult> {
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');

View file

@ -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<LoginResult> {
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, () => ({

View file

@ -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<PromptInfo> {
@ -17,7 +17,7 @@ export default class PromptInfoResolver implements ResolverInterface<PromptInfo>
@Arg('promptId') promptId: string,
@Ctx() { sessionData }: WebsiteAPIContext,
): Partial<PromptInfo> {
const prompt = sessionData.prompts.get(promptId);
const prompt = sessionData.authPrompts.get(promptId);
if (!prompt) throw new UnknownPromptError();
return {
id: promptId,

View file

@ -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<SetSymbolResult> {
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;

View file

@ -1,5 +1,5 @@
import type { Prompt } from './types';
import type { AuthPrompt } from './types';
export default class SessionData {
public prompts = new Map<string, Prompt>();
public authPrompts = new Map<string, AuthPrompt>();
}

View file

@ -27,7 +27,7 @@ export interface CodeChallenge {
method: 'plain' | 'S256';
}
export interface Prompt {
export interface AuthPrompt {
clientId: string;
redirectUri: string;
scopes: string[],

View file

@ -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';