@@ -9,7 +9,7 @@ import android.content.SharedPreferences
9
9
import android.os.Build
10
10
import androidx.annotation.RequiresApi
11
11
import androidx.security.crypto.EncryptedSharedPreferences
12
- import androidx.security.crypto.MasterKeys
12
+ import androidx.security.crypto.MasterKey
13
13
import com.adamratzman.spotify.GenericSpotifyApi
14
14
import com.adamratzman.spotify.SpotifyApi
15
15
import com.adamratzman.spotify.SpotifyApiOptions
@@ -21,7 +21,6 @@ import com.adamratzman.spotify.refreshSpotifyClientToken
21
21
import com.adamratzman.spotify.spotifyClientPkceApi
22
22
import com.adamratzman.spotify.spotifyImplicitGrantApi
23
23
import com.adamratzman.spotify.utils.logToConsole
24
- import kotlinx.coroutines.runBlocking
25
24
26
25
/* *
27
26
* Provided credential store for holding current Spotify token credentials, allowing you to easily store and retrieve
@@ -61,7 +60,8 @@ public class SpotifyDefaultCredentialStore(
61
60
/* *
62
61
* The PKCE code verifier key currently being used in [EncryptedSharedPreferences]
63
62
*/
64
- public const val SpotifyCurrentPkceCodeVerifierKey : String = " spotifyCurrentPkceCodeVerifier"
63
+ public const val SpotifyCurrentPkceCodeVerifierKey : String =
64
+ " spotifyCurrentPkceCodeVerifier"
65
65
66
66
/* *
67
67
* The activity to return to if re-authentication is necessary on implicit authentication. Null except during authentication when using [guardValidImplicitSpotifyApi]
@@ -71,18 +71,20 @@ public class SpotifyDefaultCredentialStore(
71
71
72
72
public var credentialTypeStored: CredentialType ? = null
73
73
74
+ private val masterKeyForEncryption =
75
+ MasterKey .Builder (applicationContext).setKeyScheme(MasterKey .KeyScheme .AES256_GCM ).build()
76
+
74
77
/* *
75
78
* The [EncryptedSharedPreferences] that this API saves to/retrieves from.
76
79
*/
77
- @Suppress(" DEPRECATION" )
78
- public val encryptedPreferences: SharedPreferences = EncryptedSharedPreferences
79
- .create(
80
- " spotify-api-encrypted-preferences" ,
81
- MasterKeys .getOrCreate(MasterKeys .AES256_GCM_SPEC ),
82
- applicationContext,
83
- EncryptedSharedPreferences .PrefKeyEncryptionScheme .AES256_SIV ,
84
- EncryptedSharedPreferences .PrefValueEncryptionScheme .AES256_GCM
85
- )
80
+ public val encryptedPreferences: SharedPreferences = EncryptedSharedPreferences .create(
81
+ applicationContext,
82
+ " spotify-api-encrypted-preferences" ,
83
+ masterKeyForEncryption,
84
+ EncryptedSharedPreferences .PrefKeyEncryptionScheme .AES256_SIV ,
85
+ EncryptedSharedPreferences .PrefValueEncryptionScheme .AES256_GCM
86
+ )
87
+
86
88
87
89
/* *
88
90
* Get/set when the Spotify access token will expire, in milliseconds from UNIX epoch. This will be one hour from authentication.
@@ -126,7 +128,8 @@ public class SpotifyDefaultCredentialStore(
126
128
*/
127
129
public var currentSpotifyPkceCodeVerifier: String?
128
130
get() = encryptedPreferences.getString(SpotifyCurrentPkceCodeVerifierKey , null )
129
- set(value) = encryptedPreferences.edit().putString(SpotifyCurrentPkceCodeVerifierKey , value).apply ()
131
+ set(value) = encryptedPreferences.edit().putString(SpotifyCurrentPkceCodeVerifierKey , value)
132
+ .apply ()
130
133
131
134
/* *
132
135
* Get/set the Spotify [Token] obtained from [spotifyToken].
@@ -181,31 +184,27 @@ public class SpotifyDefaultCredentialStore(
181
184
*
182
185
* @param block Applied configuration to the [SpotifyClientApi]
183
186
*/
184
- public fun getSpotifyClientPkceApi (block : ((SpotifyApiOptions ).() -> Unit )? = null): SpotifyClientApi ? {
187
+ public suspend fun getSpotifyClientPkceApi (block : ((SpotifyApiOptions ).() -> Unit )? = null): SpotifyClientApi ? {
185
188
val token = spotifyToken
186
189
? : if (spotifyRefreshToken != null ) {
187
- runBlocking {
188
- val newToken = refreshSpotifyClientToken(clientId, null , spotifyRefreshToken, true )
189
- spotifyToken = newToken
190
- newToken
191
- }
190
+ val newToken = refreshSpotifyClientToken(clientId, null , spotifyRefreshToken, true )
191
+ spotifyToken = newToken
192
+ newToken
192
193
} else {
193
194
return null
194
195
}
195
196
196
- return runBlocking {
197
- spotifyClientPkceApi(
198
- clientId,
199
- redirectUri,
200
- SpotifyUserAuthorization (token = token),
201
- block ? : {}
202
- ).build().apply {
203
- val previousAfterTokenRefresh = spotifyApiOptions.afterTokenRefresh
204
- spotifyApiOptions.afterTokenRefresh = {
205
- spotifyToken = this .token
206
- logToConsole(" Refreshed Spotify PKCE token in credential store... $token " )
207
- previousAfterTokenRefresh?.invoke(this )
208
- }
197
+ return spotifyClientPkceApi(
198
+ clientId,
199
+ redirectUri,
200
+ SpotifyUserAuthorization (token = token),
201
+ block ? : {}
202
+ ).build().apply {
203
+ val previousAfterTokenRefresh = spotifyApiOptions.afterTokenRefresh
204
+ spotifyApiOptions.afterTokenRefresh = {
205
+ spotifyToken = this .token
206
+ logToConsole(" Refreshed Spotify PKCE token in credential store... $token " )
207
+ previousAfterTokenRefresh?.invoke(this )
209
208
}
210
209
}
211
210
}
@@ -245,6 +244,9 @@ public enum class CredentialType {
245
244
}
246
245
247
246
@RequiresApi(Build .VERSION_CODES .M )
248
- public fun Application.getDefaultCredentialStore (clientId : String , redirectUri : String ): SpotifyDefaultCredentialStore {
247
+ public fun Application.getDefaultCredentialStore (
248
+ clientId : String ,
249
+ redirectUri : String
250
+ ): SpotifyDefaultCredentialStore {
249
251
return SpotifyDefaultCredentialStore (clientId, redirectUri, applicationContext)
250
252
}
0 commit comments