-
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.
Cleanup calendar authentication implementation
This pass brings this code more properly in-line with adopted patterns, updates the Google api dependencies, and begins defining the pattern for api wrappers
- Loading branch information
1 parent
45d6508
commit cf17c30
Showing
25 changed files
with
351 additions
and
273 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
cam/src/main/kotlin/org/dreamexposure/discal/cam/business/google/GoogleAuthService.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,52 @@ | ||
package org.dreamexposure.discal.cam.business.google | ||
|
||
import org.dreamexposure.discal.core.business.CalendarService | ||
import org.dreamexposure.discal.core.business.CredentialService | ||
import org.dreamexposure.discal.core.business.api.GoogleCalendarApiWrapper | ||
import org.dreamexposure.discal.core.exceptions.EmptyNotAllowedException | ||
import org.dreamexposure.discal.core.exceptions.NotFoundException | ||
import org.dreamexposure.discal.core.extensions.isExpiredTtl | ||
import org.dreamexposure.discal.core.logger.LOGGER | ||
import org.dreamexposure.discal.core.`object`.new.CalendarMetadata | ||
import org.dreamexposure.discal.core.`object`.new.model.discal.cam.TokenV1Model | ||
import org.springframework.stereotype.Component | ||
import java.time.Duration | ||
import java.time.Instant | ||
|
||
@Component | ||
class GoogleAuthService( | ||
private val credentialService: CredentialService, | ||
private val calendarService: CalendarService, | ||
private val googleCalendarApiWrapper: GoogleCalendarApiWrapper, | ||
) { | ||
suspend fun requestNewAccessToken(calendar: CalendarMetadata): TokenV1Model? { | ||
if (!calendar.secrets.expiresAt.isExpiredTtl()) return TokenV1Model(calendar.secrets.accessToken, calendar.secrets.expiresAt) | ||
|
||
LOGGER.debug("Refreshing access token | guildId:{} | calendar:{}", calendar.guildId, calendar.number) | ||
|
||
val refreshed = googleCalendarApiWrapper.refreshAccessToken(calendar.secrets.refreshToken).entity ?: return null | ||
calendar.secrets.accessToken = refreshed.accessToken | ||
calendar.secrets.expiresAt = Instant.now().plusSeconds(refreshed.expiresIn.toLong()).minus(Duration.ofMinutes(5)) // Add some wiggle room | ||
calendarService.updateCalendarMetadata(calendar) | ||
|
||
LOGGER.debug("Refreshed access token | guildId:{} | calendar:{}, validUntil:{}", calendar.guildId, calendar.number, calendar.external) | ||
|
||
return TokenV1Model(calendar.secrets.accessToken, calendar.secrets.expiresAt) | ||
} | ||
|
||
suspend fun requestNewAccessToken(credentialId: Int): TokenV1Model { | ||
val credential = credentialService.getCredential(credentialId) ?: throw NotFoundException() | ||
if (!credential.expiresAt.isExpiredTtl()) return TokenV1Model(credential.accessToken, credential.expiresAt) | ||
|
||
LOGGER.debug("Refreshing access token | credentialId:$credentialId") | ||
|
||
val refreshed = googleCalendarApiWrapper.refreshAccessToken(credential.refreshToken).entity ?: throw EmptyNotAllowedException() | ||
credential.accessToken = refreshed.accessToken | ||
credential.expiresAt = Instant.now().plusSeconds(refreshed.expiresIn.toLong()).minus(Duration.ofMinutes(5)) // Add some wiggle room | ||
credentialService.updateCredential(credential) | ||
|
||
LOGGER.debug("Refreshed access token | credentialId:{} | validUntil:{}", credentialId, credential.expiresAt) | ||
|
||
return TokenV1Model(credential.accessToken, credential.expiresAt) | ||
} | ||
} |
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
118 changes: 0 additions & 118 deletions
118
cam/src/main/kotlin/org/dreamexposure/discal/cam/google/GoogleAuth.kt
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
cam/src/main/kotlin/org/dreamexposure/discal/cam/json/google/ErrorData.kt
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
cam/src/main/kotlin/org/dreamexposure/discal/cam/json/google/RefreshData.kt
This file was deleted.
Oops, something went wrong.
14 changes: 7 additions & 7 deletions
14
cam/src/main/kotlin/org/dreamexposure/discal/cam/managers/CalendarAuthManager.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
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.