-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6adf478
commit 58786b7
Showing
8 changed files
with
142 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# imagery | ||
|
||
A WIP ShareX file server. | ||
A WIP ShareX file server. | ||
|
||
Overcomplicating? Never heard of her. |
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
68 changes: 68 additions & 0 deletions
68
app/src/main/kotlin/dev/mizule/imagery/app/auth/AuthHandler.kt
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,68 @@ | ||
package dev.mizule.imagery.app.auth | ||
|
||
import org.spongepowered.configurate.jackson.JacksonConfigurationLoader | ||
import org.spongepowered.configurate.kotlin.extensions.get | ||
import org.spongepowered.configurate.kotlin.objectMapperFactory | ||
import java.security.SecureRandom | ||
import java.util.Base64 | ||
import java.util.HashMap | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.exists | ||
|
||
class AuthHandler(userConfigPath: String) { | ||
|
||
private val usersMap: MutableMap<String, User> = mutableMapOf() | ||
|
||
val usersPath = Path(userConfigPath) | ||
val usersLoader = JacksonConfigurationLoader.builder() | ||
.path(usersPath) | ||
.defaultOptions { options -> | ||
options.shouldCopyDefaults(true) | ||
options.serializers { builder -> | ||
builder.registerAnnotatedObjects(objectMapperFactory()) | ||
} | ||
} | ||
.build() | ||
|
||
val usersNode = usersLoader.load() | ||
val usersConfig = requireNotNull(usersNode.get<UserConfig>()) { | ||
"Could not read user configuration" | ||
} | ||
|
||
init { | ||
if (!usersPath.exists()) { | ||
usersNode.set(usersConfig) // update the backing node to add defaults | ||
usersLoader.save(usersNode) | ||
} | ||
loadUsers() | ||
} | ||
|
||
private fun loadUsers() { | ||
usersConfig.users.forEach { | ||
this.usersMap[it.username] = User(it.username, it.token) | ||
} | ||
} | ||
|
||
fun createUser(name: String): User { | ||
val secret = ByteArray(48) | ||
SecureRandom().nextBytes(secret) | ||
return createUser(name, Base64.getEncoder().encodeToString(secret)); | ||
} | ||
|
||
fun isAuthorized(token: String): Boolean { | ||
return usersMap.values.any { it.token == token } | ||
} | ||
|
||
fun getUserByToken(token: String): User? { | ||
return usersMap.values.find { it.token == token } | ||
} | ||
|
||
fun createUser(name: String, token: String): User { | ||
val user = User(name, token) | ||
this.usersMap[name] to user | ||
this.usersConfig.users.add(user) | ||
usersNode.set(usersConfig) | ||
usersLoader.save(usersNode) | ||
return user | ||
} | ||
} |
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,11 @@ | ||
package dev.mizule.imagery.app.auth | ||
|
||
import org.spongepowered.configurate.objectmapping.ConfigSerializable | ||
|
||
@ConfigSerializable | ||
data class User( | ||
|
||
val username: String, | ||
|
||
val token: String, | ||
) |
10 changes: 10 additions & 0 deletions
10
app/src/main/kotlin/dev/mizule/imagery/app/auth/UserConfig.kt
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,10 @@ | ||
package dev.mizule.imagery.app.auth | ||
|
||
import org.spongepowered.configurate.objectmapping.ConfigSerializable | ||
import org.spongepowered.configurate.objectmapping.meta.Comment | ||
|
||
@ConfigSerializable | ||
data class UserConfig( | ||
|
||
val users: MutableList<User> = mutableListOf() | ||
) |
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,9 @@ | ||
package dev.mizule.imagery.app.model | ||
|
||
import io.javalin.security.RouteRole | ||
|
||
enum class Roles : RouteRole { | ||
|
||
PUBLIC, | ||
PRIVATE | ||
} |