Skip to content

Commit

Permalink
Fix my siliness and provide API consumers a way to provide their refr…
Browse files Browse the repository at this point in the history
…esh token when using static auth
  • Loading branch information
rajulbhatnagar committed Jul 23, 2016
1 parent ccf016d commit 7ccf228
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/main/java/com/pokegoapi/auth/GoogleLogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package com.pokegoapi.auth;

import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo;

import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.util.Log;
import com.squareup.moshi.Moshi;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand All @@ -40,6 +42,7 @@ public GoogleLogin(OkHttpClient client) {

/**
* Given the refresh token fetches a new access token and returns AuthInfo.
*
* @param refreshToken Refresh token returned during initial login
* @return Refreshed AuthInfo object
* @throws IOException If the network call fails
Expand All @@ -48,8 +51,8 @@ public AuthInfo refreshToken(String refreshToken) throws IOException {
HttpUrl url = HttpUrl.parse(GoogleLoginSecrets.OAUTH_TOKEN_ENDPOINT).newBuilder()
.addQueryParameter("client_id", GoogleLoginSecrets.CLIENT_ID)
.addQueryParameter("client_secret", GoogleLoginSecrets.SECRET)
.addQueryParameter("refreshToken", refreshToken)
.addQueryParameter("grant_type", "refreshToken")
.addQueryParameter("refresh_token", refreshToken)
.addQueryParameter("grant_type", "refresh_token")
.build();
//Empty request body
RequestBody reqBody = RequestBody.create(null, new byte[0]);
Expand All @@ -59,12 +62,12 @@ public AuthInfo refreshToken(String refreshToken) throws IOException {
.build();

Response response = client.newCall(request).execute();

Moshi moshi = new Moshi.Builder().build();
GoogleAuthTokenJson token = moshi.adapter(GoogleAuthTokenJson.class).fromJson(response.body().string());
if (token.getError() == null) {
if (token.getError() != null) {
return null;
} else {
Log.d(TAG, "Refreshed Token " + token.getIdToken());
AuthInfo.Builder builder = AuthInfo.newBuilder();
builder.setProvider("google");
builder.setToken(AuthInfo.JWT.newBuilder().setContents(token.getIdToken()).setUnknown2(59).build());
Expand All @@ -85,15 +88,28 @@ public AuthInfo login(String token) {
return builder.build();
}

/**
* Returns an AuthInfo object given a token, this should not be an access token but rather an id_token.
*
* @param token the id_token stored from a previous oauth attempt.
* @param refreshToken Let app provide refresh token if they have persisted it
* @return AuthInfo a AuthInfo proto structure to be encapsulated in server requests
*/
public AuthInfo login(String token, String refreshToken) {
GoogleLoginSecrets.refresh_token = refreshToken;
AuthInfo.Builder builder = AuthInfo.newBuilder();
builder.setProvider("google");
builder.setToken(AuthInfo.JWT.newBuilder().setContents(token).setUnknown2(59).build());
return builder.build();
}

/**
* Starts a login flow for google using a username and password, this uses googles device oauth endpoint,
* a URL and code is displayed, not really ideal right now.
*
* @param username Google username
* @param password Google password
* @return AuthInfo a AuthInfo proto structure to be encapsulated in server requests
*/
public AuthInfo login(String username, String password) throws LoginFailedException {
public AuthInfo login() throws LoginFailedException {
try {
HttpUrl url = HttpUrl.parse(GoogleLoginSecrets.OAUTH_ENDPOINT).newBuilder()
.addQueryParameter("client_id", GoogleLoginSecrets.CLIENT_ID)
Expand Down

0 comments on commit 7ccf228

Please sign in to comment.