Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alex/3.1 fixes #698

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/classes/APIError.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable jsdoc/require-jsdoc */

import UUID from 'pure-uuid'
import * as constants from '../constants'

import i18next from './Localisation'
import StatusCode from './StatusCode'

Expand All @@ -17,7 +18,7 @@ export class APIError extends Error {
constructor (source) {
super()

this.id = new UUID(global.UUID_VERSION)
this.id = new UUID(constants.uuidVersion)
this.source = source
}

Expand Down
11 changes: 6 additions & 5 deletions src/classes/Authentication.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bcrypt from 'bcrypt'
import UUID from 'pure-uuid'
import * as constants from '../constants'
import {
User, Token, Client, Reset, db,
} from '../db'
Expand Down Expand Up @@ -68,8 +69,8 @@ class Authentication {
throw new GoneAPIError({ pointer: '/data/attributes/email' })
}

if (bcrypt.getRounds(user.password) > global.BCRYPT_ROUNDS_COUNT) {
const newRoundPassword = await bcrypt.hash(password, global.BCRYPT_ROUNDS_COUNT)
if (bcrypt.getRounds(user.password) > constants.bcryptRoundsCount) {
const newRoundPassword = await bcrypt.hash(password, constants.bcryptRoundsCount)
User.update({
password: newRoundPassword,
}, {
Expand Down Expand Up @@ -169,8 +170,8 @@ class Authentication {
throw new GoneAPIError({})
}

if (bcrypt.getRounds(client.secret) > global.BCRYPT_ROUNDS_COUNT) {
const newRoundSecret = await bcrypt.hash(secret, global.BCRYPT_ROUNDS_COUNT)
if (bcrypt.getRounds(client.secret) > constants.bcryptRoundsCount) {
const newRoundSecret = await bcrypt.hash(secret, constants.bcryptRoundsCount)
Client.update({
secret: newRoundSecret,
}, {
Expand Down Expand Up @@ -257,7 +258,7 @@ class Authentication {
}

let representedUser = undefined
if (new UUID(global.UUID_VERSION).parse(representing)) {
if (new UUID(constants.uuidVersion).parse(representing)) {
representedUser = await User.findOne({
where: {
id: representing,
Expand Down
3 changes: 2 additions & 1 deletion src/classes/WebSocket.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import UUID from 'pure-uuid'
import { URL } from 'url'
import { WebSocketServer } from 'ws'
import config from '../config'
import * as constants from '../constants'
import { User } from '../db'
import logger from '../logging'
import {
Expand Down Expand Up @@ -60,7 +61,7 @@ export default class WebSocket {

WebSocket.wss.on('connection', async (client, req) => {
client.req = req
client.clientId = new UUID(global.UUID_VERSION)
client.clientId = new UUID(constants.uuidVersion)
client.subscriptions = []


Expand Down
3 changes: 3 additions & 0 deletions src/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const websocketIdentifierRounds = 16
export const bcryptRoundsCount = 12
export const uuidVersion = 4
3 changes: 2 additions & 1 deletion src/db/Client.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bcrypt from 'bcrypt'

import * as constants from '../constants'
import Model, { column, table, type, validate } from './Model'
import { OAuthClientName, isURL } from '../helpers/Validators'

Expand Down Expand Up @@ -47,7 +48,7 @@ export default class Client extends Model {
if (!instance.changed('secret')) {
return
}
const hash = await bcrypt.hash(instance.get('secret'), global.BCRYPT_ROUNDS_COUNT)
const hash = await bcrypt.hash(instance.get('secret'), constants.bcryptRoundsCount)
instance.set('secret', hash)
}

Expand Down
3 changes: 2 additions & 1 deletion src/db/User.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bcrypt from 'bcrypt'
import * as constants from '../constants'
import Model, { column, table, validate, type } from './Model'
import { JSONObject } from '../helpers/Validators'

Expand Down Expand Up @@ -77,7 +78,7 @@ export default class User extends Model {
if (!instance.changed('password')) {
return
}
const hash = await bcrypt.hash(instance.get('password'), global.BCRYPT_ROUNDS_COUNT)
const hash = await bcrypt.hash(instance.get('password'), constants.bcryptRoundsCount)
instance.set('password', hash)
}

Expand Down
4 changes: 0 additions & 4 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ const app = new Koa()
querystring(app)


global.WEBSOCKET_IDENTIFIER_ROUNDS = 16
global.BCRYPT_ROUNDS_COUNT = 12
global.UUID_VERSION = 4

try {
npid.remove('api.pid')
const pid = npid.create('api.pid')
Expand Down
2 changes: 1 addition & 1 deletion src/routes/Rescues.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ export default class Rescues extends APIResource {
isFirstLimpet = entity.firstLimpet.userId === user.id
}

if (isAssigned || isFirstLimpet) {
if (isAssigned || isFirstLimpet || entity.status !== 'closed') {
return Permission.granted({ permissions: ['rescues.write.me'], connection: ctx })
}

Expand Down