Skip to content

Commit

Permalink
Update Last.fm auth callback & API key/shared secret
Browse files Browse the repository at this point in the history
the new API key/shared secret was so I could add
the new description to the browser sign in page
  • Loading branch information
Pixaurora committed Sep 10, 2024
1 parent 209429f commit 85b3fde
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 17 deletions.
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
org.gradle.jvmargs = -Xmx4G
org.gradle.caching = true
org.gradle.parallel = true
org.gradle.configureondemand = true

# Mod Properties
mod_version = 0.3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import net.pixaurora.kitten_heart.impl.network.XMLHelper;

public class LastFMScrobbler implements Scrobbler {
public static final String API_KEY = "693bf5425eb442ceaf6f627993c7918d";
public static final String SHARED_SECRET = "9920afdfeba7ec08b3dc966f9d603cd5";
public static final String API_KEY = "6f9e533b5f6631a5aa3070f5e757de8c";
public static final String SHARED_SECRET = "97fbf9a3d76ba36dfb5a2f6c3215bf49";

public static final String ROOT_API_URL = "http://ws.audioscrobbler.com/2.0/";
public static final String SETUP_URL = "https://last.fm/api/auth?api_key=" + API_KEY;

public static final ScrobblerType<LastFMScrobbler> TYPE = new ScrobblerType<>("last.fm", LastFMScrobbler.class,
public static final ScrobblerType<LastFMScrobbler> TYPE = new ScrobblerType<>("last.fm.new", LastFMScrobbler.class,
SETUP_URL, LastFMScrobbler::setup);

private final LastFMSession session;
Expand All @@ -51,14 +51,14 @@ public void startScrobbling(ScrobbleInfo track) throws KitTunesException {

args.put("artist", track.artistTitle());
args.put("track", track.trackTitle());
args.put("api_key", API_KEY);
args.put("api_key", this.apiKey());
args.put("sk", this.session.key);

if (track.albumTitle().isPresent()) {
args.put("album", track.albumTitle().get());
}

this.handleScrobbling(addSignature(args));
this.handleScrobbling(this.addSignature(args));
}

@Override
Expand All @@ -70,15 +70,15 @@ public void completeScrobbling(ScrobbleInfo track) throws KitTunesException {
args.put("artist", track.artistTitle());
args.put("track", track.trackTitle());
args.put("timestamp", String.valueOf(track.startTime().getEpochSecond()));
args.put("api_key", API_KEY);
args.put("api_key", this.apiKey());
args.put("sk", this.session.key);

Optional<String> albumTitle = track.albumTitle();
if (albumTitle.isPresent()) {
args.put("album", albumTitle.get());
}

this.handleScrobbling(addSignature(args));
this.handleScrobbling(this.addSignature(args));
}

private void handleScrobbling(Map<String, String> args) throws KitTunesException {
Expand All @@ -87,7 +87,11 @@ private void handleScrobbling(Map<String, String> args) throws KitTunesException
UnhandledKitTunesException.runOrThrow(() -> HttpHelper.logResponse(responseBody));
}

private static Map<String, String> addSignature(Map<String, String> parameters) {
private Map<String, String> addSignature(Map<String, String> parameters) {
return addSignature(parameters, this.sharedSecret());
}

private static Map<String, String> addSignature(Map<String, String> parameters, String sharedSecret) {
List<Map.Entry<String, String>> sortedParameters = parameters.entrySet().stream()
.sorted(Comparator.comparing(entry -> entry.getKey())).collect(Collectors.toList());

Expand All @@ -96,7 +100,7 @@ private static Map<String, String> addSignature(Map<String, String> parameters)
regularSignature += parameter.getKey() + parameter.getValue();
}

regularSignature += SHARED_SECRET;
regularSignature += sharedSecret;

parameters = new HashMap<>(parameters);
parameters.put("api_sig", Encryption.signMd5(regularSignature));
Expand All @@ -111,7 +115,7 @@ private static LastFMSession createSession(String token) throws ScrobblerParsing
args.put("api_key", API_KEY);
args.put("token", token);

InputStream responseBody = HttpHelper.get(ROOT_API_URL, addSignature(args));
InputStream responseBody = HttpHelper.get(ROOT_API_URL, addSignature(args, SHARED_SECRET));

Document body = XMLHelper.getDocument(responseBody);

Expand All @@ -125,6 +129,14 @@ public ScrobblerType<?> type() {
return TYPE;
}

protected String apiKey() {
return API_KEY;
}

protected String sharedSecret() {
return SHARED_SECRET;
}

public static class LastFMSession {
private final String name;
private final String key;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.pixaurora.kitten_heart.impl.scrobble;

public class LegacyLastFMScrobbler extends LastFMScrobbler {
public static final String API_KEY = "693bf5425eb442ceaf6f627993c7918d";
public static final String SHARED_SECRET = "9920afdfeba7ec08b3dc966f9d603cd5";

public static final ScrobblerType<LegacyLastFMScrobbler> TYPE = new ScrobblerType<>("last.fm",
LegacyLastFMScrobbler.class, null, null);

public LegacyLastFMScrobbler(LastFMSession session) {
super(session);
}

@Override
protected String apiKey() {
return API_KEY;
}

@Override
protected String sharedSecret() {
return SHARED_SECRET;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public interface Scrobbler extends SimpleScrobbler, SpecifiesType<Scrobbler> {
public static final DispatchGroup<Scrobbler, ScrobblerType<? extends Scrobbler>> TYPES = new DispatchGroup<>(
"scrobbler", Arrays.asList(LastFMScrobbler.TYPE));
"scrobbler", Arrays.asList(LastFMScrobbler.TYPE, LegacyLastFMScrobbler.TYPE));

public String username() throws KitTunesException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ pub extern "system" fn Java_net_pixaurora_kitten_1thoughts_impl_http_server_Serv
object: JObject<'local>,
) -> () {
if let Err(error) = drop(&object, &mut env) {
error.throw(&mut env);
panic!("Couldn't drop server due to an error! {}", error);
}
}
5 changes: 3 additions & 2 deletions projects/kitten-thoughts/src/main/rust/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ pub type Result<T> = core::result::Result<T, Error>;

impl Error {
pub fn throw(&self, env: &mut JNIEnv) {
env.throw_new("java/io/IOException", self.to_string())
.unwrap();
if let Err(error) = env.throw_new("java/io/IOException", self.to_string()) {
eprintln!("Couldn't throw exception due to an error! {}", error);
}
}
}

Expand Down
18 changes: 15 additions & 3 deletions projects/kitten-thoughts/src/main/rust/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,21 @@ pub struct Server {
shutdown: Option<Shutdown>,
}

#[get("/?<token>")]
async fn handle_received_token(sender: &Sender<String>, token: String) {
if let Err(error) = sender.send(token).await {
eprintln!("Couldn't send token due to an error! {}", error);
}
}

#[get("/auth/callback/generic?<code>")]
async fn receive_generic_token(sender: &State<Sender<String>>, code: String) -> RawHtml<&str> {
handle_received_token(sender.inner(), code).await;
RawHtml(RESPONSE)
}

#[get("/auth/callback/lastfm?<token>")]
async fn receive_lastfm_token(sender: &State<Sender<String>>, token: String) -> RawHtml<&str> {
sender.inner().send(token).await.unwrap();
handle_received_token(sender.inner(), token).await;
RawHtml(RESPONSE)
}

Expand All @@ -42,7 +54,7 @@ impl Server {
..Config::release_default()
})
.manage(sender)
.mount("/", routes![receive_lastfm_token]);
.mount("/", routes![receive_generic_token, receive_lastfm_token]);

let server = server.ignite().await?;
self.shutdown = Some(server.shutdown());
Expand Down

0 comments on commit 85b3fde

Please sign in to comment.