-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support MusicBrainz OAuth refresh so that user will stay logged…
… in until they click log out resolves #415
- Loading branch information
David Ly
committed
Sep 3, 2023
1 parent
f9a5633
commit ad79187
Showing
4 changed files
with
115 additions
and
10 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
58 changes: 58 additions & 0 deletions
58
data/musicbrainz/src/main/java/ly/david/data/musicbrainz/api/MusicBrainzOAuthApi.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,58 @@ | ||
package ly.david.data.musicbrainz.api | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.body | ||
import io.ktor.client.request.forms.submitForm | ||
import io.ktor.http.parameters | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
// TODO: could be generic and reuse for mb and spotify | ||
@Serializable | ||
data class MusicBrainzOAuthAccessToken( | ||
@SerialName("access_token") val accessToken: String, | ||
@SerialName("refresh_token") val refreshToken: String, | ||
) | ||
|
||
internal const val REFRESH_TOKEN = "refresh_token" | ||
|
||
interface MusicBrainzOAuthApi { | ||
|
||
companion object { | ||
fun create( | ||
httpClient: HttpClient, | ||
): MusicBrainzOAuthApi { | ||
return MusicBrainzOAuthApiImpl( | ||
httpClient = httpClient, | ||
) | ||
} | ||
} | ||
|
||
suspend fun getAccessToken( | ||
clientId: String, | ||
clientSecret: String, | ||
grantType: String, | ||
refreshToken: String, | ||
): MusicBrainzOAuthAccessToken | ||
} | ||
|
||
class MusicBrainzOAuthApiImpl( | ||
val httpClient: HttpClient, | ||
) : MusicBrainzOAuthApi { | ||
override suspend fun getAccessToken( | ||
clientId: String, | ||
clientSecret: String, | ||
grantType: String, | ||
refreshToken: String, | ||
): MusicBrainzOAuthAccessToken { | ||
return httpClient.submitForm( | ||
url = "$MUSIC_BRAINZ_BASE_URL/oauth2/token", | ||
formParameters = parameters { | ||
append("client_id", clientId) | ||
append("client_secret", clientSecret) | ||
append("grant_type", grantType) | ||
append("refresh_token", refreshToken) | ||
}, | ||
).body() | ||
} | ||
} |
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