com.infobip.kafkistry.webapp.security.auth.StaticUsersParser.kt Maven / Gradle / Ivy
The newest version!
package com.infobip.kafkistry.webapp.security.auth
import com.infobip.kafkistry.webapp.security.User
import com.infobip.kafkistry.webapp.security.UserRole
import org.springframework.security.crypto.bcrypt.BCrypt
object StaticUsersParser {
private val roles = listOf(UserRole.ADMIN, UserRole.USER, UserRole.READ_SERVICE).associateBy { it.name }
fun parseUsers(usersRaw: String): List = usersRaw.parseElements { parseUser(it) }
fun parseOwners(ownersRaw: String): List = ownersRaw.parseElements { parseOwnerGroup(it) }
private fun String.parseElements(elementParser: String.(Int) -> T): List {
return this
.split("\n", ";")
.filter { it.isNotBlank() }
.mapIndexed { index, element ->
element.trim().elementParser(index + 1)
}
}
/**
* username|password|name|surname|email|role|apiKeyToken
*/
private fun String.parseUser(index: Int): StaticUsers.StaticUser {
val parts = split("|")
if (parts.size != 7) {
throw IllegalArgumentException("Invalid ($index-th) user: expecting 7 parts delimited by |, got '$this'")
}
val (username, plainPassword, name, surname, email, roleName, token) = parts
val role =
roles[roleName] ?: throw IllegalArgumentException("Invalid ($index-th) user: unresolvable role '$roleName'")
return StaticUsers.StaticUser(
user = User(username, name, surname, email, role),
token = token.takeIf { it.isNotBlank() },
password = plainPassword.takeIf { it.isNotBlank() }?.let {
BCrypt.hashpw(plainPassword, BCrypt.gensalt())
},
passwordEncrypted = true,
)
}
/**
* owner|user1,user2,user3
*/
private fun String.parseOwnerGroup(index: Int): StaticUsers.OwnerUsers {
val parts = split("|", limit = 2)
if (parts.size != 2) {
throw IllegalArgumentException("Invalid ($index-th) owner group: expecting 2 parts delimited by |, got '$this'")
}
val owner = parts[0]
val users = parts[1].split(",").map { it.trim() }
return StaticUsers.OwnerUsers(owner, users)
}
private operator fun List.component6() = get(5)
private operator fun List.component7() = get(6)
}