Skip to content

Commit

Permalink
Only apply OAuth token to requests from OAuth-enabled clients.
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Dec 2, 2024
1 parent 667b077 commit 8742c34
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ protected AudioItem loadItemOnce(@NotNull AudioReference reference) {
}

log.debug("Attempting to load {} with client \"{}\"", reference.identifier, client.getIdentifier());
httpInterface.getContext().setAttribute(Client.OAUTH_CLIENT_ATTRIBUTE, client.supportsOAuth());

try {
AudioItem item = router.route(client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public boolean canHandleRequest(@NotNull String identifier) {
return (!identifier.contains("list=") || identifier.contains("list=RD")) && super.canHandleRequest(identifier);
}

@Override
public boolean supportsOAuth() {
return true;
}

@Override
@NotNull
public String getIdentifier() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* The interface for a Client.
*/
public interface Client {
String OAUTH_CLIENT_ATTRIBUTE = "yt-oauth-enabled-client";

String WATCH_URL = "https://www.youtube.com/watch?v=";
String API_BASE_URL = "https://youtubei.googleapis.com/youtubei/v1";
String PLAYER_URL = API_BASE_URL + "/player?prettyPrint=false";
Expand Down Expand Up @@ -208,6 +210,13 @@ default boolean isEmbedded() {
return false;
}

/**
* @return True, if this client supports account linking via OAuth (i.e. TV)
*/
default boolean supportsOAuth() {
return false;
}

void setPlaylistPageCount(int count);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.tools.http.HttpContextRetryCounter;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpClientTools;
import dev.lavalink.youtube.clients.skeleton.Client;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpUriRequest;
Expand Down Expand Up @@ -79,7 +80,12 @@ public void onRequest(HttpClientContext context,
context.removeAttribute(ATTRIBUTE_USER_AGENT_SPECIFIED);
}

oauth2Handler.applyToken(request);
boolean isRequestFromOauthedClient = context.getAttribute(Client.OAUTH_CLIENT_ATTRIBUTE) == Boolean.TRUE;

if (isRequestFromOauthedClient && Client.PLAYER_URL.equals(request.getURI().toString())) {
// only apply the token to /player requests.
oauth2Handler.applyToken(request);
}
}

// try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,6 @@ public void applyToken(HttpUriRequest request) {
return;
}

if (!Client.PLAYER_URL.equals(request.getURI().toString())) {
return;
}

if (shouldRefreshAccessToken()) {
log.debug("Access token has expired, refreshing...");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public void process(LocalAudioTrackExecutor localExecutor) throws Exception {
continue;
}

httpInterface.getContext().setAttribute(Client.OAUTH_CLIENT_ATTRIBUTE, client.supportsOAuth());

try {
processWithClient(localExecutor, httpInterface, client, 0);
return; // stream played through successfully, short-circuit.
Expand Down

0 comments on commit 8742c34

Please sign in to comment.