-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I redid the new auth system I added a little while back, this time it's not spaghetti and should be maintainable
- Loading branch information
1 parent
adaea8d
commit 2d475e0
Showing
49 changed files
with
332 additions
and
395 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
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
31 changes: 31 additions & 0 deletions
31
cam/src/main/kotlin/org/dreamexposure/discal/cam/controllers/v1/SecurityController.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,31 @@ | ||
package org.dreamexposure.discal.cam.controllers.v1 | ||
|
||
import org.dreamexposure.discal.cam.business.SecurityService | ||
import org.dreamexposure.discal.core.annotations.SecurityRequirement | ||
import org.dreamexposure.discal.core.`object`.new.security.Scope.INTERNAL_CAM_VALIDATE_TOKEN | ||
import org.dreamexposure.discal.core.`object`.new.security.TokenType.INTERNAL | ||
import org.dreamexposure.discal.core.`object`.rest.v1.security.ValidateRequest | ||
import org.dreamexposure.discal.core.`object`.rest.v1.security.ValidateResponse | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/v1/security") | ||
class SecurityController( | ||
private val securityService: SecurityService, | ||
) { | ||
@SecurityRequirement(schemas = [INTERNAL], scopes = [INTERNAL_CAM_VALIDATE_TOKEN]) | ||
@PostMapping("/validate", produces = ["application/json"]) | ||
suspend fun validate(@RequestBody request: ValidateRequest): ValidateResponse { | ||
val result = securityService.authenticateAndAuthorizeToken( | ||
request.token, | ||
request.schemas, | ||
request.scopes, | ||
) | ||
|
||
return ValidateResponse(result.first == HttpStatus.OK, result.first, result.second) | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
client/src/main/kotlin/org/dreamexposure/discal/client/business/cronjob/HeartbeatCronJob.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,59 @@ | ||
package org.dreamexposure.discal.client.business.cronjob | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import discord4j.core.GatewayDiscordClient | ||
import kotlinx.coroutines.reactor.awaitSingle | ||
import kotlinx.coroutines.reactor.mono | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import okhttp3.Response | ||
import org.dreamexposure.discal.core.config.Config | ||
import org.dreamexposure.discal.core.extensions.asSeconds | ||
import org.dreamexposure.discal.core.logger.LOGGER | ||
import org.dreamexposure.discal.core.`object`.network.discal.BotInstanceData | ||
import org.dreamexposure.discal.core.`object`.rest.HeartbeatRequest | ||
import org.dreamexposure.discal.core.`object`.rest.HeartbeatType | ||
import org.dreamexposure.discal.core.utils.GlobalVal | ||
import org.springframework.boot.ApplicationArguments | ||
import org.springframework.boot.ApplicationRunner | ||
import org.springframework.stereotype.Component | ||
import reactor.core.publisher.Flux | ||
import reactor.core.publisher.Mono | ||
import reactor.core.scheduler.Schedulers | ||
|
||
@Component | ||
class HeartbeatCronJob( | ||
private val discordClient: GatewayDiscordClient, | ||
private val httpClient: OkHttpClient, | ||
private val objectMapper: ObjectMapper, | ||
): ApplicationRunner { | ||
private final val apiUrl = Config.URL_API.getString() | ||
|
||
override fun run(args: ApplicationArguments?) { | ||
Flux.interval(Config.HEARTBEAT_INTERVAL.getLong().asSeconds()) | ||
.flatMap { heartbeat() } | ||
.doOnError { LOGGER.error(GlobalVal.DEFAULT, "[Heartbeat] Failed to heartbeat", it) } | ||
.onErrorResume { Mono.empty() } | ||
.subscribe() | ||
} | ||
|
||
private fun heartbeat() = mono { | ||
val data = BotInstanceData.load(discordClient).awaitSingle() | ||
|
||
val requestBody = HeartbeatRequest(HeartbeatType.BOT, botInstanceData = data) | ||
val request = Request.Builder() | ||
.url("$apiUrl/v3/status/heartbeat") | ||
.post(objectMapper.writeValueAsString(requestBody).toRequestBody(GlobalVal.JSON)) | ||
.header("Authorization", "Int ${Config.SECRET_DISCAL_API_KEY.getString()}") | ||
.header("Content-Type", "application/json") | ||
.build() | ||
|
||
Mono.fromCallable(httpClient.newCall(request)::execute) | ||
.map(Response::close) | ||
.subscribeOn(Schedulers.boundedElastic()) | ||
.doOnError { LOGGER.error(GlobalVal.DEFAULT, "[Heartbeat] Failed to heartbeat", it) } | ||
.onErrorResume { Mono.empty() } | ||
.subscribe() | ||
} | ||
} |
60 changes: 0 additions & 60 deletions
60
client/src/main/kotlin/org/dreamexposure/discal/client/service/HeartbeatService.kt
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
core/src/main/kotlin/org/dreamexposure/discal/core/annotations/Authentication.kt
This file was deleted.
Oops, something went wrong.
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
15 changes: 0 additions & 15 deletions
15
core/src/main/kotlin/org/dreamexposure/discal/core/enums/network/DisCalRealm.kt
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
core/src/main/kotlin/org/dreamexposure/discal/core/enums/network/PubSubReason.kt
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
core/src/main/kotlin/org/dreamexposure/discal/core/exceptions/TeaPotException.kt
This file was deleted.
Oops, something went wrong.
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
10 changes: 10 additions & 0 deletions
10
.../src/main/kotlin/org/dreamexposure/discal/core/object/rest/v1/security/ValidateRequest.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 org.dreamexposure.discal.core.`object`.rest.v1.security | ||
|
||
import org.dreamexposure.discal.core.`object`.new.security.Scope | ||
import org.dreamexposure.discal.core.`object`.new.security.TokenType | ||
|
||
data class ValidateRequest( | ||
val token: String, | ||
val schemas: List<TokenType>, | ||
val scopes: List<Scope>, | ||
) |
9 changes: 9 additions & 0 deletions
9
...src/main/kotlin/org/dreamexposure/discal/core/object/rest/v1/security/ValidateResponse.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,9 @@ | ||
package org.dreamexposure.discal.core.`object`.rest.v1.security | ||
|
||
import org.springframework.http.HttpStatus | ||
|
||
data class ValidateResponse( | ||
val valid: Boolean, | ||
val code: HttpStatus, | ||
val message: String, | ||
) |
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
Oops, something went wrong.