Skip to content

Commit 68038d9

Browse files
authored
Merge pull request #264 from adamint/dev
add more builder methods
2 parents fc3a06e + 2caafac commit 68038d9

File tree

19 files changed

+168
-47
lines changed

19 files changed

+168
-47
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,18 @@ the code challenge used to authorize the user.
193193

194194
This library contains helpful methods that can be used to simplify the PKCE authorization process.
195195
This includes `getSpotifyPkceCodeChallenge`, which SHA256 hashes and base64url encodes the code
196-
challenge, and `getPkceAuthorizationUrl`, which allows you to generate an easy authorization url for PKCE flow.
196+
challenge, and `getSpotifyPkceAuthorizationUrl`, which allows you to generate an easy authorization url for PKCE flow.
197197

198198
Please see the [spotifyClientPkceApi builder docs](https://adamint.github.io/spotify-web-api-kotlin-docs/spotify-web-api-kotlin/com.adamratzman.spotify/spotify-client-pkce-api.html) for a full list of available builders.
199199

200200
**Takeaway**: Use PKCE authorization flow in applications where you cannot secure the client secret.
201201

202-
To get a PKCE authorization url, to which you can redirect a user, you can use the `getPkceAuthorizationUrl`
202+
To get a PKCE authorization url, to which you can redirect a user, you can use the `getSpotifyPkceAuthorizationUrl`
203203
top-level method. An example is shown below, requesting 4 different scopes.
204204
```kotlin
205205
val codeVerifier = "thisisaveryrandomalphanumericcodeverifierandisgreaterthan43characters"
206206
val codeChallenge = getSpotifyPkceCodeChallenge(codeVerifier) // helper method
207-
val url: String = getPkceAuthorizationUrl(
207+
val url: String = getSpotifyPkceAuthorizationUrl(
208208
SpotifyScope.PLAYLIST_READ_PRIVATE,
209209
SpotifyScope.PLAYLIST_MODIFY_PRIVATE,
210210
SpotifyScope.USER_FOLLOW_READ,

src/androidMain/kotlin/com/adamratzman/spotify/auth/SpotifyDefaultCredentialStore.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
12
package com.adamratzman.spotify.auth
23

34
import android.annotation.SuppressLint
@@ -20,7 +21,6 @@ import com.adamratzman.spotify.spotifyClientPkceApi
2021
import com.adamratzman.spotify.spotifyImplicitGrantApi
2122
import com.adamratzman.spotify.utils.logToConsole
2223

23-
2424
/**
2525
* Provided credential store for holding current Spotify token credentials, allowing you to easily store and retrieve
2626
* Spotify tokens. Recommended in most use-cases.
@@ -57,7 +57,6 @@ public class SpotifyDefaultCredentialStore(
5757
public var activityBackOnImplicitAuth: Class<out Activity>? = null
5858
}
5959

60-
6160
public var credentialTypeStored: CredentialType? = null
6261

6362
/**
@@ -100,7 +99,6 @@ public class SpotifyDefaultCredentialStore(
10099
get() = encryptedPreferences.getString(SpotifyRefreshTokenKey, null)
101100
set(value) = encryptedPreferences.edit().putString(SpotifyRefreshTokenKey, value).apply()
102101

103-
104102
/**
105103
* Get/set the Spotify [Token] obtained from [spotifyToken].
106104
* If the token has expired according to [spotifyTokenExpiresAt], this will return null.
@@ -197,4 +195,4 @@ public enum class CredentialType {
197195
@RequiresApi(Build.VERSION_CODES.M)
198196
public fun Application.getDefaultCredentialStore(clientId: String, redirectUri: String): SpotifyDefaultCredentialStore {
199197
return SpotifyDefaultCredentialStore(clientId, redirectUri, applicationContext)
200-
}
198+
}

src/androidMain/kotlin/com/adamratzman/spotify/auth/implicit/AbstractSpotifyAppCompatImplicitLoginActivity.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
12
package com.adamratzman.spotify.auth.implicit
23

34
import android.app.Activity
@@ -17,16 +18,14 @@ public abstract class AbstractSpotifyAppCompatImplicitLoginActivity : SpotifyImp
1718
public override val activity: Activity = this
1819
public override val useDefaultRedirectHandler: Boolean = true
1920

20-
2121
override fun onCreate(savedInstanceState: Bundle?) {
2222
super.onCreate(savedInstanceState)
2323

2424
triggerLoginActivity()
2525
}
2626

27-
2827
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
2928
super.onActivityResult(requestCode, resultCode, intent)
3029
processActivityResult(requestCode, resultCode, intent)
3130
}
32-
}
31+
}

src/androidMain/kotlin/com/adamratzman/spotify/auth/implicit/AbstractSpotifyAppImplicitLoginActivity.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
12
package com.adamratzman.spotify.auth.implicit
23

34
import android.app.Activity
@@ -17,16 +18,14 @@ public abstract class AbstractSpotifyAppImplicitLoginActivity : SpotifyImplicitL
1718
public override val activity: Activity = this
1819
public override val useDefaultRedirectHandler: Boolean = true
1920

20-
2121
override fun onCreate(savedInstanceState: Bundle?) {
2222
super.onCreate(savedInstanceState)
2323

2424
triggerLoginActivity()
2525
}
2626

27-
2827
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
2928
super.onActivityResult(requestCode, resultCode, intent)
3029
processActivityResult(requestCode, resultCode, intent)
3130
}
32-
}
31+
}

src/androidMain/kotlin/com/adamratzman/spotify/auth/implicit/ImplicitAuthUtils.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
12
package com.adamratzman.spotify.auth.implicit
23

34
import android.app.Activity
@@ -46,4 +47,3 @@ public fun <T> Activity.guardValidImplicitSpotifyApi(
4647
null
4748
}
4849
}
49-

src/androidMain/kotlin/com/adamratzman/spotify/auth/implicit/SpotifyImplicitLoginActivity.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
12
package com.adamratzman.spotify.auth.implicit
23

34
import android.app.Activity
@@ -52,7 +53,6 @@ public interface SpotifyImplicitLoginActivity {
5253
*/
5354
public fun onFailure(errorMessage: String)
5455

55-
5656
/**
5757
* Override this to define what to do after [onSuccess] has run.
5858
* The default behavior is to finish the activity, and redirect the user back to the activity set on [SpotifyDefaultCredentialStore.activityBackOnImplicitAuth]
@@ -90,7 +90,7 @@ public interface SpotifyImplicitLoginActivity {
9090
val token = Token(
9191
response.accessToken,
9292
response.type.name,
93-
response.expiresIn,
93+
response.expiresIn
9494
)
9595
val api = spotifyImplicitGrantApi(
9696
clientId = clientId,
@@ -101,7 +101,7 @@ public interface SpotifyImplicitLoginActivity {
101101
redirectAfterOnSuccessAuthentication()
102102
}
103103
// AuthorizationResponse.Type.CODE -> TODO()
104-
//AuthorizationResponse.Type.UNKNOWN -> TODO()
104+
// AuthorizationResponse.Type.UNKNOWN -> TODO()
105105
AuthorizationResponse.Type.ERROR -> {
106106
logToConsole("Got error in authorization... executing error handler")
107107
onFailure(response.error ?: "Generic authentication error")
@@ -113,7 +113,5 @@ public interface SpotifyImplicitLoginActivity {
113113
}
114114
activity.finish()
115115
}
116-
117116
}
118-
119-
}
117+
}

src/androidMain/kotlin/com/adamratzman/spotify/auth/pkce/AbstractSpotifyPkceLoginActivity.kt

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
12
package com.adamratzman.spotify.auth.pkce
23

34
import android.content.Intent
@@ -15,13 +16,13 @@ import com.adamratzman.spotify.SpotifyScope
1516
import com.adamratzman.spotify.SpotifyUserAuthorization
1617
import com.adamratzman.spotify.auth.SpotifyDefaultCredentialStore
1718
import com.adamratzman.spotify.auth.getDefaultCredentialStore
18-
import com.adamratzman.spotify.getPkceAuthorizationUrl
19+
import com.adamratzman.spotify.getSpotifyPkceAuthorizationUrl
1920
import com.adamratzman.spotify.getSpotifyPkceCodeChallenge
2021
import com.adamratzman.spotify.spotifyClientPkceApi
2122
import com.adamratzman.spotify.utils.logToConsole
2223
import com.spotify.sdk.android.auth.AuthorizationResponse
23-
import kotlinx.coroutines.runBlocking
2424
import kotlin.random.Random
25+
import kotlinx.coroutines.runBlocking
2526

2627
/**
2728
* This class hooks into spotify-web-api-kotlin to provide PKCE authorization for Android application. Paired with [SpotifyDefaultCredentialStore] to easily store credentials.
@@ -59,7 +60,7 @@ public abstract class AbstractSpotifyPkceLoginActivity : AppCompatActivity() {
5960
/**
6061
* Get the authorization url that the client will be redirected to during PKCE authorization.
6162
*/
62-
public fun getAuthorizationUrl(): Uri = getPkceAuthorizationUrl(
63+
public fun getAuthorizationUrl(): Uri = getSpotifyPkceAuthorizationUrl(
6364
*scopes.toTypedArray(),
6465
clientId = clientId,
6566
redirectUri = redirectUri,
@@ -162,7 +163,6 @@ public abstract class AbstractSpotifyPkceLoginActivity : AppCompatActivity() {
162163
IllegalArgumentException("API token was blank")
163164
)
164165
}
165-
166166
} catch (exception: Exception) {
167167
setProgressBarInvisible()
168168
logToConsole("Got error in authorization... executing error handler")
@@ -182,5 +182,4 @@ public abstract class AbstractSpotifyPkceLoginActivity : AppCompatActivity() {
182182
private fun setProgressBarVisible() {
183183
findViewById<FrameLayout>(R.id.progress_overlay).visibility = View.VISIBLE
184184
}
185-
186-
}
185+
}

src/androidMain/kotlin/com/adamratzman/spotify/auth/pkce/PkceAuthUtils.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
12
package com.adamratzman.spotify.auth.pkce
23

34
import android.app.Activity
@@ -6,8 +7,8 @@ import android.os.Build
67
import androidx.annotation.RequiresApi
78

89
public fun Intent?.isSpotifyPkceAuthIntent(redirectUri: String): Boolean {
9-
return this != null
10-
&& (dataString?.startsWith("$redirectUri/?code=") == true || dataString?.startsWith("$redirectUri/?error=") == true)
10+
return this != null &&
11+
(dataString?.startsWith("$redirectUri/?code=") == true || dataString?.startsWith("$redirectUri/?error=") == true)
1112
}
1213

1314
/**
@@ -19,4 +20,4 @@ public fun Intent?.isSpotifyPkceAuthIntent(redirectUri: String): Boolean {
1920
public fun Activity.startSpotifyClientPkceLoginActivity(spotifyLoginImplementationClass: Class<out AbstractSpotifyPkceLoginActivity>) {
2021
val intent = Intent(this, spotifyLoginImplementationClass)
2122
startActivity(intent)
22-
}
23+
}

src/androidMain/kotlin/com/adamratzman/spotify/notifications/AbstractSpotifyBroadcastReceiver.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
12
package com.adamratzman.spotify.notifications
23

34
import android.content.BroadcastReceiver
@@ -43,7 +44,6 @@ public abstract class AbstractSpotifyBroadcastReceiver : BroadcastReceiver() {
4344
)
4445
)
4546
}
46-
4747
}
4848

4949
/**
@@ -80,7 +80,6 @@ public abstract class AbstractSpotifyBroadcastReceiver : BroadcastReceiver() {
8080
public companion object {
8181
public const val BaseSpotifyNotificationId: String = "com.spotify.music"
8282
}
83-
8483
}
8584

8685
/**
@@ -122,7 +121,7 @@ public data class SpotifyMetadataChangedData(
122121
* A playback state change is sent whenever the user presses play/pause, or when seeking the track position. It uses the intent action com.spotify.music.playbackstatechanged.
123122
*
124123
* @param playing True if playing, false if paused.
125-
* @param positionInMs The current playback position in milliseconds.
124+
* @param positionInMs The current playback position in milliseconds.
126125
* @param timeSentInMs When the notification was sent.
127126
*/
128127
public data class SpotifyPlaybackStateChangedData(

src/androidMain/kotlin/com/adamratzman/spotify/notifications/SpotifyBroadcastReceiverUtils.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
12
package com.adamratzman.spotify.notifications
23

34
import android.app.Activity
@@ -24,4 +25,4 @@ public fun Context.registerSpotifyBroadcastReceiver(
2425
notificationTypes.forEach { filter.addAction(it.id) }
2526

2627
registerReceiver(receiver, filter)
27-
}
28+
}

src/androidMain/kotlin/com/adamratzman/spotify/utils/PlatformUtils.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public actual typealias ConcurrentHashMap<K, V> = java.util.concurrent.Concurren
2626

2727
public actual fun <K, V> ConcurrentHashMap<K, V>.asList(): List<Pair<K, V>> = toList()
2828

29-
3029
// safeLet retrieved from: https://stackoverflow.com/a/35522422/6422820
3130
private fun <T1 : Any, T2 : Any, T3 : Any, R : Any> safeLet(p1: T1?, p2: T2?, p3: T3?, block: (T1, T2, T3) -> R?): R? =
3231
if (p1 != null && p2 != null && p3 != null) block(p1, p2, p3) else null
@@ -41,4 +40,4 @@ internal fun toast(context: Context?, message: String?, duration: Int = Toast.LE
4140

4241
internal fun logToConsole(message: String) {
4342
Log.i("spotify-web-api-kotlin", message)
44-
}
43+
}

0 commit comments

Comments
 (0)