Skip to content

Commit

Permalink
Attempting possible fix when handling invalid encrypted data
Browse files Browse the repository at this point in the history
Specifically only on encrypted access tokens are we theoretically should be able to recover from this state
  • Loading branch information
NovaFox161 committed Jan 2, 2024
1 parent be4a527 commit 9ea7fab
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.dreamexposure.discal.core.enums.calendar.CalendarHost
import org.dreamexposure.discal.core.extensions.asInstantMilli
import org.dreamexposure.discal.core.extensions.asSnowflake
import java.time.Instant
import javax.crypto.IllegalBlockSizeException

data class Calendar private constructor(
val guildId: Snowflake,
Expand All @@ -21,6 +22,11 @@ data class Calendar private constructor(
companion object {
suspend operator fun invoke(data: CalendarData): Calendar {
val aes = AESEncryption(data.privateKey)
val accessToken = try {
aes.decrypt(data.accessToken).awaitSingle()
} catch (ex: IllegalBlockSizeException) {
null
}

return Calendar(
guildId = data.guildId.asSnowflake(),
Expand All @@ -32,9 +38,9 @@ data class Calendar private constructor(
secrets = Secrets(
credentialId = data.credentialId,
privateKey = data.privateKey,
expiresAt = data.expiresAt.asInstantMilli(),
expiresAt = if (accessToken != null) data.expiresAt.asInstantMilli() else Instant.EPOCH,
refreshToken = aes.decrypt(data.refreshToken).awaitSingle(),
accessToken = aes.decrypt(data.accessToken).awaitSingle(),
accessToken = accessToken ?: "",
)
)
}
Expand Down

0 comments on commit 9ea7fab

Please sign in to comment.