|
1 | 1 | package xyz.gianlu.librespot.core; |
2 | 2 |
|
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.JsonParser; |
3 | 5 | import com.google.protobuf.ByteString; |
4 | 6 | import com.spotify.Authentication; |
5 | 7 | import com.spotify.Keyexchange; |
@@ -353,6 +355,22 @@ private void authenticatePartial(@NotNull Authentication.LoginCredentials creden |
353 | 355 | authLock.notifyAll(); |
354 | 356 | } |
355 | 357 | } |
| 358 | + |
| 359 | + if (conf().storeCredentials()) { |
| 360 | + ByteString reusable = apWelcome.getReusableAuthCredentials(); |
| 361 | + Authentication.AuthenticationType reusableType = apWelcome.getReusableAuthCredentialsType(); |
| 362 | + |
| 363 | + JsonObject obj = new JsonObject(); |
| 364 | + obj.addProperty("username", apWelcome.getCanonicalUsername()); |
| 365 | + obj.addProperty("credentials", Utils.toBase64(reusable)); |
| 366 | + obj.addProperty("type", reusableType.name()); |
| 367 | + |
| 368 | + File storeFile = conf().credentialsFile(); |
| 369 | + if (storeFile == null) throw new IllegalArgumentException(); |
| 370 | + try (FileOutputStream out = new FileOutputStream(storeFile)) { |
| 371 | + out.write(obj.toString().getBytes()); |
| 372 | + } |
| 373 | + } |
356 | 374 | } else if (packet.is(Packet.Type.AuthFailure)) { |
357 | 375 | throw new SpotifyAuthenticationException(Keyexchange.APLoginFailed.parseFrom(packet.payload)); |
358 | 376 | } else { |
@@ -773,33 +791,41 @@ public Builder userPass(@NotNull String username, @NotNull String password) { |
773 | 791 | */ |
774 | 792 | @NotNull |
775 | 793 | public Session create() throws IOException, GeneralSecurityException, SpotifyAuthenticationException, MercuryClient.MercuryException { |
| 794 | + if (authConf.storeCredentials()) { |
| 795 | + File storeFile = authConf.credentialsFile(); |
| 796 | + if (storeFile != null && storeFile.exists()) { |
| 797 | + JsonObject obj = JsonParser.parseReader(new FileReader(storeFile)).getAsJsonObject(); |
| 798 | + loginCredentials = Authentication.LoginCredentials.newBuilder() |
| 799 | + .setTyp(Authentication.AuthenticationType.valueOf(obj.get("type").getAsString())) |
| 800 | + .setUsername(obj.get("username").getAsString()) |
| 801 | + .setAuthData(Utils.fromBase64(obj.get("credentials").getAsString())) |
| 802 | + .build(); |
| 803 | + } |
| 804 | + } |
| 805 | + |
776 | 806 | if (loginCredentials == null) { |
777 | | - if (authConf != null) { |
778 | | - String blob = authConf.authBlob(); |
779 | | - String username = authConf.authUsername(); |
780 | | - String password = authConf.authPassword(); |
781 | | - |
782 | | - switch (authConf.authStrategy()) { |
783 | | - case FACEBOOK: |
784 | | - facebook(); |
785 | | - break; |
786 | | - case BLOB: |
787 | | - if (username == null) throw new IllegalArgumentException("Missing authUsername!"); |
788 | | - if (blob == null) throw new IllegalArgumentException("Missing authBlob!"); |
789 | | - blob(username, Base64.getDecoder().decode(blob)); |
790 | | - break; |
791 | | - case USER_PASS: |
792 | | - if (username == null) throw new IllegalArgumentException("Missing authUsername!"); |
793 | | - if (password == null) throw new IllegalArgumentException("Missing authPassword!"); |
794 | | - userPass(username, password); |
795 | | - break; |
796 | | - case ZEROCONF: |
797 | | - throw new IllegalStateException("Cannot handle ZEROCONF! Use ZeroconfServer."); |
798 | | - default: |
799 | | - throw new IllegalStateException("Unknown auth authStrategy: " + authConf.authStrategy()); |
800 | | - } |
801 | | - } else { |
802 | | - throw new IllegalStateException("Missing credentials!"); |
| 807 | + String blob = authConf.authBlob(); |
| 808 | + String username = authConf.authUsername(); |
| 809 | + String password = authConf.authPassword(); |
| 810 | + |
| 811 | + switch (authConf.authStrategy()) { |
| 812 | + case FACEBOOK: |
| 813 | + facebook(); |
| 814 | + break; |
| 815 | + case BLOB: |
| 816 | + if (username == null) throw new IllegalArgumentException("Missing authUsername!"); |
| 817 | + if (blob == null) throw new IllegalArgumentException("Missing authBlob!"); |
| 818 | + blob(username, Base64.getDecoder().decode(blob)); |
| 819 | + break; |
| 820 | + case USER_PASS: |
| 821 | + if (username == null) throw new IllegalArgumentException("Missing authUsername!"); |
| 822 | + if (password == null) throw new IllegalArgumentException("Missing authPassword!"); |
| 823 | + userPass(username, password); |
| 824 | + break; |
| 825 | + case ZEROCONF: |
| 826 | + throw new IllegalStateException("Cannot handle ZEROCONF! Use ZeroconfServer."); |
| 827 | + default: |
| 828 | + throw new IllegalStateException("Unknown auth authStrategy: " + authConf.authStrategy()); |
803 | 829 | } |
804 | 830 | } |
805 | 831 |
|
|
0 commit comments