Skip to content

Commit

Permalink
Add: base login and register
Browse files Browse the repository at this point in the history
  • Loading branch information
vulcandragi committed Sep 7, 2024
1 parent 8d1195f commit d7e775a
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/NyarukoLog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ object NyarukoLog {
fun info(message: String) {
logger.info(message)
}

fun error(message: String) {
logger.error(message)
}
}
4 changes: 3 additions & 1 deletion src/main/kotlin/com/fardragi/nyaruko/auth/AuthModule.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fardragi.nyaruko.auth

import com.fardragi.nyaruko.auth.commands.LoginCommand
import com.fardragi.nyaruko.auth.commands.RegisterCommand
import com.fardragi.nyaruko.auth.handlers.LoginHandler
import cpw.mods.fml.common.FMLCommonHandler
Expand All @@ -18,6 +19,7 @@ class AuthModule() : KoinScopeComponent {
loginHandler = LoginHandler(scope.get())
FMLCommonHandler.instance().bus().register(loginHandler)

commandManager.registerCommand(RegisterCommand())
commandManager.registerCommand(RegisterCommand(scope.get()))
commandManager.registerCommand(LoginCommand(scope.get()))
}
}
44 changes: 44 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/auth/commands/LoginCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.fardragi.nyaruko.auth.commands

import com.fardragi.nyaruko.auth.messages.LoginCheckFailMessage
import com.fardragi.nyaruko.auth.messages.LoginCheckSuccessMessage
import com.fardragi.nyaruko.enums.PermissionLevel
import com.fardragi.nyaruko.services.UserService
import com.fardragi.nyaruko.shared.commands.NyarukoCommandBase
import com.fardragi.nyaruko.shared.messages.FailCommandMessage
import net.minecraft.command.ICommandSender
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.util.ChatComponentText

class LoginCommand(private val userService: UserService) : NyarukoCommandBase() {
override fun getCommandName(): String {
return "login"
}

override fun getCommandUsage(sender: ICommandSender?): String {
return "/login <password>"
}

override suspend fun processCommandPlayer(player: EntityPlayerMP, args: Array<out String>): ChatComponentText {
if (args.isEmpty()) {
return FailCommandMessage.create()
}

val userId = player.uniqueID.toString()
val password = args[0]

if (!userService.checkPassword(userId, password)) {
return LoginCheckFailMessage.create()
}

return LoginCheckSuccessMessage.create()
}

override fun getRequiredPermissionLevel(): Int {
return PermissionLevel.True.level
}

override fun canCommandSenderUseCommand(sender: ICommandSender?): Boolean {
return true
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.fardragi.nyaruko.auth.commands

import com.fardragi.nyaruko.auth.messages.AlreadyRegisteredMessage
import com.fardragi.nyaruko.auth.messages.LoginMessage
import com.fardragi.nyaruko.enums.PermissionLevel
import net.minecraft.command.CommandBase
import com.fardragi.nyaruko.services.UserService
import com.fardragi.nyaruko.shared.commands.NyarukoCommandBase
import com.fardragi.nyaruko.shared.messages.FailCommandMessage
import net.minecraft.command.ICommandSender
import net.minecraft.server.MinecraftServer
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.util.ChatComponentText

class RegisterCommand : CommandBase() {
class RegisterCommand(private val userService: UserService) : NyarukoCommandBase() {
override fun getCommandName(): String {
return "register"
}
Expand All @@ -15,9 +19,28 @@ class RegisterCommand : CommandBase() {
return "/register <password> <repeat password>"
}

override fun processCommand(sender: ICommandSender?, args: Array<out String>?) {
val text = ChatComponentText("foda")
MinecraftServer.getServer().configurationManager.sendChatMsg(text)
override suspend fun processCommandPlayer(player: EntityPlayerMP, args: Array<out String>): ChatComponentText {
if (args.isEmpty() || args.size < 2) {
return FailCommandMessage.create()
}

val userId = player.uniqueID.toString()
val password = args[0]
val repeatPassword = args[1]

val user = userService.getById(userId)

if (user.isRegistered) {
return AlreadyRegisteredMessage.create()
}

if (password != repeatPassword) {
FailCommandMessage.create("Password not match")
}

userService.setPassword(userId, repeatPassword)

return LoginMessage.create()
}

override fun getRequiredPermissionLevel(): Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fardragi.nyaruko.auth.messages

import net.minecraft.util.ChatComponentText
import net.minecraft.util.ChatStyle
import net.minecraft.util.EnumChatFormatting

object AlreadyRegisteredMessage {
fun create(): ChatComponentText {
val text = ChatComponentText("User already registered")
text.chatStyle = ChatStyle().setColor(EnumChatFormatting.YELLOW)

return text
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fardragi.nyaruko.auth.messages

import net.minecraft.util.ChatComponentText
import net.minecraft.util.ChatStyle
import net.minecraft.util.EnumChatFormatting

object LoginCheckFailMessage {
fun create(): ChatComponentText {
val text = ChatComponentText("Password incorrect")
text.chatStyle = ChatStyle().setColor(EnumChatFormatting.RED)

return text
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fardragi.nyaruko.auth.messages

import net.minecraft.util.ChatComponentText
import net.minecraft.util.ChatStyle
import net.minecraft.util.EnumChatFormatting

object LoginCheckSuccessMessage {
fun create(): ChatComponentText {
val text = ChatComponentText("Login with success")
text.chatStyle = ChatStyle().setColor(EnumChatFormatting.GREEN)

return text
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import net.minecraft.util.EnumChatFormatting

object RegisterMessage {
fun create(): ChatComponentText {
val text = ChatComponentText("use /register <password> <password>");
val text = ChatComponentText("use /register <password> <repeat password>")
text.chatStyle = ChatStyle().setColor(EnumChatFormatting.YELLOW)

return text
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package com.fardragi.nyaruko.exceptions

class NotFoundException(message: String) : Exception(message) {
}
class NotFoundException(className: String?, id: String?) : Exception("$className not found with id: $id")
4 changes: 4 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/models/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class User(id: EntityID<String>) : Entity<String>(id) {
hash = createEncoder().encode(password)
}

fun checkPassword(password: String): Boolean {
return createEncoder().matches(password, hash)
}

private fun createEncoder(): Argon2PasswordEncoder {
return Argon2PasswordEncoder(
16, 32, 2, 1024 * 64, 10
Expand Down
17 changes: 16 additions & 1 deletion src/main/kotlin/com/fardragi/nyaruko/services/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class UserService() {
suspend fun getOrCreateUser(id: String, name: String): User {
return query {
var user = User.findById(id)

if (user == null) {
user = User.new(id) {

Expand All @@ -23,7 +24,21 @@ class UserService() {
query {
User.findByIdAndUpdate(id) { user ->
user.updatePassword(password)
} ?: throw NotFoundException("User not found")
} ?: throw NotFoundException(User::class.simpleName, id)
}
}

suspend fun checkPassword(id: String, password: String): Boolean {
return query {
val user = User.findById(id) ?: throw NotFoundException(User::class.simpleName, id)

user.checkPassword(password)
}
}

suspend fun getById(id: String): User {
return query {
User.findById(id) ?: throw NotFoundException(User::class.simpleName, id)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
package com.fardragi.nyaruko.shared.commands

import com.fardragi.nyaruko.NyarukoLog
import com.fardragi.nyaruko.shared.messages.FailCommandMessage
import com.fardragi.nyaruko.shared.messages.NotImplementedCommandMessage
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.minecraft.command.CommandBase
import net.minecraft.command.ICommandSender
import net.minecraft.command.server.CommandBlockLogic
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.util.ChatComponentText

class NyarukoCommandBase : CommandBase() {
override fun getCommandName(): String {
TODO("Not yet implemented")
abstract class NyarukoCommandBase : CommandBase() {
override fun processCommand(sender: ICommandSender, args: Array<out String>) {
CoroutineScope(Dispatchers.Default).launch {
try {
val message = when (sender) {
is EntityPlayerMP -> processCommandPlayer(sender, args)
is CommandBlockLogic -> processCommandBlock(sender, args)
else -> processCommandConsole(sender, args)
}

sender.addChatMessage(message)
} catch (e: Exception) {
e.message?.let { NyarukoLog.error(it) }
sender.addChatMessage(FailCommandMessage.create())
}
}
}

open suspend fun processCommandPlayer(player: EntityPlayerMP, args: Array<out String>): ChatComponentText {
return NotImplementedCommandMessage.create()
}

override fun getCommandUsage(sender: ICommandSender?): String {
TODO("Not yet implemented")
open suspend fun processCommandBlock(commandBlock: CommandBlockLogic, args: Array<out String>): ChatComponentText {
return NotImplementedCommandMessage.create()
}

override fun processCommand(sender: ICommandSender?, args: Array<out String>?) {
TODO("Not yet implemented")
open suspend fun processCommandConsole(sender: ICommandSender, args: Array<out String>): ChatComponentText {
return NotImplementedCommandMessage.create()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fardragi.nyaruko.shared.messages

import net.minecraft.util.ChatComponentText
import net.minecraft.util.ChatStyle
import net.minecraft.util.EnumChatFormatting

object FailCommandMessage {
fun create(message: String = ""): ChatComponentText {
val text = ChatComponentText("Fail to execute command: $message")
text.chatStyle = ChatStyle().setColor(EnumChatFormatting.RED)

return text
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.fardragi.nyaruko.shared.messages

import net.minecraft.util.ChatComponentText
import net.minecraft.util.ChatStyle
import net.minecraft.util.EnumChatFormatting

object NotImplementedCommandMessage {
fun create(): ChatComponentText {
val text = ChatComponentText("Command not implemented")
text.chatStyle = ChatStyle().setColor(EnumChatFormatting.RED)

return text
}

}

0 comments on commit d7e775a

Please sign in to comment.