better code: use that const

Massimo Melina committed Jun 13, 2022 at 16:55 UTC 2b8f1364c3d3e98246db62d335e1003fe01442f8
5 files changed +23 -12
server/src/adminApis.ts
+11 -2
@@ -3,7 +3,16 @@
3 import { ApiError, ApiHandlers } from './apiMiddleware'
4 import { defineConfig, getWholeConfig, setConfig } from './config'
5 import { getStatus, getUrls, httpsPortCfg, portCfg } from './listen'
6 -import { API_VERSION, BUILD_TIMESTAMP, COMPATIBLE_API_VERSION, FORBIDDEN, HFS_STARTED, IS_WINDOWS, VERSION } from './const'
6 +import {
7 + API_VERSION,
8 + BUILD_TIMESTAMP,
9 + COMPATIBLE_API_VERSION,
10 + FORBIDDEN,
11 + HFS_STARTED,
12 + IS_WINDOWS,
13 + UNAUTHORIZED,
14 + VERSION
15 +} from './const'
16 import vfsApis from './api.vfs'
17 import accountsApis from './api.accounts'
18 import pluginsApis from './api.plugins'
@@ -118,7 +127,7 @@ for (const k in adminApis) {
127 const was = adminApis[k]
128 adminApis[k] = (params, ctx) =>
129 ctxAdminAccess(ctx) ? was(params, ctx)
121 - : new ApiError(401)
130 + : new ApiError(UNAUTHORIZED)
131 }
132
133 export const localhostAdmin = defineConfig('localhost_admin', true)
server/src/api.auth.ts
+5 -5
@@ -4,7 +4,7 @@ import { getAccount, getCurrentUsername } from './perm'
4 import { verifyPassword } from './crypt'
5 import { ApiError, ApiHandler } from './apiMiddleware'
6 import { SRPParameters, SRPRoutines, SRPServerSession, SRPServerSessionStep1 } from 'tssrp6a'
7 -import { ADMIN_URI, SESSION_DURATION } from './const'
7 +import { ADMIN_URI, SESSION_DURATION, UNAUTHORIZED } from './const'
8 import { randomId } from './misc'
9 import Koa from 'koa'
10 import { changeSrpHelper, changePasswordHelper } from './api.helpers'
@@ -43,11 +43,11 @@ export const login: ApiHandler = async ({ username, password }, ctx) => {
43 username = username.toLocaleLowerCase()
44 const acc = getAccount(username)
45 if (!acc)
46 - return new ApiError(401)
46 + return new ApiError(UNAUTHORIZED)
47 if (!acc.hashed_password)
48 return new ApiError(406)
49 if (!await verifyPassword(acc.hashed_password, password))
50 - return new ApiError(401)
50 + return new ApiError(UNAUTHORIZED)
51 if (!ctx.session)
52 return new ApiError(500)
53 loggedIn(ctx, username)
@@ -62,7 +62,7 @@ export const loginSrp1: ApiHandler = async ({ username }, ctx) => {
62 if (!ctx.session)
63 return new ApiError(500)
64 if (!account) // TODO simulate fake account to prevent knowing valid usernames
65 - return new ApiError(401)
65 + return new ApiError(UNAUTHORIZED)
66 if (!account.srp)
67 return new ApiError(406) // unacceptable
68 const [salt, verifier] = account.srp.split('|')
@@ -92,7 +92,7 @@ export const loginSrp2: ApiHandler = async ({ pubKey, proof }, ctx) => {
92 }
93 }
94 catch(e) {
95 - return new ApiError(401, String(e))
95 + return new ApiError(UNAUTHORIZED, String(e))
96 }
97 finally {
98 delete ongoingLogins[sid]
server/src/api.helpers.ts
+3 -2
@@ -2,12 +2,13 @@
2
3 import { Account, allowClearTextLogin, saveSrpInfo, updateAccount } from './perm'
4 import { ApiError } from './apiMiddleware'
5 +import { UNAUTHORIZED } from './const'
6
7 export async function changePasswordHelper(account: Account | undefined, newPassword: string) {
8 if (!newPassword) // clear text version
9 return Error('missing parameters')
10 if (!account)
10 - return new ApiError(401)
11 + return new ApiError(UNAUTHORIZED)
12 await updateAccount(account, account => {
13 account.password = newPassword
14 })
@@ -20,7 +21,7 @@ export async function changeSrpHelper(account: Account | undefined, salt: string
21 if (!salt || !verifier)
22 return Error('missing parameters')
23 if (!account)
23 - return new ApiError(401)
24 + return new ApiError(UNAUTHORIZED)
25 await updateAccount(account, account => {
26 saveSrpInfo(account, salt, verifier)
27 delete account.hashed_password // remove leftovers
server/src/apiMiddleware.ts
+2 -1
@@ -6,6 +6,7 @@ import createSSE from './sse'
6 import { Readable } from 'stream'
7 import { asyncGeneratorToReadable, objSameKeys, onOff, tryJson } from './misc'
8 import events from './events'
9 +import { UNAUTHORIZED } from './const'
10
11 export class ApiError extends Error {
12 constructor(public status:number, message?:string | Error) {
@@ -27,7 +28,7 @@ export function apiMiddleware(apis: ApiHandlers) : Koa.Middleware {
28 }
29 const csrf = ctx.cookies.get('csrf')
30 // we don't rely on SameSite cookie option because it's https-only
30 - let res = csrf && csrf !== params.csrf ? new ApiError(401, 'csrf')
31 + let res = csrf && csrf !== params.csrf ? new ApiError(UNAUTHORIZED, 'csrf')
32 : await apis[ctx.path](params || {}, ctx)
33 if (isAsyncGenerator(res))
34 res = asyncGeneratorToReadable(res)
server/src/vfs.ts
+2 -2
@@ -8,7 +8,7 @@ import Koa from 'koa'
8 import glob from 'fast-glob'
9 import _ from 'lodash'
10 import { defineConfig, setConfig } from './config'
11 -import { FORBIDDEN, IS_WINDOWS } from './const'
11 +import { FORBIDDEN, IS_WINDOWS, UNAUTHORIZED } from './const'
12 import events from './events'
13 import { getCurrentUsernameExpanded } from './perm'
14 import { with_ } from './misc'
@@ -233,7 +233,7 @@ function matchWho(who: Who, ctx: Koa.Context) {
233 }
234
235 export function cantReadStatusCode(node: VfsNode) {
236 - return node.can_read === false ? FORBIDDEN : 401
236 + return node.can_read === false ? FORBIDDEN : UNAUTHORIZED
237 }
238
239 events.on('accountRenamed', (from, to) => {