1515
1616package com .pokegoapi .auth ;
1717
18- import POGOProtos .Networking .Envelopes .RequestEnvelopeOuterClass ;
1918import POGOProtos .Networking .Envelopes .RequestEnvelopeOuterClass .RequestEnvelope .AuthInfo ;
20-
2119import com .pokegoapi .exceptions .LoginFailedException ;
22- import com .pokegoapi .util . Log ;
20+ import com .pokegoapi .exceptions . RemoteServerException ;
2321import com .squareup .moshi .Moshi ;
24-
25- import lombok .Getter ;
2622import okhttp3 .Cookie ;
2723import okhttp3 .CookieJar ;
2824import okhttp3 .HttpUrl ;
3935
4036
4137public class PtcCredentialProvider extends CredentialProvider {
42- private static final String TAG = PtcCredentialProvider .class .getSimpleName ();
43-
4438 public static final String CLIENT_SECRET = "w8ScCUXJQc6kXKw8FiOhd8Fixzht18Dq3PEVkUCP5ZPxtgyWsbTvWHFLm2wNY0JR" ;
4539 public static final String REDIRECT_URI = "https://www.nianticlabs.com/pokemongo/error" ;
4640 public static final String CLIENT_ID = "mobile-app_pokemon-go" ;
47-
4841 public static final String API_URL = "https://pgorelease.nianticlabs.com/plfe/rpc" ;
4942 public static final String LOGIN_URL = "https://sso.pokemon.com/sso/login?service=https%3A%2F%2Fsso.pokemon.com%2Fsso%2Foauth2.0%2FcallbackAuthorize" ;
5043 public static final String LOGIN_OAUTH = "https://sso.pokemon.com/sso/oauth2.0/accessToken" ;
51-
5244 public static final String USER_AGENT = "niantic" ;
53-
45+ private static final String TAG = PtcCredentialProvider . class . getSimpleName ();
5446 //We try and refresh token 5 minutes before it actually expires
5547 private static final long REFRESH_TOKEN_BUFFER_TIME = 5 * 60 * 1000 ;
5648
5749 private final OkHttpClient client ;
58-
50+ private final String username ;
51+ private final String password ;
5952 private String tokenId ;
60-
6153 private long expiresTimestamp ;
62-
6354 private AuthInfo .Builder authbuilder ;
6455
65- private final String username ;
66- private final String password ;
67-
6856 /**
6957 * Instantiates a new Ptc login.
7058 *
71- * @param client the client
59+ * @param client the client
7260 * @param username Username
7361 * @param password password
7462 */
75- public PtcCredentialProvider (OkHttpClient client , String username , String password ) throws LoginFailedException {
63+ public PtcCredentialProvider (OkHttpClient client , String username , String password )
64+ throws LoginFailedException , RemoteServerException {
7665 this .username = username ;
7766 this .password = password ;
7867 /*
@@ -119,7 +108,7 @@ public Response intercept(Chain chain) throws IOException {
119108 * @param username PTC username
120109 * @param password PTC password
121110 */
122- private void login (String username , String password ) throws LoginFailedException {
111+ private void login (String username , String password ) throws LoginFailedException , RemoteServerException {
123112 //TODO: stop creating an okhttp client per request
124113 Request get = new Request .Builder ()
125114 .url (LOGIN_URL )
@@ -130,7 +119,7 @@ private void login(String username, String password) throws LoginFailedException
130119 try {
131120 getResponse = client .newCall (get ).execute ();
132121 } catch (IOException e ) {
133- throw new LoginFailedException ( e );
122+ throw new RemoteServerException ( "Failed to receive contents from server" , e );
134123 }
135124
136125 Moshi moshi = new Moshi .Builder ().build ();
@@ -140,7 +129,7 @@ private void login(String username, String password) throws LoginFailedException
140129 String response = getResponse .body ().string ();
141130 ptcAuth = moshi .adapter (PtcAuthJson .class ).fromJson (response );
142131 } catch (IOException e ) {
143- throw new LoginFailedException ("Looks like the servers are down" , e );
132+ throw new RemoteServerException ("Looks like the servers are down" , e );
144133 }
145134
146135 HttpUrl url = HttpUrl .parse (LOGIN_URL ).newBuilder ()
@@ -168,22 +157,22 @@ private void login(String username, String password) throws LoginFailedException
168157 .newCall (postRequest )
169158 .execute ();
170159 } catch (IOException e ) {
171- throw new LoginFailedException ("Network failure" , e );
160+ throw new RemoteServerException ("Network failure" , e );
172161 }
173162
174163 String body = null ;
175164 try {
176165 body = response .body ().string ();
177166 } catch (IOException e ) {
178- throw new LoginFailedException ("Response body fetching failed" , e );
167+ throw new RemoteServerException ("Response body fetching failed" , e );
179168 }
180169
181170 if (body .length () > 0 ) {
182171 PtcError ptcError = null ;
183172 try {
184173 ptcError = moshi .adapter (PtcError .class ).fromJson (body );
185174 } catch (IOException e ) {
186- throw new LoginFailedException ( "Unmarshling failure" , e );
175+ throw new RemoteServerException ( "Unmarshalling failure" , e );
187176 }
188177 if (ptcError .getError () != null && ptcError .getError ().length () > 0 ) {
189178 throw new LoginFailedException (ptcError .getError ());
@@ -211,13 +200,13 @@ private void login(String username, String password) throws LoginFailedException
211200 try {
212201 response = client .newCall (postRequest ).execute ();
213202 } catch (IOException e ) {
214- throw new LoginFailedException ("Network Failure " , e );
203+ throw new RemoteServerException ("Network Failure " , e );
215204 }
216205
217206 try {
218207 body = response .body ().string ();
219208 } catch (IOException e ) {
220- throw new LoginFailedException ( e );
209+ throw new RemoteServerException ( "Network failure" , e );
221210 }
222211
223212 String [] params ;
@@ -232,7 +221,7 @@ private void login(String username, String password) throws LoginFailedException
232221 }
233222
234223 @ Override
235- public String getTokenId () throws LoginFailedException {
224+ public String getTokenId () throws LoginFailedException , RemoteServerException {
236225 if (isTokenIdExpired ()) {
237226 login (username , password );
238227 }
@@ -246,7 +235,7 @@ public String getTokenId() throws LoginFailedException {
246235 * @throws LoginFailedException when refreshing fails
247236 */
248237 @ Override
249- public AuthInfo getAuthInfo () throws LoginFailedException {
238+ public AuthInfo getAuthInfo () throws LoginFailedException , RemoteServerException {
250239 if (isTokenIdExpired ()) {
251240 login (username , password );
252241 }
0 commit comments