Skip to content

Commit 178fac0

Browse files
committed
add parseSpotifyCallbackHashToToken utility to convert hash to Token
1 parent d626837 commit 178fac0

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,15 +624,16 @@ open class SpotifyClientApi internal constructor(
624624
/**
625625
* Whether the current access token allows access to scope [scope]
626626
*/
627-
fun hasScope(scope: SpotifyScope): Boolean = hasScopes(scope)
627+
fun hasScope(scope: SpotifyScope): Boolean? = hasScopes(scope)
628628

629629
/**
630630
* Whether the current access token allows access to all of the provided scopes
631631
*/
632-
fun hasScopes(scope: SpotifyScope, vararg scopes: SpotifyScope): Boolean =
633-
!isTokenValid(false).isValid &&
634-
token.scopes.contains(scope) &&
635-
scopes.all { token.scopes.contains(it) }
632+
fun hasScopes(scope: SpotifyScope, vararg scopes: SpotifyScope): Boolean? =
633+
if (token.scopes == null) null
634+
else !isTokenValid(false).isValid &&
635+
token.scopes?.contains(scope) == true &&
636+
scopes.all { token.scopes?.contains(it) == true }
636637
}
637638

638639

src/commonMain/kotlin/com.adamratzman.spotify/models/Authentication.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ data class Token(
3232
@Transient
3333
var expiresAt: Long = getCurrentTimeMs() + expiresIn * 1000
3434
@Transient
35-
var scopes: List<SpotifyScope> = scopeString?.let { str ->
35+
var scopes: List<SpotifyScope>? = scopeString?.let { str ->
3636
str.split(" ").mapNotNull { scope -> SpotifyScope.values().find { it.uri.equals(scope, true) } }
37-
} ?: listOf()
37+
}
3838

3939
fun shouldRefresh(): Boolean = getCurrentTimeMs() > expiresAt
4040

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.adamratzman.spotify.utils
2+
3+
import com.adamratzman.spotify.SpotifyImplicitGrantApi
4+
import com.adamratzman.spotify.models.Token
5+
import org.w3c.dom.url.URLSearchParams
6+
import kotlin.browser.window
7+
8+
/**
9+
* Parse the current url into a valid [Token] to be used when instantiating a new [SpotifyImplicitGrantApi]
10+
*/
11+
fun parseSpotifyCallbackHashToToken() = parseSpotifyCallbackHashToToken(window.location.hash.substring(1))
12+
13+
/**
14+
* Parse the hash string into a valid [Token] to be used when instantiating a new [SpotifyImplicitGrantApi]
15+
*
16+
* @param hashString The Spotify hash string containing access_token, token_type, and expires_in.
17+
*/
18+
fun parseSpotifyCallbackHashToToken(hashString: String): Token {
19+
val hash = URLSearchParams(hashString)
20+
21+
return Token(
22+
hash.get("access_token") ?: throw IllegalStateException("access_token is not part of the hash!"),
23+
hash.get("token_type") ?: throw IllegalStateException("token_type is not part of the hash!"),
24+
hash.get("expires_in")?.toIntOrNull() ?: throw IllegalStateException("expires_in is not part of the hash!")
25+
)
26+
}

0 commit comments

Comments
 (0)