Skip to content

Commit a200fcf

Browse files
committed
code improvements
1 parent 67cbfd9 commit a200fcf

File tree

3 files changed

+34
-77
lines changed

3 files changed

+34
-77
lines changed

src/main/java/de/labystudio/spotifyapi/open/OpenSpotifyAPI.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ private void generateAccessTokenAsync(Consumer<AccessTokenResponse> callback) {
6161
});
6262
}
6363

64+
/**
65+
* Request server time of Spotify for time-based one time password
66+
*
67+
* @return server time in seconds
68+
*/
6469
public long requestServerTime() throws IOException {
6570
// Get server time
6671
URL url = new URL(URL_API_SERVER_TIME);
@@ -78,22 +83,33 @@ public long requestServerTime() throws IOException {
7883
return obj.get("serverTime").getAsLong();
7984
}
8085

86+
/**
87+
* Generate time-based one time password
88+
*
89+
* @param serverTime server time in seconds
90+
* @return 6 digits one time password
91+
*/
8192
public String generateTotp(long serverTime) {
93+
// Convert secret numbers to xor results
8294
StringBuilder xorResults = new StringBuilder();
8395
for (int i = 0; i < TOTP_SECRET.length; i++) {
8496
int result = TOTP_SECRET[i] ^ (i % 33 + 9);
8597
xorResults.append(result);
8698
}
99+
100+
// Convert xor results to hex
87101
StringBuilder hexResult = new StringBuilder();
88102
for (int i = 0; i < xorResults.length(); i++) {
89103
hexResult.append(String.format("%02x", (int) xorResults.charAt(i)));
90104
}
105+
106+
// Convert hex to byte array
91107
byte[] byteArray = new byte[hexResult.length() / 2];
92108
for (int i = 0; i < hexResult.length(); i += 2) {
93109
int byteValue = Integer.parseInt(hexResult.substring(i, i + 2), 16);
94110
byteArray[i / 2] = (byte) byteValue;
95111
}
96-
return TOTP.generateOtp(byteArray, serverTime);
112+
return TOTP.generateOtp(byteArray, serverTime, 30, 6);
97113
}
98114

99115
/**
@@ -116,10 +132,6 @@ private AccessTokenResponse generateAccessToken() throws IOException {
116132
return response;
117133
}
118134

119-
private boolean hasValidAccessToken(AccessTokenResponse response) {
120-
return response != null && response.accessToken != null && !response.accessToken.isEmpty();
121-
}
122-
123135
/**
124136
* Retrieve access token using totp
125137
*/
@@ -377,6 +389,10 @@ public <T> T request(String url, Class<?> clazz, boolean canGenerateNewAccessTok
377389
return GSON.fromJson(reader, clazz);
378390
}
379391

392+
private boolean hasValidAccessToken(AccessTokenResponse response) {
393+
return response != null && response.accessToken != null && !response.accessToken.isEmpty();
394+
}
395+
380396
public Cache<BufferedImage> getImageCache() {
381397
return this.imageCache;
382398
}

src/main/java/de/labystudio/spotifyapi/open/util/Base32.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/main/java/de/labystudio/spotifyapi/open/util/TOTP.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ public class TOTP {
1111

1212
private static final String DEFAULT_ALGORITHM = "HmacSHA1";
1313

14-
private static final int DEFAULT_DIGITS = 6;
15-
private static final int DEFAULT_PERIOD = 30;
16-
17-
public static String generateOtp(byte[] secret, long time) {
18-
long counter = time / DEFAULT_PERIOD;
14+
/**
15+
* Generate a TOTP (Time-based One-Time Password) using the given secret, time, period, and number of digits.
16+
*
17+
* @param secret The secret key
18+
* @param time The time in milliseconds
19+
* @param period The period in seconds
20+
* @param digits The number of digits
21+
* @return The generated TOTP
22+
*/
23+
public static String generateOtp(byte[] secret, long time, int period, int digits) {
24+
long counter = time / period;
1925

2026

2127
// Convert counter to byte array (Big Endian)
@@ -38,10 +44,10 @@ public static String generateOtp(byte[] secret, long time) {
3844
(hmac[offset + 3] & 0xFF);
3945

4046
// Compute OTP
41-
int otp = binary % ((int) Math.pow(10, DEFAULT_DIGITS));
47+
int otp = binary % ((int) Math.pow(10, digits));
4248

4349
// Return zero-padded OTP
44-
return String.format("%0" + DEFAULT_DIGITS + "d", otp);
50+
return String.format("%0" + digits + "d", otp);
4551
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
4652
throw new IllegalStateException("Failed to generate TOTP", e);
4753
}

0 commit comments

Comments
 (0)