Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
package com.github.andlyticsproject.console.v2;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AuthenticatorException;
Expand All @@ -31,7 +20,17 @@
import com.github.andlyticsproject.console.AuthenticationException;
import com.github.andlyticsproject.console.NetworkException;
import com.github.andlyticsproject.model.DeveloperConsoleAccount;
import com.github.andlyticsproject.util.FileUtils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.List;

public class AccountManagerAuthenticator extends BaseAuthenticator {

Expand Down Expand Up @@ -208,14 +207,14 @@ private SessionCredentials authenticateInternal(Activity activity, boolean inval

DeveloperConsoleAccount[] developerAccounts = findDeveloperAccounts(responseStr);
if (developerAccounts == null) {
debugAuthFailure(activity, responseStr);
debugAuthFailure(activity, responseStr, webloginUrl);

throw new AuthenticationException("Couldn't get developer account ID.");
}

String xsrfToken = findXsrfToken(responseStr);
if (xsrfToken == null) {
debugAuthFailure(activity, responseStr);
debugAuthFailure(activity, responseStr, webloginUrl);

throw new AuthenticationException("Couldn't get XSRF token.");
}
Expand All @@ -237,40 +236,4 @@ private SessionCredentials authenticateInternal(Activity activity, boolean inval
}
}

private void debugAuthFailure(Activity activity, String responseStr) {
FileUtils.writeToAndlyticsDir("console-response.html", responseStr);
openAuthUrlInBrowser(activity);
}

private void openAuthUrlInBrowser(Activity activity) {
if (webloginUrl == null) {
Log.d(TAG, "Null webloginUrl?");
return;
}

Log.d(TAG, "Opening login URL in browser: " + webloginUrl);

Intent viewInBrowser = new Intent(Intent.ACTION_VIEW);
viewInBrowser.setData(Uri.parse(webloginUrl));

// Always show the notification
// When this occurs, it can often occur in batches, e.g. if a the user also clicks to view
// comments which results in multiple dev consoles opening in their browser without an
// explanation. This is even worse if they have multiple accounts and/or are currently
// signed in via a different account
Context ctx = AndlyticsApp.getInstance();
Builder builder = new NotificationCompat.Builder(ctx);
builder.setSmallIcon(R.drawable.statusbar_andlytics);
builder.setContentTitle(ctx.getResources().getString(R.string.auth_error, accountName));
builder.setContentText(ctx.getResources().getString(R.string.auth_error_open_browser,
accountName));
builder.setAutoCancel(true);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, accountName.hashCode(),
viewInBrowser, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);

NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(accountName.hashCode(), builder.build());
}
}
60 changes: 56 additions & 4 deletions src/com/github/andlyticsproject/console/v2/BaseAuthenticator.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
package com.github.andlyticsproject.console.v2;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;

import com.github.andlyticsproject.AndlyticsApp;
import com.github.andlyticsproject.R;
import com.github.andlyticsproject.model.DeveloperConsoleAccount;
import com.github.andlyticsproject.util.FileUtils;

import org.apache.commons.lang3.StringEscapeUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringEscapeUtils;

import com.github.andlyticsproject.model.DeveloperConsoleAccount;

public abstract class BaseAuthenticator implements DevConsoleAuthenticator {

private static final String TAG = BaseAuthenticator.class.getSimpleName();

protected static final Pattern DEV_ACC_PATTERN = Pattern
.compile("\"DeveloperConsoleAccounts\":\"\\{\\\\\"1\\\\\":\\[\\{\\\\\"1\\\\\":\\\\\"(\\d{20})\\\\\"");
protected static final Pattern DEV_ACCS_PATTERN = Pattern
Expand Down Expand Up @@ -72,4 +87,41 @@ public String getAccountName() {
return accountName;
}

protected void debugAuthFailure(Activity activity, String responseStr, String webloginUrl) {
FileUtils.writeToAndlyticsDir("console-response.html", responseStr);
openAuthUrlInBrowser(activity, webloginUrl);
}

protected void openAuthUrlInBrowser(Activity activity, String webloginUrl) {
if (webloginUrl == null) {
Log.d(TAG, "Null webloginUrl?");
return;
}

Log.d(TAG, "Opening login URL in browser: " + webloginUrl);

Intent viewInBrowser = new Intent(Intent.ACTION_VIEW);
viewInBrowser.setData(Uri.parse(webloginUrl));

// Always show the notification
// When this occurs, it can often occur in batches, e.g. if a the user also clicks to view
// comments which results in multiple dev consoles opening in their browser without an
// explanation. This is even worse if they have multiple accounts and/or are currently
// signed in via a different account
Context ctx = AndlyticsApp.getInstance();
Builder builder = new NotificationCompat.Builder(ctx);
builder.setSmallIcon(R.drawable.statusbar_andlytics);
builder.setContentTitle(ctx.getResources().getString(R.string.auth_error, accountName));
builder.setContentText(ctx.getResources().getString(R.string.auth_error_open_browser,
accountName));
builder.setAutoCancel(true);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, accountName.hashCode(),
viewInBrowser, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);

NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(accountName.hashCode(), builder.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.github.andlyticsproject.console.AuthenticationException;
import com.github.andlyticsproject.console.NetworkException;
import com.github.andlyticsproject.model.DeveloperConsoleAccount;
import com.github.andlyticsproject.util.FileUtils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
Expand Down Expand Up @@ -163,6 +162,12 @@ private SessionCredentials authenticateInternal(Activity activity, boolean inval
pairs.add(new BasicNameValuePair("uberauth", uberToken));
pairs.add(new BasicNameValuePair("continue", DEVELOPER_CONSOLE_URL));
pairs.add(new BasicNameValuePair("source", "ChromiumBrowser"));
// for debugging?
webloginUrl = Uri.parse(MERGE_SESSION_URL).buildUpon()
.appendQueryParameter("source", "ChromiumBrowser")
.appendQueryParameter("uberauth", uberToken)
.appendQueryParameter("continue", DEVELOPER_CONSOLE_URL).build().toString();
Log.d(TAG, "MergeSession URL: " + webloginUrl);

UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs, "UTF-8");
getConsole.setEntity(formEntity);
Expand All @@ -188,14 +193,14 @@ private SessionCredentials authenticateInternal(Activity activity, boolean inval

DeveloperConsoleAccount[] developerAccounts = findDeveloperAccounts(responseStr);
if (developerAccounts == null) {
debugAuthFailure(activity, responseStr);
debugAuthFailure(activity, responseStr, webloginUrl);

throw new AuthenticationException("Couldn't get developer account ID.");
}

String xsrfToken = findXsrfToken(responseStr);
if (xsrfToken == null) {
debugAuthFailure(activity, responseStr);
debugAuthFailure(activity, responseStr, webloginUrl);

throw new AuthenticationException("Couldn't get XSRF token.");
}
Expand All @@ -217,40 +222,4 @@ private SessionCredentials authenticateInternal(Activity activity, boolean inval
}
}

private void debugAuthFailure(Activity activity, String responseStr) {
FileUtils.writeToAndlyticsDir("console-response.html", responseStr);
openAuthUrlInBrowser(activity);
}

private void openAuthUrlInBrowser(Activity activity) {
if (webloginUrl == null) {
Log.d(TAG, "Null webloginUrl?");
return;
}

Log.d(TAG, "Opening login URL in browser: " + webloginUrl);

Intent viewInBrowser = new Intent(Intent.ACTION_VIEW);
viewInBrowser.setData(Uri.parse(webloginUrl));

// Always show the notification
// When this occurs, it can often occur in batches, e.g. if a the user also clicks to view
// comments which results in multiple dev consoles opening in their browser without an
// explanation. This is even worse if they have multiple accounts and/or are currently
// signed in via a different account
Context ctx = AndlyticsApp.getInstance();
Builder builder = new NotificationCompat.Builder(ctx);
builder.setSmallIcon(R.drawable.statusbar_andlytics);
builder.setContentTitle(ctx.getResources().getString(R.string.auth_error, accountName));
builder.setContentText(ctx.getResources().getString(R.string.auth_error_open_browser,
accountName));
builder.setAutoCancel(true);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, accountName.hashCode(),
viewInBrowser, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);

NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(accountName.hashCode(), builder.build());
}
}