-
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.
Implement new GoogleCalendarApiWrapper component
Now to build up the main abstraction layer for calendars. Maybe PartialCalendar/FullCalendar? TBD
- Loading branch information
1 parent
b40ed04
commit 1166303
Showing
11 changed files
with
424 additions
and
34 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
43 changes: 43 additions & 0 deletions
43
core/src/main/kotlin/org/dreamexposure/discal/core/business/api/GoogleAuthApiWrapper.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,43 @@ | ||
package org.dreamexposure.discal.core.business.api | ||
|
||
import okhttp3.FormBody | ||
import okhttp3.Request | ||
import org.dreamexposure.discal.core.config.Config | ||
import org.dreamexposure.discal.core.exceptions.AccessRevokedException | ||
import org.dreamexposure.discal.core.logger.LOGGER | ||
import org.dreamexposure.discal.core.`object`.new.model.ResponseModel | ||
import org.dreamexposure.discal.core.`object`.new.model.google.OauthV4RefreshTokenResponse | ||
import org.dreamexposure.discal.core.utils.GlobalVal.DEFAULT | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class GoogleAuthApiWrapper( | ||
private val apiWrapperClient: ApiWrapperClient, | ||
) { | ||
suspend fun refreshAccessToken(refreshToken: String): ResponseModel<OauthV4RefreshTokenResponse> { | ||
val requestFormBody = FormBody.Builder() | ||
.addEncoded("client_id", Config.SECRET_GOOGLE_CLIENT_ID.getString()) | ||
.addEncoded("client_secret", Config.SECRET_GOOGLE_CLIENT_SECRET.getString()) | ||
.addEncoded("refresh_token", refreshToken) | ||
.addEncoded("grant_type", "refresh_token") | ||
.build() | ||
val request = Request.Builder() | ||
.url("https://www.googleapis.com/oauth2/v4/token") | ||
.post(requestFormBody) | ||
.header("Content-Type", "application/x-www-form-urlencoded") | ||
.build() | ||
|
||
val response = apiWrapperClient.makeRequest(request, OauthV4RefreshTokenResponse::class.java) | ||
|
||
|
||
// TODO: Handling of this should be moved up higher in the impl? | ||
if (response.error?.error == "invalid_grant") { | ||
LOGGER.debug(DEFAULT, "Google Oauth invalid_grant for access token refresh") | ||
throw AccessRevokedException() // TODO: How should I handle this for external calendars? Right now we just delete everything | ||
} else if (response.error != null) { | ||
LOGGER.error(DEFAULT, "[Google] Error requesting new access token | ${response.code} | ${response.error.error}") | ||
} | ||
|
||
return response | ||
} | ||
} |
Oops, something went wrong.