@@ -217,7 +217,6 @@ public void close() throws IOException {
217217
218218 public void closeSession () throws IOException {
219219 if (session != null ) session .close ();
220-
221220 session = null ;
222221 }
223222
@@ -279,6 +278,12 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
279278 synchronized (connectionLock ) {
280279 if (username .equals (connectingUsername )) {
281280 LOGGER .info ("{} is already trying to connect." , username );
281+
282+ out .write (httpVersion .getBytes ());
283+ out .write (" 403 Forbidden" .getBytes ()); // I don't think this is the Spotify way
284+ out .write (EOL );
285+ out .write (EOL );
286+ out .flush ();
282287 return ;
283288 }
284289 }
@@ -310,13 +315,25 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
310315
311316 if (!Arrays .equals (mac , checksum )) {
312317 LOGGER .fatal ("Mac and checksum don't match!" );
318+
319+ out .write (httpVersion .getBytes ());
320+ out .write (" 400 Bad Request" .getBytes ()); // I don't think this is the Spotify way
321+ out .write (EOL );
322+ out .write (EOL );
323+ out .flush ();
313324 return ;
314325 }
315326
316327 Cipher aes = Cipher .getInstance ("AES/CTR/NoPadding" );
317328 aes .init (Cipher .DECRYPT_MODE , new SecretKeySpec (Arrays .copyOfRange (encryptionKey , 0 , 16 ), "AES" ), new IvParameterSpec (iv ));
318329 byte [] decrypted = aes .doFinal (encrypted );
319330
331+ try {
332+ closeSession ();
333+ } catch (IOException ex ) {
334+ LOGGER .warn ("Failed closing previous session." , ex );
335+ }
336+
320337 try {
321338 synchronized (connectionLock ) {
322339 connectingUsername = username ;
@@ -327,13 +344,6 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
327344 session = Session .from (inner );
328345 LOGGER .info ("Accepted new user from {}. {deviceId: {}}" , params .get ("deviceName" ), session .deviceId ());
329346
330- session .connect ();
331- session .authenticate (credentials );
332-
333- synchronized (connectionLock ) {
334- connectingUsername = null ;
335- }
336-
337347 // Sending response
338348 String resp = DEFAULT_SUCCESSFUL_ADD_USER .toString ();
339349 out .write (httpVersion .getBytes ());
@@ -348,6 +358,14 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
348358 out .write (resp .getBytes ());
349359 out .flush ();
350360
361+
362+ session .connect ();
363+ session .authenticate (credentials );
364+
365+ synchronized (connectionLock ) {
366+ connectingUsername = null ;
367+ }
368+
351369 sessionListeners .forEach (l -> l .sessionChanged (session ));
352370 } catch (Session .SpotifyAuthenticationException | MercuryClient .MercuryException ex ) {
353371 LOGGER .fatal ("Couldn't establish a new session." , ex );
0 commit comments