-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #527 from YunhwanJeong/519-block-failed-login-atte…
…mpts Block user accounts if an incorrect password was entered 5 times
- Loading branch information
Showing
10 changed files
with
219 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { NotFound } from '@curveball/http-errors'; | ||
import { UserLoginActivityRecord } from 'knex/types/tables.js'; | ||
import db from '../../database.js'; | ||
import { User } from '../../types.js'; | ||
|
||
const MAX_FAILED_ATTEMPTS = 5; | ||
|
||
export function reachedMaxAttempts(attempts: number) { | ||
return attempts >= MAX_FAILED_ATTEMPTS; | ||
} | ||
|
||
async function getLoginActivity(user: User): Promise<UserLoginActivityRecord> { | ||
const loginActivity = await db<UserLoginActivityRecord>('user_login_activity') | ||
.where({ principal_id: user.id }) | ||
.first(); | ||
|
||
if (!loginActivity) throw new NotFound(`Login activity record for user with ID ${user.id} was not found.`); | ||
|
||
return loginActivity; | ||
} | ||
|
||
async function ensureUserLoginActivityRecord(user: User): Promise<void> { | ||
await db('user_login_activity') | ||
.insert({ | ||
principal_id: user.id, | ||
failed_login_attempts: 0, | ||
account_locked: 0, | ||
}) | ||
.onConflict('principal_id') | ||
.ignore(); | ||
} | ||
|
||
export async function incrementFailedLoginAttempts(user: User): Promise<number> { | ||
await ensureUserLoginActivityRecord(user); | ||
|
||
return await db.transaction(async (trx) => { | ||
|
||
const loginActivity = await trx('user_login_activity') | ||
.where({ principal_id: user.id }) | ||
.forUpdate() | ||
.first(); | ||
|
||
const newAttempts = loginActivity!.failed_login_attempts + 1; | ||
|
||
await trx('user_login_activity') | ||
.where({ principal_id: user.id }) | ||
.update({ | ||
failed_login_attempts: newAttempts, | ||
account_locked: newAttempts >= MAX_FAILED_ATTEMPTS ? 1 : 0, | ||
}); | ||
|
||
return newAttempts; | ||
}); | ||
} | ||
|
||
export async function resetFailedLoginAttempts(user: User): Promise<void> { | ||
await db('user_login_activity') | ||
.where({ principal_id: user.id }) | ||
.update({ | ||
failed_login_attempts: 0, | ||
account_locked: 0, | ||
}); | ||
} | ||
|
||
export async function isAccountLocked(user: User): Promise<boolean> { | ||
await ensureUserLoginActivityRecord(user); | ||
|
||
const loginActivity = await getLoginActivity(user); | ||
return !!loginActivity.account_locked; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/migrations/20240908210700_block_failed_login_attempts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Knex } from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
await knex.schema.createTable('user_login_activity', (table) => { | ||
table | ||
.integer('principal_id') | ||
.unsigned() | ||
.notNullable() | ||
.primary() | ||
.comment('Foreign key to the ‘principals’ table, representing the user'); | ||
table | ||
.foreign('principal_id') | ||
.references('id') | ||
.inTable('principals') | ||
.onDelete('CASCADE'); | ||
table | ||
.integer('failed_login_attempts') | ||
.unsigned() | ||
.defaultTo(0) | ||
.notNullable() | ||
.comment('Tracks the number of consecutive failed login attempts'); | ||
table | ||
.boolean('account_locked') | ||
.defaultTo(false) | ||
.notNullable() | ||
.comment( | ||
"Indicates if the user's account is currently locked due to suspicious activity, such as too many failed login attempts" | ||
); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex.schema.dropTable('user_login_activity'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
export * as appClient from './app-client/service.js'; | ||
export * as log from './log/service.js'; | ||
export * as loginActivity from './login/login-activity/service.js'; | ||
export * as mfaTotp from './mfa/totp/service.js'; | ||
export * as oauth2 from './oauth2/service.js'; | ||
export * as principal from './principal/service.js'; | ||
export * as principalIdentity from './principal-identity/service.js'; | ||
export * as principal from './principal/service.js'; | ||
export * as privilege from './privilege/service.js'; | ||
export * as resetPassword from './reset-password/service.js'; | ||
export * as user from './user/service.js'; | ||
export * as userAuthFactor from './user-auth-factor/service.js'; | ||
export * as user from './user/service.js'; | ||
export * as verificationToken from './verification-token/service.js'; |
Oops, something went wrong.