@@ -75,7 +75,12 @@ private static int readBlobInt(ByteBuffer buffer) {
7575 return lo & 0x7f | hi << 7 ;
7676 }
7777
78- private void connect () throws IOException , GeneralSecurityException , SpotifyAuthenticationException {
78+ @ NotNull
79+ static Session from (@ NotNull Inner inner ) throws IOException {
80+ return new Session (inner , ApResolver .getSocketFromRandomAccessPoint ());
81+ }
82+
83+ void connect () throws IOException , GeneralSecurityException , SpotifyAuthenticationException {
7984 Accumulator acc = new Accumulator ();
8085
8186 // Send ClientHello
@@ -191,7 +196,7 @@ private void connect() throws IOException, GeneralSecurityException, SpotifyAuth
191196 LOGGER .info ("Connected successfully!" );
192197 }
193198
194- private void authenticate (@ NotNull Authentication .LoginCredentials credentials ) throws IOException , GeneralSecurityException , SpotifyAuthenticationException , MercuryClient .PubSubException , SpotifyIrc .IrcException {
199+ void authenticate (@ NotNull Authentication .LoginCredentials credentials ) throws IOException , GeneralSecurityException , SpotifyAuthenticationException , MercuryClient .PubSubException , SpotifyIrc .IrcException {
195200 if (cipherPair == null ) throw new IllegalStateException ("Connection not established!" );
196201
197202 Authentication .ClientResponseEncrypted clientResponseEncrypted = Authentication .ClientResponseEncrypted .newBuilder ()
@@ -300,6 +305,10 @@ public Authentication.APWelcome apWelcome() {
300305 return apWelcome ;
301306 }
302307
308+ public boolean valid () {
309+ return apWelcome != null && !socket .isClosed ();
310+ }
311+
303312 @ NotNull
304313 public String deviceId () {
305314 return inner .deviceId ;
@@ -360,6 +369,19 @@ private Inner(DeviceType deviceType, String deviceName, AbsConfiguration configu
360369 this .deviceId = UUID .randomUUID ().toString ();
361370 }
362371
372+ @ NotNull
373+ static Inner from (@ NotNull AbsConfiguration configuration ) {
374+ String deviceName = configuration .deviceName ();
375+ if (deviceName == null || deviceName .isEmpty ())
376+ throw new IllegalArgumentException ("Device name required: " + deviceName );
377+
378+ DeviceType deviceType = configuration .deviceType ();
379+ if (deviceType == null )
380+ throw new IllegalArgumentException ("Device type required!" );
381+
382+ return new Inner (deviceType , deviceName , configuration );
383+ }
384+
363385 @ NotNull Authentication .LoginCredentials decryptBlob (String username , byte [] encryptedBlob ) throws GeneralSecurityException , IOException {
364386 encryptedBlob = Base64 .getDecoder ().decode (encryptedBlob );
365387
@@ -408,28 +430,18 @@ public static class Builder {
408430 private final Inner inner ;
409431 private Authentication .LoginCredentials loginCredentials = null ;
410432 private AuthConfiguration authConf ;
411- private ZeroconfAuthenticator .Configuration zeroconfConf ;
412433
413434 public Builder (@ NotNull DeviceType deviceType , @ NotNull String deviceName , @ NotNull AbsConfiguration configuration ) {
414435 this .inner = new Inner (deviceType , deviceName , configuration );
415436 this .authConf = configuration ;
416- this .zeroconfConf = configuration ;
417437 }
418438
419439 public Builder (@ NotNull AbsConfiguration configuration ) {
420- String deviceName = configuration .deviceName ();
421- if (deviceName == null || deviceName .isEmpty ())
422- throw new IllegalArgumentException ("Device name required: " + deviceName );
423-
424- DeviceType deviceType = configuration .deviceType ();
425- if (deviceType == null )
426- throw new IllegalArgumentException ("Device type required!" );
427-
428- this .inner = new Inner (deviceType , deviceName , configuration );
440+ this .inner = Inner .from (configuration );
429441 this .authConf = configuration ;
430- this .zeroconfConf = configuration ;
431442 }
432443
444+ @ NotNull
433445 public Builder facebook () throws IOException {
434446 try (FacebookAuthenticator authenticator = new FacebookAuthenticator ()) {
435447 loginCredentials = authenticator .lockUntilCredentials ();
@@ -439,20 +451,13 @@ public Builder facebook() throws IOException {
439451 }
440452 }
441453
442- public Builder zeroconf () throws IOException {
443- try (ZeroconfAuthenticator authenticator = new ZeroconfAuthenticator (inner , zeroconfConf )) {
444- loginCredentials = authenticator .lockUntilCredentials ();
445- return this ;
446- } catch (InterruptedException ex ) {
447- throw new IOException (ex );
448- }
449- }
450-
454+ @ NotNull
451455 public Builder blob (String username , byte [] blob ) throws GeneralSecurityException , IOException {
452456 loginCredentials = inner .decryptBlob (username , blob );
453457 return this ;
454458 }
455459
460+ @ NotNull
456461 public Builder userPass (@ NotNull String username , @ NotNull String password ) {
457462 loginCredentials = Authentication .LoginCredentials .newBuilder ()
458463 .setUsername (username )
@@ -466,36 +471,35 @@ public Builder userPass(@NotNull String username, @NotNull String password) {
466471 public Session create () throws IOException , GeneralSecurityException , SpotifyAuthenticationException , MercuryClient .PubSubException , SpotifyIrc .IrcException {
467472 if (loginCredentials == null ) {
468473 if (authConf != null ) {
469- String blob = authConf .blob ();
470- String username = authConf .username ();
471- String password = authConf .password ();
474+ String blob = authConf .authBlob ();
475+ String username = authConf .authUsername ();
476+ String password = authConf .authPassword ();
472477
473- switch (authConf .strategy ()) {
478+ switch (authConf .authStrategy ()) {
474479 case FACEBOOK :
475480 facebook ();
476481 break ;
477482 case BLOB :
478- if (username == null ) throw new IllegalArgumentException ("Missing username !" );
479- if (blob == null ) throw new IllegalArgumentException ("Missing blob !" );
483+ if (username == null ) throw new IllegalArgumentException ("Missing authUsername !" );
484+ if (blob == null ) throw new IllegalArgumentException ("Missing authBlob !" );
480485 blob (username , Base64 .getDecoder ().decode (blob ));
481486 break ;
482487 case USER_PASS :
483- if (username == null ) throw new IllegalArgumentException ("Missing username !" );
484- if (password == null ) throw new IllegalArgumentException ("Missing password !" );
488+ if (username == null ) throw new IllegalArgumentException ("Missing authUsername !" );
489+ if (password == null ) throw new IllegalArgumentException ("Missing authPassword !" );
485490 userPass (username , password );
486491 break ;
487492 case ZEROCONF :
488- zeroconf ();
489- break ;
493+ throw new IllegalStateException ("Cannot handle ZEROCONF! Use ZeroconfServer." );
490494 default :
491- throw new IllegalStateException ("Unknown auth strategy : " + authConf .strategy ());
495+ throw new IllegalStateException ("Unknown auth authStrategy : " + authConf .authStrategy ());
492496 }
493497 } else {
494498 throw new IllegalStateException ("Missing credentials!" );
495499 }
496500 }
497501
498- Session session = new Session ( inner , ApResolver . getSocketFromRandomAccessPoint () );
502+ Session session = Session . from ( inner );
499503 session .connect ();
500504 session .authenticate (loginCredentials );
501505 return session ;
0 commit comments