Skip to content

Commit 90978c1

Browse files
werwolf2303devgianlu
authored andcommitted
Request token from Login5
Changed token request from Mercury to Login5 Removed scoped token functionality
1 parent 5981fb5 commit 90978c1

File tree

18 files changed

+117
-137
lines changed

18 files changed

+117
-137
lines changed

api/src/main/java/xyz/gianlu/librespot/api/Main.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.jetbrains.annotations.NotNull;
2222
import xyz.gianlu.librespot.common.Log4JUncaughtExceptionHandler;
2323
import xyz.gianlu.librespot.core.Session;
24-
import xyz.gianlu.librespot.mercury.MercuryClient;
24+
import xyz.gianlu.librespot.core.TokenProvider;
2525
import xyz.gianlu.librespot.player.FileConfiguration;
2626
import xyz.gianlu.librespot.player.FileConfiguration.AuthStrategy;
2727

@@ -33,7 +33,7 @@
3333
*/
3434
public class Main {
3535

36-
public static void main(String[] args) throws IOException, MercuryClient.MercuryException, GeneralSecurityException, Session.SpotifyAuthenticationException {
36+
public static void main(String[] args) throws IOException, TokenProvider.TokenException, GeneralSecurityException, Session.SpotifyAuthenticationException {
3737
FileConfiguration conf = new FileConfiguration(args);
3838
Configurator.setRootLevel(conf.loggingLevel());
3939
Thread.setDefaultUncaughtExceptionHandler(new Log4JUncaughtExceptionHandler());
@@ -45,7 +45,7 @@ public static void main(String[] args) throws IOException, MercuryClient.Mercury
4545
else withPlayer(port, host, conf);
4646
}
4747

48-
private static void withPlayer(int port, @NotNull String host, @NotNull FileConfiguration conf) throws IOException, MercuryClient.MercuryException, GeneralSecurityException, Session.SpotifyAuthenticationException {
48+
private static void withPlayer(int port, @NotNull String host, @NotNull FileConfiguration conf) throws IOException, TokenProvider.TokenException, GeneralSecurityException, Session.SpotifyAuthenticationException {
4949
PlayerWrapper wrapper;
5050
if (conf.authStrategy() == AuthStrategy.ZEROCONF)
5151
wrapper = PlayerWrapper.fromZeroconf(conf.initZeroconfBuilder().create(), conf.toPlayer(), conf.toEventsShell());
@@ -57,7 +57,7 @@ private static void withPlayer(int port, @NotNull String host, @NotNull FileConf
5757
server.start();
5858
}
5959

60-
private static void withoutPlayer(int port, @NotNull String host, @NotNull FileConfiguration conf) throws IOException, MercuryClient.MercuryException, GeneralSecurityException, Session.SpotifyAuthenticationException {
60+
private static void withoutPlayer(int port, @NotNull String host, @NotNull FileConfiguration conf) throws IOException, TokenProvider.TokenException, GeneralSecurityException, Session.SpotifyAuthenticationException {
6161
SessionWrapper wrapper;
6262
if (conf.authStrategy() == AuthStrategy.ZEROCONF)
6363
wrapper = SessionWrapper.fromZeroconf(conf.initZeroconfBuilder().create(), conf.toEventsShell());

api/src/main/java/xyz/gianlu/librespot/api/handlers/MetadataHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import xyz.gianlu.librespot.api.Utils;
2727
import xyz.gianlu.librespot.common.ProtobufToJson;
2828
import xyz.gianlu.librespot.core.Session;
29+
import xyz.gianlu.librespot.core.TokenProvider;
2930
import xyz.gianlu.librespot.dealer.ApiClient;
30-
import xyz.gianlu.librespot.mercury.MercuryClient;
3131
import xyz.gianlu.librespot.metadata.*;
3232

3333
import java.io.IOException;
@@ -91,7 +91,7 @@ public void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Session
9191

9292
Utils.internalError(exchange, ex);
9393
LOGGER.error("Failed handling api request. {type: {}, uri: {}, code: {}}", type, uri, ex.code, ex);
94-
} catch (IOException | MercuryClient.MercuryException ex) {
94+
} catch (IOException | TokenProvider.TokenException ex) {
9595
Utils.internalError(exchange, ex);
9696
LOGGER.error("Failed handling api request. {type: {}, uri: {}}", type, uri, ex);
9797
} catch (IllegalArgumentException ex) {
@@ -100,7 +100,7 @@ public void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Session
100100
}
101101

102102
@NotNull
103-
private JsonObject handle(@NotNull Session session, @NotNull MetadataType type, @NotNull String uri) throws IOException, MercuryClient.MercuryException, IllegalArgumentException {
103+
private JsonObject handle(@NotNull Session session, @NotNull MetadataType type, @NotNull String uri) throws IOException, TokenProvider.TokenException, IllegalArgumentException {
104104
switch (type) {
105105
case ALBUM:
106106
return ProtobufToJson.convert(session.api().getMetadata4Album(AlbumId.fromUri(uri)));
@@ -120,7 +120,7 @@ private JsonObject handle(@NotNull Session session, @NotNull MetadataType type,
120120
}
121121

122122
@NotNull
123-
private JsonObject handlePlaylist(@NotNull Session session, @NotNull String uri) throws IOException, MercuryClient.MercuryException {
123+
private JsonObject handlePlaylist(@NotNull Session session, @NotNull String uri) throws IOException, TokenProvider.TokenException {
124124
return ProtobufToJson.convert(session.api().getPlaylist(PlaylistId.fromUri(uri)));
125125
}
126126

api/src/main/java/xyz/gianlu/librespot/api/handlers/TokensHandler.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@
2020
import io.undertow.server.HttpServerExchange;
2121
import org.jetbrains.annotations.NotNull;
2222
import xyz.gianlu.librespot.api.SessionWrapper;
23-
import xyz.gianlu.librespot.api.Utils;
2423
import xyz.gianlu.librespot.core.Session;
2524
import xyz.gianlu.librespot.core.TokenProvider;
2625

27-
import java.util.Deque;
28-
import java.util.Map;
29-
3026
public final class TokensHandler extends AbsSessionHandler {
3127

3228
public TokensHandler(@NotNull SessionWrapper wrapper) {
@@ -41,14 +37,7 @@ protected void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Sess
4137
return;
4238
}
4339

44-
Map<String, Deque<String>> params = Utils.readParameters(exchange);
45-
String scope = Utils.getFirstString(params, "scope");
46-
if (scope == null) {
47-
Utils.invalidParameter(exchange, "scope");
48-
return;
49-
}
50-
51-
TokenProvider.StoredToken token = session.tokens().getToken(scope);
40+
TokenProvider.StoredToken token = session.tokens().getToken();
5241
JsonObject obj = new JsonObject();
5342
obj.addProperty("token", token.accessToken);
5443
obj.addProperty("timestamp", token.timestamp);

api/src/main/java/xyz/gianlu/librespot/api/handlers/WebApiHandler.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import xyz.gianlu.librespot.core.TokenProvider;
2929

3030
public final class WebApiHandler extends AbsSessionHandler {
31-
private static final String[] API_TOKENS_ALL = new String[]{"ugc-image-upload", "playlist-read-collaborative", "playlist-modify-private", "playlist-modify-public", "playlist-read-private", "user-read-playback-position", "user-read-recently-played", "user-top-read", "user-modify-playback-state", "user-read-currently-playing", "user-read-playback-state", "user-read-private", "user-read-email", "user-library-modify", "user-library-read", "user-follow-modify", "user-follow-read", "streaming", "app-remote-control"};
3231
private static final HttpUrl BASE_API_URL = HttpUrl.get("https://api.spotify.com");
3332
private static final HttpString HEADER_X_SCOPE = HttpString.tryFromString("X-Spotify-Scope");
3433

@@ -47,11 +46,7 @@ protected void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Sess
4746
String body = FileUtils.readFile(exchange.getInputStream());
4847
HeaderValues contentType = exchange.getRequestHeaders().get(Headers.CONTENT_TYPE);
4948

50-
String[] scopes = API_TOKENS_ALL;
51-
if (exchange.getRequestHeaders().contains(HEADER_X_SCOPE))
52-
scopes = exchange.getRequestHeaders().get(HEADER_X_SCOPE).toArray(new String[0]);
53-
54-
TokenProvider.StoredToken token = session.tokens().getToken(scopes);
49+
TokenProvider.StoredToken token = session.tokens().getToken();
5550

5651
HttpUrl.Builder url = BASE_API_URL.newBuilder()
5752
.addPathSegments(exchange.getRelativePath().substring(1))

lib/src/main/java/xyz/gianlu/librespot/ZeroconfServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import xyz.gianlu.librespot.common.NameThreadFactory;
2828
import xyz.gianlu.librespot.common.Utils;
2929
import xyz.gianlu.librespot.core.Session;
30+
import xyz.gianlu.librespot.core.TokenProvider;
3031
import xyz.gianlu.librespot.crypto.DiffieHellman;
31-
import xyz.gianlu.librespot.mercury.MercuryClient;
3232
import xyz.gianlu.zeroconf.Service;
3333
import xyz.gianlu.zeroconf.Zeroconf;
3434

@@ -371,7 +371,7 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
371371
}
372372

373373
sessionListeners.forEach(l -> l.sessionChanged(session));
374-
} catch (Session.SpotifyAuthenticationException | MercuryClient.MercuryException | IOException | GeneralSecurityException ex) {
374+
} catch (Session.SpotifyAuthenticationException | TokenProvider.TokenException | IOException | GeneralSecurityException ex) {
375375
LOGGER.error("Couldn't establish a new session.", ex);
376376

377377
synchronized (connectionLock) {

lib/src/main/java/xyz/gianlu/librespot/audio/PlayableContentFeeder.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import xyz.gianlu.librespot.common.NameThreadFactory;
3737
import xyz.gianlu.librespot.common.Utils;
3838
import xyz.gianlu.librespot.core.Session;
39-
import xyz.gianlu.librespot.mercury.MercuryClient;
39+
import xyz.gianlu.librespot.core.TokenProvider;
4040
import xyz.gianlu.librespot.metadata.EpisodeId;
4141
import xyz.gianlu.librespot.metadata.LocalId;
4242
import xyz.gianlu.librespot.metadata.PlayableId;
@@ -81,7 +81,7 @@ private static Metadata.Track pickAlternativeIfNecessary(@NotNull Metadata.Track
8181
}
8282

8383
@NotNull
84-
public final LoadedStream load(@NotNull PlayableId id, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws CdnManager.CdnException, ContentRestrictedException, MercuryClient.MercuryException, IOException {
84+
public final LoadedStream load(@NotNull PlayableId id, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws CdnManager.CdnException, ContentRestrictedException, TokenProvider.TokenException, IOException {
8585
if (id instanceof TrackId)
8686
return loadTrack((TrackId) id, audioQualityPicker, preload, haltListener);
8787
else if (id instanceof EpisodeId)
@@ -91,7 +91,7 @@ else if (id instanceof EpisodeId)
9191
}
9292

9393
@NotNull
94-
private StorageResolveResponse resolveStorageInteractive(@NotNull ByteString fileId, boolean preload) throws IOException, MercuryClient.MercuryException {
94+
private StorageResolveResponse resolveStorageInteractive(@NotNull ByteString fileId, boolean preload) throws IOException, TokenProvider.TokenException {
9595
try (Response resp = session.api().send("GET", String.format(preload ? STORAGE_RESOLVE_INTERACTIVE_PREFETCH : STORAGE_RESOLVE_INTERACTIVE, Utils.bytesToHex(fileId)), null, null)) {
9696
if (resp.code() != 200) throw new IOException(resp.code() + ": " + resp.message());
9797

@@ -102,7 +102,7 @@ private StorageResolveResponse resolveStorageInteractive(@NotNull ByteString fil
102102
}
103103
}
104104

105-
private @NotNull LoadedStream loadTrack(@NotNull TrackId id, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws IOException, MercuryClient.MercuryException, ContentRestrictedException, CdnManager.CdnException {
105+
private @NotNull LoadedStream loadTrack(@NotNull TrackId id, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws IOException, TokenProvider.TokenException, ContentRestrictedException, CdnManager.CdnException {
106106
Metadata.Track original = session.api().getMetadata4Track(id);
107107
Metadata.Track track = pickAlternativeIfNecessary(original);
108108
if (track == null) {
@@ -129,7 +129,7 @@ private LoadedStream loadCdnStream(@NotNull Metadata.AudioFile file, @Nullable M
129129

130130
@NotNull
131131
@Contract("_, null, null, _, _ -> fail")
132-
private LoadedStream loadStream(@NotNull Metadata.AudioFile file, @Nullable Metadata.Track track, @Nullable Metadata.Episode episode, boolean preload, @Nullable HaltListener haltListener) throws IOException, MercuryClient.MercuryException, CdnManager.CdnException {
132+
private LoadedStream loadStream(@NotNull Metadata.AudioFile file, @Nullable Metadata.Track track, @Nullable Metadata.Episode episode, boolean preload, @Nullable HaltListener haltListener) throws IOException, TokenProvider.TokenException, CdnManager.CdnException {
133133
if (track == null && episode == null)
134134
throw new IllegalStateException();
135135

@@ -156,7 +156,7 @@ private LoadedStream loadStream(@NotNull Metadata.AudioFile file, @Nullable Meta
156156
}
157157

158158
@NotNull
159-
private LoadedStream loadTrack(@NotNull Metadata.Track track, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws IOException, CdnManager.CdnException, MercuryClient.MercuryException {
159+
private LoadedStream loadTrack(@NotNull Metadata.Track track, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws IOException, CdnManager.CdnException, TokenProvider.TokenException {
160160
Metadata.AudioFile file = audioQualityPicker.getFile(track.getFileList());
161161
if (file == null) {
162162
LOGGER.error("Couldn't find any suitable audio file, available: {}", Utils.formatsToString(track.getFileList()));
@@ -167,7 +167,7 @@ private LoadedStream loadTrack(@NotNull Metadata.Track track, @NotNull AudioQual
167167
}
168168

169169
@NotNull
170-
private LoadedStream loadEpisode(@NotNull EpisodeId id, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws IOException, MercuryClient.MercuryException, CdnManager.CdnException {
170+
private LoadedStream loadEpisode(@NotNull EpisodeId id, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws IOException, TokenProvider.TokenException, CdnManager.CdnException {
171171
Metadata.Episode episode = session.api().getMetadata4Episode(id);
172172

173173
if (episode.hasExternalUrl()) {

lib/src/main/java/xyz/gianlu/librespot/audio/cdn/CdnManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import xyz.gianlu.librespot.common.NameThreadFactory;
3535
import xyz.gianlu.librespot.common.Utils;
3636
import xyz.gianlu.librespot.core.Session;
37-
import xyz.gianlu.librespot.mercury.MercuryClient;
37+
import xyz.gianlu.librespot.core.TokenProvider;
3838

3939
import java.io.IOException;
4040
import java.io.InputStream;
@@ -88,7 +88,7 @@ public Streamer streamFile(@NotNull Metadata.AudioFile file, @NotNull byte[] key
8888
* This is used only to RENEW the url if needed.
8989
*/
9090
@NotNull
91-
private HttpUrl getAudioUrl(@NotNull ByteString fileId) throws IOException, CdnException, MercuryClient.MercuryException {
91+
private HttpUrl getAudioUrl(@NotNull ByteString fileId) throws IOException, CdnException, TokenProvider.TokenException {
9292
try (Response resp = session.api().send("GET", String.format("/storage-resolve/files/audio/interactive/%s", Utils.bytesToHex(fileId)), null, null)) {
9393
if (resp.code() != 200)
9494
throw new IOException(resp.code() + ": " + resp.message());
@@ -145,7 +145,7 @@ HttpUrl url() throws CdnException {
145145
if (expiration <= System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5)) {
146146
try {
147147
url = getAudioUrl(fileId);
148-
} catch (IOException | MercuryClient.MercuryException ex) {
148+
} catch (IOException | TokenProvider.TokenException ex) {
149149
throw new CdnException(ex);
150150
}
151151
}

lib/src/main/java/xyz/gianlu/librespot/core/ApResolver.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.slf4j.LoggerFactory;
3030

3131
import java.io.IOException;
32-
import java.io.Reader;
3332
import java.util.ArrayList;
3433
import java.util.HashMap;
3534
import java.util.List;

lib/src/main/java/xyz/gianlu/librespot/core/OAuth.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
import org.slf4j.LoggerFactory;
1010

1111
import java.io.*;
12-
import java.net.*;
12+
import java.net.HttpURLConnection;
13+
import java.net.InetSocketAddress;
14+
import java.net.MalformedURLException;
15+
import java.net.URL;
1316
import java.nio.charset.StandardCharsets;
1417
import java.security.MessageDigest;
1518
import java.security.NoSuchAlgorithmException;

lib/src/main/java/xyz/gianlu/librespot/core/Session.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import com.spotify.connectstate.Connect;
2525
import com.spotify.explicit.ExplicitContentPubsub;
2626
import com.spotify.explicit.ExplicitContentPubsub.UserAttributesUpdate;
27-
import okhttp3.Authenticator;
2827
import okhttp3.*;
28+
import okhttp3.Authenticator;
2929
import okio.BufferedSink;
3030
import okio.GzipSink;
3131
import okio.Okio;
@@ -340,7 +340,7 @@ private void connect() throws IOException, GeneralSecurityException, SpotifyAuth
340340
* Authenticates with the server and creates all the necessary components.
341341
* All of them should be initialized inside the synchronized block and MUST NOT call any method on this {@link Session} object.
342342
*/
343-
private void authenticate(@NotNull Authentication.LoginCredentials credentials) throws IOException, GeneralSecurityException, SpotifyAuthenticationException, MercuryClient.MercuryException {
343+
private void authenticate(@NotNull Authentication.LoginCredentials credentials) throws IOException, GeneralSecurityException, SpotifyAuthenticationException, TokenProvider.TokenException {
344344
authenticatePartial(credentials, false);
345345

346346
if (credentials.getTyp() == Authentication.AuthenticationType.AUTHENTICATION_SPOTIFY_TOKEN)
@@ -1069,7 +1069,7 @@ public Builder userPass(@NotNull String username, @NotNull String password) {
10691069
* Creates a connected and fully authenticated {@link Session} object.
10701070
*/
10711071
@NotNull
1072-
public Session create() throws IOException, GeneralSecurityException, SpotifyAuthenticationException, MercuryClient.MercuryException {
1072+
public Session create() throws IOException, GeneralSecurityException, SpotifyAuthenticationException, TokenProvider.TokenException {
10731073
if (loginCredentials == null)
10741074
throw new IllegalStateException("You must select an authentication method.");
10751075

0 commit comments

Comments
 (0)