Skip to content

Commit

Permalink
Adding some extra logging and small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
NovaFox161 committed Oct 1, 2023
1 parent c1f0ef3 commit 6758f20
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.dreamexposure.discal.cam.endpoints.v1
package org.dreamexposure.discal.cam.controllers.v1

import discord4j.common.util.Snowflake
import org.dreamexposure.discal.cam.managers.CalendarAuthManager
Expand All @@ -11,13 +11,13 @@ import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/v1/")
class GetEndpoint(
@RequestMapping("/v1/token/")
class TokenController(
private val calendarAuthManager: CalendarAuthManager,
) {
@Authentication(access = Authentication.AccessLevel.ADMIN)
@GetMapping("token", produces = ["application/json"])
suspend fun get(@RequestParam host: CalendarHost, @RequestParam id: Int, @RequestParam guild: Snowflake?): CredentialData? {
@GetMapping(produces = ["application/json"])
suspend fun getToken(@RequestParam host: CalendarHost, @RequestParam id: Int, @RequestParam guild: Snowflake?): CredentialData? {
return calendarAuthManager.getCredentialData(host, id, guild)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.dreamexposure.discal.cam.endpoints.v1.oauth2
package org.dreamexposure.discal.cam.controllers.v1.oauth2

import org.dreamexposure.discal.cam.json.discal.LoginResponse
import org.dreamexposure.discal.cam.json.discal.TokenRequest
Expand All @@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/oauth2/discord/")
class DiscordOauthEndpoint(
class DiscordOauthController(
private val discordOauthManager: DiscordOauthManager,
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class GoogleAuth(
.map { CredentialData(it, calendar.secrets.expiresAt) }
.awaitSingle()
}
LOGGER.debug("Refreshing access token | guildId:{} | calendar:{}", calendar.guildId, calendar.number)

val refreshToken = aes.decrypt(calendar.secrets.encryptedRefreshToken).awaitSingle()
val refreshedCredential = doAccessTokenRequest(refreshToken) ?: return null
Expand All @@ -61,6 +62,8 @@ class GoogleAuth(
return CredentialData(accessToken, credential.expiresAt)
}

LOGGER.debug("Refreshing access token | credentialId:$credentialId")

val refreshToken = aes.decrypt(credential.encryptedRefreshToken).awaitSingle()
val refreshedCredentialData = doAccessTokenRequest(refreshToken) ?: throw EmptyNotAllowedException()
credential.encryptedAccessToken = aes.encrypt(refreshedCredentialData.accessToken).awaitSingle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import discord4j.common.util.Snowflake
import org.dreamexposure.discal.cam.google.GoogleAuth
import org.dreamexposure.discal.core.business.CalendarService
import org.dreamexposure.discal.core.enums.calendar.CalendarHost
import org.dreamexposure.discal.core.logger.LOGGER
import org.dreamexposure.discal.core.`object`.network.discal.CredentialData
import org.springframework.stereotype.Component

Expand All @@ -13,17 +14,22 @@ class CalendarAuthManager(
private val googleAuth: GoogleAuth,
) {
suspend fun getCredentialData(host: CalendarHost, id: Int, guild: Snowflake?): CredentialData? {
return when (host) {
CalendarHost.GOOGLE -> {
if (guild == null) {
// Internal (owned by DisCal, should never go bad)
googleAuth.requestNewAccessToken(id)
} else {
// External (owned by user)
val calendar = calendarService.getCalendar(guild, id) ?: return null
googleAuth.requestNewAccessToken(calendar)
return try {
when (host) {
CalendarHost.GOOGLE -> {
if (guild == null) {
// Internal (owned by DisCal, should never go bad)
googleAuth.requestNewAccessToken(id)
} else {
// External (owned by user)
val calendar = calendarService.getCalendar(guild, id) ?: return null
googleAuth.requestNewAccessToken(calendar)
}
}
}
} catch (ex: Exception) {
LOGGER.error("Get CredentialData Exception | guildId:$guild | credentialId:$id | calendarHost:${host.name}", ex)
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,5 @@ class DiscordOauthManager(
sessionService.removeAndInsertSession(session)

return TokenResponse(session.token, session.expiresAt, authInfo.user)

TODO()
}
}

0 comments on commit 6758f20

Please sign in to comment.