Skip to content

Commit

Permalink
린트 에러 수정, any 타입 제거 (#250)
Browse files Browse the repository at this point in the history
* 린트 에러 수정, any 타입 제거

* 테스트 수정

* 테스트 수정 2

* POST user에 빠진 조건 추가
  • Loading branch information
tirr-c authored Oct 3, 2023
1 parent 45e5285 commit 20f0180
Show file tree
Hide file tree
Showing 27 changed files with 352 additions and 322 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ module.exports = {
'no-sequences': 'error',
'no-constant-condition': ['error', { checkLoops: false }],
'@typescript-eslint/array-type': ['error', { default: 'generic' }],
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/no-explicit-any': 'warn',
},
};
32 changes: 16 additions & 16 deletions src/api/handlers/emails.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IMiddleware } from 'koa-router';
import z from 'zod';
import Config from '../../config';
import { EmailAddress } from '../../model/email_addresses';
import { EmailInUseError, InvalidEmailError, ResendLimitExeededError } from '../../model/errors';
Expand All @@ -7,23 +8,19 @@ import { sendEmail } from '../email';
import emailVerificationTemplate from '../templates/verification_email_template';

export function sendVerificationEmail(model: Model, config: Config): IMiddleware {
return async (ctx, next) => {
const body: any = ctx.request.body;

if (body == null || typeof body !== 'object') {
ctx.status = 400;
return;
}
const bodySchema = z.object({
emailLocal: z.string().trim().nonempty(),
emailDomain: z.string().trim().nonempty(),
});

let { emailLocal, emailDomain } = body;
emailLocal = emailLocal.trim();
emailDomain = emailDomain.trim();

if (!emailLocal || !emailDomain) {
return async (ctx, next) => {
const bodyResult = bodySchema.safeParse(ctx.request.body);
if (!bodyResult.success) {
ctx.status = 400;
return;
}

const { emailLocal, emailDomain } = bodyResult.data;
if (emailDomain !== 'snu.ac.kr') {
ctx.status = 400;
return;
Expand Down Expand Up @@ -82,15 +79,18 @@ export function sendVerificationEmail(model: Model, config: Config): IMiddleware
}

export function checkVerificationEmailToken(model: Model): IMiddleware {
return async (ctx, next) => {
const body: any = ctx.request.body;
const bodySchema = z.object({
token: z.string().nonempty(),
});

if (body == null || typeof body !== 'object') {
return async (ctx, next) => {
const bodyResult = bodySchema.safeParse(ctx.request.body);
if (!bodyResult.success) {
ctx.status = 400;
return;
}

const { token } = body;
const { token } = bodyResult.data;
let emailAddress: EmailAddress;
let result;

Expand Down
25 changes: 15 additions & 10 deletions src/api/handlers/groups.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IMiddleware } from 'koa-router';
import z from 'zod';
import { BadParameterError } from '../../model/errors';
import Model from '../../model/model';
import { User } from '../../model/users';
Expand Down Expand Up @@ -64,7 +65,7 @@ export function listMembers(model: Model): IMiddleware {
}

if (!owner) {
ctx.status = 401;
ctx.status = 403;
return;
}

Expand Down Expand Up @@ -107,7 +108,7 @@ export function listPending(model: Model): IMiddleware {
}

if (!owner) {
ctx.status = 401;
ctx.status = 403;
return;
}

Expand Down Expand Up @@ -160,24 +161,26 @@ export function applyGroup(model: Model): IMiddleware {
}

export function acceptGroup(model: Model): IMiddleware {
const bodySchema = z.number().array().nonempty();

return async (ctx, next) => {
if (typeof ctx.state.userIdx === 'number') {
const gid = Number(ctx.params.gid);

const body: any = ctx.request.body;
if (body === null || !(body instanceof Array) || !body.every(v => typeof v === 'number')) {
const bodyResult = bodySchema.safeParse(ctx.request.body);
if (!bodyResult.success) {
ctx.status = 400;
return;
}
const users: Array<number> = body;
const users = bodyResult.data;

try {
await model.pgDo(async tr => {
const group = await model.groups.getByIdx(tr, gid);

const owner = await model.groups.checkOwner(tr, group.idx, ctx.state.userIdx);
if (!owner) {
ctx.status = 401;
ctx.status = 403;
return;
}

Expand All @@ -202,24 +205,26 @@ export function acceptGroup(model: Model): IMiddleware {
}

export function rejectGroup(model: Model): IMiddleware {
const bodySchema = z.number().array().nonempty();

return async (ctx, next) => {
if (typeof ctx.state.userIdx === 'number') {
const gid = Number(ctx.params.gid);

const body: any = ctx.request.body;
if (body === null || !(body instanceof Array) || !body.every(v => typeof v === 'number')) {
const bodyResult = bodySchema.safeParse(ctx.request.body);
if (!bodyResult.success) {
ctx.status = 400;
return;
}
const users: Array<number> = body;
const users = bodyResult.data;

try {
await model.pgDo(async tr => {
const group = await model.groups.getByIdx(tr, gid);

const owner = await model.groups.checkOwner(tr, group.idx, ctx.state.userIdx);
if (!owner) {
ctx.status = 401;
ctx.status = 403;
return;
}

Expand Down
43 changes: 21 additions & 22 deletions src/api/handlers/login.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { IMiddleware } from 'koa-router';

import z from 'zod';
import Config from '../../config';
import { AuthorizationError, ControllableError, NoSuchEntryError } from '../../model/errors';
import Model from '../../model/model';
import { SignatureError, verifyPubkeyReq } from '../pubkey';

const loginBodySchema = z.object({
username: z.string(),
password: z.string(),
});

const legacyLoginBodySchema = z.object({
member_account: z.string(),
member_password: z.string(),
});

export function login(model: Model): IMiddleware {
return async ctx => {
const body: any = ctx.request.body;

if (!body || typeof body !== 'object') {
const bodyResult = loginBodySchema.safeParse(ctx.request.body);
if (!bodyResult.success) {
ctx.status = 400;
return;
}

const { username, password } = body;

const { username, password } = bodyResult.data;
try {
const userIdx = await model.pgDo(tr => model.users.authenticate(tr, username, password));
await ctx.state.setSession(userIdx);
Expand All @@ -36,14 +45,13 @@ export function login(model: Model): IMiddleware {

export function loginPAM(model: Model): IMiddleware {
return async ctx => {
const body: any = ctx.request.body;

if (!body || typeof body !== 'object') {
const bodyResult = loginBodySchema.safeParse(ctx.request.body);
if (!bodyResult.success) {
ctx.status = 400;
return;
}

const { username, password } = body;
const { username, password } = bodyResult.data;
try {
await model.pgDo(async tr => {
try {
Expand Down Expand Up @@ -83,24 +91,14 @@ export function loginPAM(model: Model): IMiddleware {

export function loginLegacy(model: Model, config: Config): IMiddleware {
return async ctx => {
const body: any = ctx.request.body;

if (!body || typeof body !== 'object') {
const bodyResult = legacyLoginBodySchema.safeParse(ctx.request.body);
if (!bodyResult.success) {
ctx.status = 400;
return;
}

const username = body.member_account;
const password = body.member_password;

if (!username || !password) {
// 200 means failure
ctx.status = 200;
return;
}

const { member_account: username, member_password: password } = bodyResult.data;
let userIdx: number;

try {
await model.pgDo(async tr => {
try {
Expand All @@ -112,6 +110,7 @@ export function loginLegacy(model: Model, config: Config): IMiddleware {
throw new AuthorizationError();
}
} catch (e) {
// 200 means failure
ctx.status = 200;
throw e;
}
Expand Down
Loading

0 comments on commit 20f0180

Please sign in to comment.