Skip to content

Commit 8c63716

Browse files
committed
Support token for multiple scopes
1 parent c014f94 commit 8c63716

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

api/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ All the endpoints will respond with `200` if successful or:
2727
- `POST \search\{query}` Make a search.
2828

2929
### Tokens
30-
- `POST \token\{scope}` Request an access token for a specific scope.
30+
- `POST \token\{scope}` Request an access token for a specific scope (or a comma separated list of scopes).
3131

3232
### Events
3333
You can subscribe for players events by creating a WebSocket connection to `/events`.

core/src/main/java/xyz/gianlu/librespot/core/TokenProvider.java

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,55 @@
33
import com.google.gson.JsonArray;
44
import org.apache.log4j.Logger;
55
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
67
import xyz.gianlu.librespot.mercury.MercuryClient;
78
import xyz.gianlu.librespot.mercury.MercuryRequests;
89

910
import java.io.IOException;
11+
import java.util.ArrayList;
1012
import java.util.Arrays;
11-
import java.util.HashMap;
12-
import java.util.Map;
13+
import java.util.List;
14+
import java.util.Objects;
1315

1416
/**
1517
* @author Gianlu
1618
*/
17-
public class TokenProvider {
19+
public final class TokenProvider {
1820
private final static Logger LOGGER = Logger.getLogger(TokenProvider.class);
1921
private final static int TOKEN_EXPIRE_THRESHOLD = 10;
2022
private final Session session;
21-
private final Map<String, StoredToken> tokens = new HashMap<>();
23+
private final List<StoredToken> tokens = new ArrayList<>();
2224

2325
TokenProvider(@NotNull Session session) {
2426
this.session = session;
2527
}
2628

29+
@Nullable
30+
private StoredToken findTokenWithAllScopes(String[] scopes) {
31+
for (StoredToken token : tokens)
32+
if (token.hasScopes(scopes))
33+
return token;
34+
35+
return null;
36+
}
37+
2738
@NotNull
28-
public StoredToken getToken(@NotNull String scope) throws IOException, MercuryClient.MercuryException {
29-
if (scope.contains(",")) throw new UnsupportedOperationException("Only single scope tokens are supported.");
39+
public StoredToken getToken(@NotNull String... scopes) throws IOException, MercuryClient.MercuryException {
40+
if (scopes.length == 0) throw new IllegalArgumentException();
3041

31-
StoredToken token = tokens.get(scope);
42+
StoredToken token = findTokenWithAllScopes(scopes);
3243
if (token != null) {
33-
if (token.expired()) tokens.remove(scope);
44+
if (token.expired()) tokens.remove(token);
3445
else return token;
3546
}
3647

37-
LOGGER.debug(String.format("Token expired or not suitable, requesting again. {scope: %s, oldToken: %s}", scope, token));
38-
MercuryRequests.KeymasterToken resp = session.mercury().sendSync(MercuryRequests.requestToken(session.deviceId(), scope));
48+
LOGGER.debug(String.format("Token expired or not suitable, requesting again. {scopes: %s, oldToken: %s}", Arrays.asList(scopes), token));
49+
MercuryRequests.KeymasterToken resp = session.mercury().sendSync(MercuryRequests.requestToken(session.deviceId(), String.join(",", scopes)));
3950
token = new StoredToken(resp);
4051

41-
LOGGER.debug(String.format("Updated token successfully! {scope: %s, newToken: %s}", scope, token));
52+
LOGGER.debug(String.format("Updated token successfully! {scopes: %s, newToken: %s}", Arrays.asList(scopes), token));
53+
tokens.add(token);
4254

43-
tokens.put(scope, token);
4455
return token;
4556
}
4657

@@ -79,5 +90,21 @@ public String toString() {
7990
", timestamp=" + timestamp +
8091
'}';
8192
}
93+
94+
public boolean hasScope(@NotNull String scope) {
95+
for (String s : scopes)
96+
if (Objects.equals(s, scope))
97+
return true;
98+
99+
return false;
100+
}
101+
102+
public boolean hasScopes(String[] sc) {
103+
for (String s : sc)
104+
if (!hasScope(s))
105+
return false;
106+
107+
return true;
108+
}
82109
}
83110
}

0 commit comments

Comments
 (0)