Skip to content

Commit bd4c5ad

Browse files
committed
rename getPkceAuthorizationUrl to getSpotifyPkceAuthorizationUrl, add pkce builder
Signed-off-by: Adam Ratzman <[email protected]>
1 parent 1c9d0b4 commit bd4c5ad

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,18 @@ the code challenge used to authorize the user.
190190

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

195195
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.
196196

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

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import com.adamratzman.spotify.SpotifyScope
1515
import com.adamratzman.spotify.SpotifyUserAuthorization
1616
import com.adamratzman.spotify.auth.SpotifyDefaultCredentialStore
1717
import com.adamratzman.spotify.auth.getDefaultCredentialStore
18-
import com.adamratzman.spotify.getPkceAuthorizationUrl
18+
import com.adamratzman.spotify.getSpotifyPkceAuthorizationUrl
1919
import com.adamratzman.spotify.getSpotifyPkceCodeChallenge
2020
import com.adamratzman.spotify.spotifyClientPkceApi
2121
import com.adamratzman.spotify.utils.logToConsole
@@ -59,7 +59,7 @@ public abstract class AbstractSpotifyPkceLoginActivity : AppCompatActivity() {
5959
/**
6060
* Get the authorization url that the client will be redirected to during PKCE authorization.
6161
*/
62-
public fun getAuthorizationUrl(): Uri = getPkceAuthorizationUrl(
62+
public fun getAuthorizationUrl(): Uri = getSpotifyPkceAuthorizationUrl(
6363
*scopes.toTypedArray(),
6464
clientId = clientId,
6565
redirectUri = redirectUri,

src/commonMain/kotlin/com.adamratzman.spotify/Builder.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public fun getSpotifyAuthorizationUrl(
6060
* Then, base64url encode the hash that you generated.
6161
*
6262
*/
63-
public fun getPkceAuthorizationUrl(
63+
public fun getSpotifyPkceAuthorizationUrl(
6464
vararg scopes: SpotifyScope,
6565
clientId: String,
6666
redirectUri: String,
@@ -345,6 +345,40 @@ public fun spotifyClientPkceApi(
345345
usesPkceAuth = true
346346
}
347347

348+
/**
349+
* Instantiate a new [SpotifyClientApiBuilder]. This is for **PKCE authorization**.
350+
*
351+
* Use case: I am using the PKCE client authorization flow.
352+
*
353+
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
354+
* @param redirectUri Spotify [redirect uri](https://developer.spotify.com/documentation/general/guides/app-settings/)
355+
* @param pkceCodeVerifier The code verifier generated that the client authenticated with (using its code challenge)
356+
* @param authorizationCode Only available when building [SpotifyClientApi]. Spotify auth code
357+
* @param block Override default API options such as the cache limit
358+
*
359+
* @return Configurable [SpotifyClientApiBuilder] that, when built, creates a new [SpotifyClientApi]
360+
*/
361+
public fun spotifyClientPkceApi(
362+
clientId: String?,
363+
redirectUri: String?,
364+
authorizationCode: String,
365+
pkceCodeVerifier: String,
366+
block: SpotifyApiOptions.() -> Unit = {}
367+
): SpotifyClientApiBuilder = SpotifyClientApiBuilder().apply {
368+
credentials {
369+
this.clientId = clientId
370+
this.redirectUri = redirectUri
371+
}
372+
373+
authorization {
374+
this.authorizationCode = authorizationCode
375+
this.pkceCodeVerifier = pkceCodeVerifier
376+
}
377+
options(block)
378+
379+
usesPkceAuth = true
380+
}
381+
348382
/**
349383
* Spotify API builder
350384
*/

src/commonMain/kotlin/com.adamratzman.spotify/SpotifyApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B
169169
)
170170
}
171171

172-
public fun getPkceAuthorizationUrl(
172+
public fun getSpotifyPkceAuthorizationUrl(
173173
vararg scopes: SpotifyScope,
174174
redirectUri: String,
175175
codeChallenge: String,

src/jvmTest/kotlin/com/adamratzman/spotify/PkceTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PkceTest {
2222
val state = Random.nextLong().toString()
2323

2424
println(
25-
getPkceAuthorizationUrl(
25+
getSpotifyPkceAuthorizationUrl(
2626
*SpotifyScope.values(),
2727
clientId = clientId,
2828
redirectUri = serverRedirectUri,

0 commit comments

Comments
 (0)