Skip to content

Commit bd2fc89

Browse files
committed
Authenticate from configuration file
1 parent 74ce09e commit bd2fc89

6 files changed

Lines changed: 129 additions & 7 deletions

File tree

conf.properties

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
# Configuration!
1+
## Device name
22
deviceName=librespot-java
3-
deviceType=Computer
3+
## Device type (Computer, Tablet, Smartphone, Speaker, TV, AVR, STB, AudioDongle, Unknown)
4+
deviceType=Computer
5+
# Authentication
6+
## Strategy (userPass, zeroconf, blob, facebook)
7+
auth.strategy=zeroconf
8+
## Spotify username (blob, userPass)
9+
auth.username=
10+
## Spotify password (userPass)
11+
auth.password=
12+
## Spotify authentication blob (blob)
13+
auth.blob=

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package xyz.gianlu.librespot;
22

33
import org.jetbrains.annotations.Nullable;
4+
import xyz.gianlu.librespot.core.AuthConfiguration;
45
import xyz.gianlu.librespot.core.Session;
56
import xyz.gianlu.librespot.player.CacheManager;
67
import xyz.gianlu.librespot.player.Player;
78

89
/**
910
* @author Gianlu
1011
*/
11-
public abstract class AbsConfiguration implements Player.PlayerConfiguration, CacheManager.CacheConfiguration {
12+
public abstract class AbsConfiguration implements Player.PlayerConfiguration, CacheManager.CacheConfiguration, AuthConfiguration {
1213

1314
@Nullable
1415
public abstract String deviceName();

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package xyz.gianlu.librespot;
22

33
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
45
import xyz.gianlu.librespot.core.Session;
56
import xyz.gianlu.librespot.player.TrackHandler;
67

@@ -61,4 +62,25 @@ public String deviceName() {
6162
public Session.DeviceType deviceType() {
6263
return Session.DeviceType.Computer;
6364
}
65+
66+
@Override
67+
public @Nullable String username() {
68+
return null;
69+
}
70+
71+
@Override
72+
public @Nullable String password() {
73+
return null;
74+
}
75+
76+
@Override
77+
public @Nullable String blob() {
78+
return null;
79+
}
80+
81+
@NotNull
82+
@Override
83+
public Strategy strategy() {
84+
return Strategy.ZEROCONF;
85+
}
6486
}

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package xyz.gianlu.librespot;
22

3+
import org.jetbrains.annotations.Contract;
34
import org.jetbrains.annotations.NotNull;
45
import org.jetbrains.annotations.Nullable;
56
import xyz.gianlu.librespot.core.Session;
@@ -39,6 +40,18 @@ private float getFloat(@NotNull String key, float fallback) {
3940
}
4041
}
4142

43+
@Contract("_, _, !null -> !null")
44+
private <E extends Enum<E>> E getEnum(@NotNull Class<E> clazz, @NotNull String key, @Nullable E fallback) {
45+
String val = properties.getProperty(key, null);
46+
if (val == null) return fallback;
47+
48+
try {
49+
return Enum.valueOf(clazz, val);
50+
} catch (RuntimeException ex) {
51+
return fallback;
52+
}
53+
}
54+
4255
@Override
4356
public boolean cacheEnabled() {
4457
return getBoolean("cache.enabled", defaults.cacheEnabled());
@@ -76,8 +89,27 @@ public float normalisationPregain() {
7689

7790
@Override
7891
public @Nullable Session.DeviceType deviceType() {
79-
String val = properties.getProperty("deviceType", null);
80-
if (val == null) return null;
81-
return Session.DeviceType.valueOf(val);
92+
return getEnum(Session.DeviceType.class, "deviceType", null);
93+
}
94+
95+
@Override
96+
public @Nullable String username() {
97+
return properties.getProperty("auth.username", null);
98+
}
99+
100+
@Override
101+
public @Nullable String password() {
102+
return properties.getProperty("auth.password", null);
103+
}
104+
105+
@Override
106+
public @Nullable String blob() {
107+
return properties.getProperty("auth.blob", null);
108+
}
109+
110+
@NotNull
111+
@Override
112+
public Strategy strategy() {
113+
return getEnum(Strategy.class, "auth.strategy", defaults.strategy());
82114
}
83115
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package xyz.gianlu.librespot.core;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
/**
7+
* @author Gianlu
8+
*/
9+
public interface AuthConfiguration {
10+
@Nullable
11+
String username();
12+
13+
@Nullable
14+
String password();
15+
16+
@Nullable
17+
String blob();
18+
19+
@NotNull
20+
Strategy strategy();
21+
22+
enum Strategy {
23+
FACEBOOK, BLOB,
24+
USER_PASS, ZEROCONF
25+
}
26+
}

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ private Inner(DeviceType deviceType, String deviceName, AbsConfiguration configu
399399
public static class Builder {
400400
private final Inner inner;
401401
private Authentication.LoginCredentials loginCredentials = null;
402+
private AuthConfiguration authConf;
402403

403404
public Builder(@NotNull DeviceType deviceType, @NotNull String deviceName, @NotNull AbsConfiguration configuration) {
404405
this.inner = new Inner(deviceType, deviceName, configuration);
@@ -414,6 +415,7 @@ public Builder(@NotNull AbsConfiguration configuration) {
414415
throw new IllegalArgumentException("Device type required!");
415416

416417
this.inner = new Inner(deviceType, deviceName, configuration);
418+
this.authConf = configuration;
417419
}
418420

419421
public Builder facebook() throws IOException {
@@ -450,7 +452,36 @@ public Builder userPass(@NotNull String username, @NotNull String password) {
450452

451453
@NotNull
452454
public Session create() throws IOException, GeneralSecurityException, SpotifyAuthenticationException, MercuryClient.PubSubException, SpotifyIrc.IrcException {
453-
if (loginCredentials == null) throw new IllegalStateException("Missing credentials!");
455+
if (loginCredentials == null) {
456+
if (authConf != null) {
457+
String blob = authConf.blob();
458+
String username = authConf.username();
459+
String password = authConf.password();
460+
461+
switch (authConf.strategy()) {
462+
case FACEBOOK:
463+
facebook();
464+
break;
465+
case BLOB:
466+
if (username == null) throw new IllegalArgumentException("Missing username!");
467+
if (blob == null) throw new IllegalArgumentException("Missing blob!");
468+
blob(username, Base64.getDecoder().decode(blob));
469+
break;
470+
case USER_PASS:
471+
if (username == null) throw new IllegalArgumentException("Missing username!");
472+
if (password == null) throw new IllegalArgumentException("Missing password!");
473+
userPass(username, password);
474+
break;
475+
case ZEROCONF:
476+
zeroconf();
477+
break;
478+
default:
479+
throw new IllegalStateException("Unknown auth strategy: " + authConf.strategy());
480+
}
481+
} else {
482+
throw new IllegalStateException("Missing credentials!");
483+
}
484+
}
454485

455486
Session session = new Session(inner, ApResolver.getSocketFromRandomAccessPoint());
456487
session.connect();

0 commit comments

Comments
 (0)