Skip to content

Commit

Permalink
Removing the DatabaseManager class after all these years feels so good
Browse files Browse the repository at this point in the history
  • Loading branch information
NovaFox161 committed Dec 7, 2024
1 parent 08e7ee5 commit 3d83f03
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 107 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.dreamexposure.discal.server
import jakarta.annotation.PreDestroy
import org.dreamexposure.discal.Application
import org.dreamexposure.discal.core.config.Config
import org.dreamexposure.discal.core.database.DatabaseManager
import org.dreamexposure.discal.core.logger.LOGGER
import org.dreamexposure.discal.core.utils.GlobalVal.DEFAULT
import org.dreamexposure.discal.core.utils.GlobalVal.STATUS
Expand All @@ -18,7 +17,6 @@ class DisCalServer {
@PreDestroy
fun onShutdown() {
LOGGER.info(STATUS, "API shutting down.")
DatabaseManager.disconnectFromMySQL()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import reactor.core.publisher.Mono

@RestController
@RequestMapping("/v2/account/key")
class KeyRequestEndpoint {
class KeyRequestEndpoint(
private val authentication: Authentication,
) {
@SecurityRequirement(disableSecurity = true, scopes = [])
@PostMapping("/readonly/get", produces = ["application/json"])
fun getReadOnlyKey(swe: ServerWebExchange, response: ServerHttpResponse): Mono<String> {
return Authentication.authenticate(swe).flatMap { authState ->
return authentication.authenticate(swe).flatMap { authState ->
if (!authState.success) {
response.rawStatusCode = authState.status
return@flatMap Mono.just(GlobalVal.JSON_FORMAT.encodeToString(authState))
Expand All @@ -34,7 +36,7 @@ class KeyRequestEndpoint {
//Handle request
val key = KeyGenerator.csRandomAlphaNumericString(64)

Authentication.saveReadOnlyKey(key)
authentication.saveReadOnlyKey(key)

//Return key
val json = JSONObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import reactor.core.publisher.Mono

@RestController
@RequestMapping("/v2/account")
class LoginEndpoint {
class LoginEndpoint(
private val authentication: Authentication,
) {
@SecurityRequirement(disableSecurity = true, scopes = [])
@PostMapping("/login", produces = ["application/json"])
fun loginForKey(swe: ServerWebExchange, response: ServerHttpResponse): Mono<String> {
return Authentication.authenticate(swe).flatMap { authState ->
return authentication.authenticate(swe).flatMap { authState ->
if (!authState.success) {
response.rawStatusCode = authState.status
return@flatMap Mono.just(GlobalVal.JSON_FORMAT.encodeToString(authState))
Expand All @@ -34,7 +36,7 @@ class LoginEndpoint {
//Handle request
val key = KeyGenerator.csRandomAlphaNumericString(64)

Authentication.saveTempKey(key)
authentication.saveTempKey(key)

//Return key
val json = JSONObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import reactor.core.publisher.Mono

@RestController
@RequestMapping("/v2/account")
class LogoutEndpoint {
class LogoutEndpoint(
private val authentication: Authentication,
) {
@SecurityRequirement(disableSecurity = true, scopes = [])
@GetMapping("/logout", produces = ["application/json"])
fun logoutOfAccount(swe: ServerWebExchange, response: ServerHttpResponse): Mono<String> {
return Authentication.authenticate(swe).flatMap { authState ->
return authentication.authenticate(swe).flatMap { authState ->
if (!authState.success) {
response.rawStatusCode = authState.status
return@flatMap Mono.just(GlobalVal.JSON_FORMAT.encodeToString(authState))
Expand All @@ -30,7 +32,7 @@ class LogoutEndpoint {
}

//Handle request
Authentication.removeTempKey(authState.keyUsed)
authentication.removeTempKey(authState.keyUsed)

response.rawStatusCode = GlobalVal.STATUS_SUCCESS
return@flatMap responseMessage("Logged out")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ import reactor.core.publisher.Mono
@Deprecated("Prefer using v3 implementation, this needs to go at some point")
class GetWebGuildEndpoint(
private val webGuildService: WebGuildService,
private val authentication: Authentication,
) {
@PostMapping(value = ["/get"], produces = ["application/json"])
@SecurityRequirement(disableSecurity = true, scopes = [])
fun getSettings(swe: ServerWebExchange, response: ServerHttpResponse, @RequestBody rBody: String): Mono<String> {
return Authentication.authenticate(swe).flatMap { authState ->
return authentication.authenticate(swe).flatMap { authState ->
if (!authState.success) {
response.rawStatusCode = authState.status
return@flatMap Mono.just(GlobalVal.JSON_FORMAT.encodeToString(authState))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import java.util.*

@RestController
@RequestMapping("/v2/guild")
class UpdateWebGuildEndpoint(val client: DiscordClient) {
class UpdateWebGuildEndpoint(
private val client: DiscordClient,
private val authentication: Authentication,
) {
@PostMapping(value = ["/update"], produces = ["application/json"])
@SecurityRequirement(disableSecurity = true, scopes = [])
fun updateGuild(swe: ServerWebExchange, response: ServerHttpResponse, @RequestBody rBody: String): Mono<String> {
return Authentication.authenticate(swe).flatMap { authState ->
return authentication.authenticate(swe).flatMap { authState ->
if (!authState.success) {
response.rawStatusCode = authState.status
return@flatMap Mono.just(GlobalVal.JSON_FORMAT.encodeToString(authState))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import reactor.core.publisher.Mono
@RequestMapping("/v2/guild/settings")
class GetGuildSettingsEndpoint(
private val settingsService: GuildSettingsService,
private val authentication: Authentication,
) {

@PostMapping(value = ["/get"], produces = ["application/json"])
@SecurityRequirement(disableSecurity = true, scopes = [])
fun getSettings(swe: ServerWebExchange, response: ServerHttpResponse, @RequestBody rBody: String): Mono<String> {
return Authentication.authenticate(swe).flatMap<String?> { authState ->
return authentication.authenticate(swe).flatMap<String?> { authState ->
if (!authState.success) {
response.rawStatusCode = authState.status
return@flatMap Mono.just(GlobalVal.JSON_FORMAT.encodeToString(authState))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import java.util.*
@RequestMapping("/v2/guild/settings")
class UpdateGuildSettingsEndpoint(
private val settingsService: GuildSettingsService,
private val authentication: Authentication,
) {
@PostMapping(value = ["/update"], produces = ["application/json"])
@SecurityRequirement(disableSecurity = true, scopes = [])
fun updateSettings(swe: ServerWebExchange, response: ServerHttpResponse, @RequestBody rBody: String): Mono<String> {
return Authentication.authenticate(swe).flatMap { authState ->
return authentication.authenticate(swe).flatMap { authState ->
if (!authState.success) {
response.rawStatusCode = authState.status
return@flatMap Mono.just(GlobalVal.JSON_FORMAT.encodeToString(authState))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ import reactor.core.publisher.Mono

@RestController
@RequestMapping("/v2/status")
class GetStatusEndpoint(val networkManager: NetworkManager) {
class GetStatusEndpoint(
private val networkManager: NetworkManager,
private val authentication: Authentication,
) {

@PostMapping("/get", produces = ["application/json"])
@SecurityRequirement(disableSecurity = true, scopes = [])
fun getStatus(swe: ServerWebExchange, response: ServerHttpResponse): Mono<String> {
return Authentication.authenticate(swe).map { authState ->
return authentication.authenticate(swe).map { authState ->
if (!authState.success) {
response.rawStatusCode = authState.status
return@map GlobalVal.JSON_FORMAT.encodeToString(authState)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.dreamexposure.discal.server.utils

import kotlinx.coroutines.reactor.mono
import org.dreamexposure.discal.core.business.ApiKeyService
import org.dreamexposure.discal.core.config.Config
import org.dreamexposure.discal.core.database.DatabaseManager
import org.dreamexposure.discal.core.logger.LOGGER
import org.dreamexposure.discal.core.`object`.web.AuthenticationState
import org.dreamexposure.discal.core.utils.GlobalVal
import org.dreamexposure.discal.core.utils.GlobalVal.DEFAULT
import org.springframework.http.HttpMethod
import org.springframework.stereotype.Component
import org.springframework.web.server.ServerWebExchange
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
Expand All @@ -15,7 +17,11 @@ import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap

//TODO: Use DI and make this an application runner as well
object Authentication {
@Deprecated("Prefer to use new security requirement annotation implementation")
@Component
class Authentication(
private val apiKeyService: ApiKeyService,
) {
init {
Flux.interval(Duration.ofHours(1))
.map { handleExpiredKeys() }
Expand Down Expand Up @@ -76,7 +82,7 @@ object Authentication {
)
}
else -> { //Check if key is in database...
DatabaseManager.getAPIAccount(authKey).map { acc ->
mono { apiKeyService.getKey(authKey) }.map { acc ->
if (!acc.blocked) {
return@map AuthenticationState(true)
.status(GlobalVal.STATUS_SUCCESS)
Expand Down

0 comments on commit 3d83f03

Please sign in to comment.