Skip to content

Commit

Permalink
Update: hash password
Browse files Browse the repository at this point in the history
  • Loading branch information
vulcandragi committed Sep 4, 2024
1 parent d666afa commit 360d301
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 43 deletions.
4 changes: 3 additions & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ dependencies {
shadowImplementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
shadowImplementation("org.jetbrains.exposed:exposed-dao:$exposedVersion")
shadowImplementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion")
shadowImplementation("com.zaxxer:HikariCP:4.0.3")

shadowImplementation("io.insert-koin:koin-core:3.5.6")

shadowImplementation("org.bouncycastle:bcpkix-jdk18on:1.78.1")
shadowImplementation("org.springframework.security:spring-security-crypto:5.8.14")
}
6 changes: 4 additions & 2 deletions src/main/kotlin/com/fardragi/dragiutils/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.fardragi.dragiutils
import com.fardragi.dragiutils.auth.authModule
import com.fardragi.dragiutils.config.Config
import com.fardragi.dragiutils.config.DatabaseConfig
import com.fardragi.dragiutils.database.Database
import com.fardragi.dragiutils.database.DatabaseConnection
import com.fardragi.dragiutils.permission.permissionModule
import org.koin.core.module.dsl.createdAtStart
import org.koin.core.module.dsl.withOptions
Expand All @@ -15,5 +15,7 @@ val appModule = module {
createdAtStart()
}
single { DatabaseConfig(get()) }
single { Database(get()) }
single { DatabaseConnection(get()) } withOptions {
createdAtStart()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ package com.fardragi.dragiutils.auth.handlers
import com.fardragi.dragiutils.services.UserService
import cpw.mods.fml.common.eventhandler.SubscribeEvent
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class LoginHandler(private val userService: UserService) {
@SubscribeEvent
fun onPlayerJoin(event: PlayerLoggedInEvent) {
val userId = event.player.uniqueID.toString()
val userName = event.player.displayName

userService.getOrCreateUser(userId, userName)
CoroutineScope(Dispatchers.IO).launch {
userService.getOrCreateUser(userId, userName)
userService.setPassword(userId, "123456789")
}
}
}
30 changes: 0 additions & 30 deletions src/main/kotlin/com/fardragi/dragiutils/database/Database.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.fardragi.dragiutils.database

import com.fardragi.dragiutils.config.DatabaseConfig
import com.fardragi.dragiutils.database.tables.UsersTable
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils

class DatabaseConnection(config: DatabaseConfig) {
init {
Database.connect(
url = "jdbc:mariadb://${config.host}:${config.port}/${config.name}",
driver = "org.mariadb.jdbc.Driver",
user = config.user,
password = config.password
)

query {
SchemaUtils.createMissingTablesAndColumns(UsersTable)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import org.jetbrains.exposed.dao.id.IdTable
object UsersTable : IdTable<String>("users") {
override val id = varchar("id", 36).entityId()
val name = varchar("name", 50)
val hash = varchar("hash", 32).nullable()
val salt = varchar("salt", 32).nullable()
val hash = varchar("hash", 128).nullable()

override val primaryKey = PrimaryKey(id)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.fardragi.dragiutils.exceptions

class NotFoundException(message: String) : Exception(message) {
}
14 changes: 12 additions & 2 deletions src/main/kotlin/com/fardragi/dragiutils/models/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ import com.fardragi.dragiutils.database.tables.UsersTable
import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.dao.EntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder

class User(id: EntityID<String>) : Entity<String>(id) {
companion object : EntityClass<String, User>(UsersTable)

var name by UsersTable.name
private var hash by UsersTable.hash
private var salt by UsersTable.salt

val isRegistered get() = hash != null && salt != null
val isRegistered get() = hash != null

fun updatePassword(password: String) {
hash = createEncoder().encode(password)
}

private fun createEncoder(): Argon2PasswordEncoder {
return Argon2PasswordEncoder(
16, 32, 2, 1024 * 64, 10
)
}
}


22 changes: 17 additions & 5 deletions src/main/kotlin/com/fardragi/dragiutils/services/UserService.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
package com.fardragi.dragiutils.services

import com.fardragi.dragiutils.database.query
import com.fardragi.dragiutils.exceptions.NotFoundException
import com.fardragi.dragiutils.models.User

class UserService() {
fun getOrCreateUser(id: String, name: String): User {
var user = User.findById(id)
return query {
var user = User.findById(id)
if (user == null) {
user = User.new(id) {

if (user == null) {
user = User.new(id) {
this.name = name
this.name = name
}
}

user
}
}

return user
fun setPassword(id: String, password: String) {
query {
User.findByIdAndUpdate(id) { user ->
user.updatePassword(password)
} ?: throw NotFoundException("User not found")
}
}
}

0 comments on commit 360d301

Please sign in to comment.