Skip to content

Commit 8f6c421

Browse files
committed
add test for pkce auth
1 parent 1f473d5 commit 8f6c421

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ buildscript {
3131
}
3232

3333
group = "com.adamratzman"
34-
version = "3.2.0"
34+
version = "3.2.01"
3535

3636
/*java {
3737
withSourcesJar()
@@ -165,6 +165,7 @@ kotlin {
165165
implementation("org.spekframework.spek2:spek-dsl-jvm:$spekVersion")
166166
runtimeOnly("org.spekframework.spek2:spek-runner-junit5:$spekVersion")
167167
runtimeOnly(kotlin("reflect"))
168+
implementation("com.sparkjava:spark-core:2.9.1")
168169
}
169170
}
170171

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,6 @@ fun spotifyClientApi(block: SpotifyClientApiBuilder.() -> Unit) = SpotifyClientA
352352
|| ||
353353
*/
354354

355-
356355
/**
357356
* Instantiate a new [SpotifyClientApiBuilder] using an [authCode] and [codeVerifier]. This is for **PKCE authorization**.
358357
*
@@ -367,14 +366,12 @@ fun spotifyClientApi(block: SpotifyClientApiBuilder.() -> Unit) = SpotifyClientA
367366
*/
368367
fun spotifyClientPkceApi(
369368
clientId: String?,
370-
clientSecret: String?,
371369
redirectUri: String?,
372370
authCode: String,
373371
codeVerifier: String
374372
) = SpotifyClientApiBuilder().apply {
375373
credentials {
376374
this.clientId = clientId
377-
this.clientSecret = clientSecret
378375
this.redirectUri = redirectUri
379376
}
380377

@@ -399,15 +396,13 @@ fun spotifyClientPkceApi(
399396
*/
400397
fun spotifyClientPkceApi(
401398
clientId: String?,
402-
clientSecret: String?,
403399
redirectUri: String?,
404400
authCode: String,
405401
codeVerifier: String,
406402
block: SpotifyClientApiBuilder.() -> Unit = {}
407403
) = SpotifyClientApiBuilder().apply {
408404
credentials {
409405
this.clientId = clientId
410-
this.clientSecret = clientSecret
411406
this.redirectUri = redirectUri
412407
}
413408

@@ -723,7 +718,7 @@ class SpotifyClientApiBuilder(
723718
val redirectUri = credentials.redirectUri
724719

725720
// either application credentials, or a token is required
726-
require((clientId != null && clientSecret != null && redirectUri != null) || authorization.token != null || authorization.tokenString != null) { "You need to specify a valid clientId, clientSecret, and redirectUri in the credentials block!" }
721+
require((clientId != null && clientSecret != null && redirectUri != null) || (clientId != null && redirectUri != null && authorization.pkceCodeVerifier != null) || authorization.token != null || authorization.tokenString != null) { "You need to specify a valid clientId, clientSecret, and redirectUri in the credentials block!" }
727722
return when {
728723
authorization.authorizationCode != null && authorization.pkceCodeVerifier == null -> try {
729724
require(clientId != null && clientSecret != null && redirectUri != null) { "You need to specify a valid clientId, clientSecret, and redirectUri in the credentials block!" }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.adamratzman.spotify
2+
3+
import org.spekframework.spek2.Spek
4+
import org.spekframework.spek2.style.specification.describe
5+
import spark.Spark.exception
6+
import spark.Spark.get
7+
import spark.Spark.port
8+
import kotlin.random.Random
9+
10+
class PkceTest : Spek({
11+
if (getEnvironmentVariable("VERBOSE_TEST_ENABLED")?.toBoolean() == true
12+
&& _clientId != null) {
13+
val serverRedirectUri = "http://localhost:1337"
14+
describe("verbose pkce test. print auth, wait for redirect. requires VERBOSE_TEST_ENABLED=true AND http://localhost:1337 as a redirect uri") {
15+
val pkceCodeVerifier = (1..100).map { "1" }.joinToString("")
16+
val state = Random.nextLong().toString()
17+
18+
println(
19+
getPkceAuthorizationUrl(
20+
*SpotifyScope.values(),
21+
clientId = _clientId,
22+
redirectUri = serverRedirectUri,
23+
codeChallenge = getSpotifyPkceCodeChallenge(pkceCodeVerifier),
24+
state = state
25+
))
26+
// spotifyClientApi(_clientId,)
27+
var stop = false
28+
29+
port(1337)
30+
31+
exception(Exception::class.java) { exception, request, response -> exception.printStackTrace() }
32+
33+
get("/") { request, _ ->
34+
val code = request.queryParams("code")
35+
val actualState = request.queryParams("state")
36+
if (code != null && actualState == state) {
37+
val api = spotifyClientPkceApi(_clientId, serverRedirectUri, code, pkceCodeVerifier).build()
38+
val username = api.users.getClientProfile().complete().displayName
39+
stop = true
40+
"Successfully authenticated $username with PKCE."
41+
} else "err."
42+
}
43+
44+
while (!stop) {
45+
Thread.sleep(2000)
46+
println("Waiting...")
47+
}
48+
49+
}
50+
}
51+
})

0 commit comments

Comments
 (0)