Skip to content

Commit

Permalink
Move everything to OAuth2, drop XOAuth
Browse files Browse the repository at this point in the history
Refs #554
  • Loading branch information
jberkel committed Jun 4, 2015
1 parent 39e4260 commit 925d348
Show file tree
Hide file tree
Showing 18 changed files with 724 additions and 236 deletions.
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
</intent-filter>
</activity>

<activity android:name=".activity.auth.WebAuthActivity" android:theme="@style/Theme.WebAuth"/>
<activity android:name=".activity.auth.OAuth2WebAuthActivity" android:theme="@style/Theme.WebAuth"/>
<activity android:name=".activity.auth.AccountManagerAuthActivity" android:theme="@style/Theme.SmsBackupPlus"/>
<activity android:name=".activity.donation.DonationActivity" android:theme="@style/Theme.Donation"/>

Expand Down
4 changes: 4 additions & 0 deletions res/values/keys.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="oauth2_client_id">959061759285-uqnem9qtjr97856o7b6pkuek0ref5dnd.apps.googleusercontent.com</string>
</resources>
63 changes: 33 additions & 30 deletions src/main/java/com/zegoggles/smssync/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@
import com.zegoggles.smssync.Consts;
import com.zegoggles.smssync.R;
import com.zegoggles.smssync.activity.auth.AccountManagerAuthActivity;
import com.zegoggles.smssync.activity.auth.WebAuthActivity;
import com.zegoggles.smssync.activity.auth.OAuth2WebAuthActivity;
import com.zegoggles.smssync.activity.donation.DonationActivity;
import com.zegoggles.smssync.auth.OAuth2Client;
import com.zegoggles.smssync.calendar.CalendarAccessor;
import com.zegoggles.smssync.contacts.ContactAccessor;
import com.zegoggles.smssync.mail.BackupImapStore;
Expand All @@ -69,11 +70,9 @@
import com.zegoggles.smssync.service.SmsBackupService;
import com.zegoggles.smssync.service.SmsRestoreService;
import com.zegoggles.smssync.service.state.RestoreState;
import com.zegoggles.smssync.tasks.OAuthCallbackTask;
import com.zegoggles.smssync.tasks.RequestTokenTask;
import com.zegoggles.smssync.tasks.OAuth2CallbackTask;
import com.zegoggles.smssync.utils.AppLog;
import com.zegoggles.smssync.utils.ListPreferenceHelper;
import org.jetbrains.annotations.Nullable;

import java.text.DateFormat;
import java.util.ArrayList;
Expand All @@ -82,8 +81,22 @@
import java.util.Locale;

import static com.zegoggles.smssync.App.TAG;
import static com.zegoggles.smssync.mail.DataType.*;
import static com.zegoggles.smssync.preferences.Preferences.Keys.*;
import static com.zegoggles.smssync.mail.DataType.CALLLOG;
import static com.zegoggles.smssync.mail.DataType.MMS;
import static com.zegoggles.smssync.mail.DataType.SMS;
import static com.zegoggles.smssync.preferences.Preferences.Keys.BACKUP_CONTACT_GROUP;
import static com.zegoggles.smssync.preferences.Preferences.Keys.BACKUP_SETTINGS_SCREEN;
import static com.zegoggles.smssync.preferences.Preferences.Keys.CALLLOG_SYNC_CALENDAR;
import static com.zegoggles.smssync.preferences.Preferences.Keys.CALLLOG_SYNC_CALENDAR_ENABLED;
import static com.zegoggles.smssync.preferences.Preferences.Keys.CONNECTED;
import static com.zegoggles.smssync.preferences.Preferences.Keys.DONATE;
import static com.zegoggles.smssync.preferences.Preferences.Keys.ENABLE_AUTO_BACKUP;
import static com.zegoggles.smssync.preferences.Preferences.Keys.IMAP_SETTINGS;
import static com.zegoggles.smssync.preferences.Preferences.Keys.INCOMING_TIMEOUT_SECONDS;
import static com.zegoggles.smssync.preferences.Preferences.Keys.MAX_ITEMS_PER_RESTORE;
import static com.zegoggles.smssync.preferences.Preferences.Keys.MAX_ITEMS_PER_SYNC;
import static com.zegoggles.smssync.preferences.Preferences.Keys.REGULAR_TIMEOUT_SECONDS;
import static com.zegoggles.smssync.preferences.Preferences.Keys.WIFI_ONLY;

/**
* This is the main activity showing the status of the SMS Sync service and
Expand All @@ -106,12 +119,13 @@ enum Actions {
private AuthPreferences authPreferences;
private Preferences preferences;
private StatusPreference statusPref;
private @Nullable Uri mAuthorizeUri;
private OAuth2Client oauth2Client;

@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
authPreferences = new AuthPreferences(this);
oauth2Client = new OAuth2Client(authPreferences.getOAuth2ClientId());
preferences = new Preferences(this);
addPreferencesFromResource(R.xml.preferences);

Expand Down Expand Up @@ -220,10 +234,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
break;
}
case REQUEST_WEB_AUTH: {
Uri uri = data.getData();
if (uri != null && uri.toString().startsWith(Consts.CALLBACK_URL)) {
final String code = data.getStringExtra(OAuth2WebAuthActivity.EXTRA_CODE);
if (!TextUtils.isEmpty(code)) {
show(Dialogs.ACCESS_TOKEN);
new OAuthCallbackTask(this).execute(data);
new OAuth2CallbackTask(oauth2Client).execute(code);
} else {
show(Dialogs.ACCESS_TOKEN_ERROR);
}
break;
}
Expand All @@ -244,21 +260,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

@Subscribe public void onAuthorizedURLReceived(RequestTokenTask.AuthorizedURLReceived authorizedURLReceived) {
dismiss(Dialogs.REQUEST_TOKEN);
this.mAuthorizeUri = authorizedURLReceived.uri;
if (mAuthorizeUri != null) {
show(Dialogs.CONNECT);
} else {
show(Dialogs.CONNECT_TOKEN_ERROR);
}
}

@Subscribe public void onOAuthCallback(OAuthCallbackTask.OAuthCallbackEvent event) {
@Subscribe public void onOAuth2Callback(OAuth2CallbackTask.OAuth2CallbackEvent event) {
dismiss(Dialogs.ACCESS_TOKEN);
if (event.valid()) {
authPreferences.setOauthUsername(event.username);
authPreferences.setOauthTokens(event.token, event.tokenSecret);
authPreferences.setOauth2Token(event.token.userName, event.token.accessToken, event.token.refreshToken);
onAuthenticated();
} else {
show(Dialogs.ACCESS_TOKEN_ERROR);
Expand Down Expand Up @@ -614,10 +619,9 @@ public void onClick(DialogInterface dialog, int which) {
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (mAuthorizeUri != null) {
startActivityForResult(new Intent(MainActivity.this, WebAuthActivity.class)
.setData(mAuthorizeUri), REQUEST_WEB_AUTH);
}
startActivityForResult(new Intent(MainActivity.this, OAuth2WebAuthActivity.class)
.setData(oauth2Client.requestUrl()), REQUEST_WEB_AUTH);

dismissDialog(id);
}
}).create();
Expand Down Expand Up @@ -957,7 +961,7 @@ private void handleAccountManagerAuth(Intent data) {
String token = data.getStringExtra(AccountManagerAuthActivity.EXTRA_TOKEN);
String account = data.getStringExtra(AccountManagerAuthActivity.EXTRA_ACCOUNT);
if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(account)) {
authPreferences.setOauth2Token(account, token);
authPreferences.setOauth2Token(account, token, null);
onAuthenticated();
} else {
String error = data.getStringExtra(AccountManagerAuthActivity.EXTRA_ERROR);
Expand All @@ -968,8 +972,7 @@ private void handleAccountManagerAuth(Intent data) {
}

private void handleFallbackAuth() {
show(Dialogs.REQUEST_TOKEN);
new RequestTokenTask(this).execute(Consts.CALLBACK_URL);
show(Dialogs.CONNECT);
}

private void checkDefaultSmsApp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,32 @@
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.zegoggles.smssync.App;
import com.zegoggles.smssync.Consts;
import com.zegoggles.smssync.R;

public class WebAuthActivity extends Activity {
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.zegoggles.smssync.App.TAG;

public class OAuth2WebAuthActivity extends Activity {
private WebView mWebview;
private ProgressDialog mProgress;

public static final String EXTRA_CODE = "code";
public static final String EXTRA_ERROR = "error";

// Success code=4/8imH8gQubRYrWu_Fpv6u4Yri5kTNEWmm_XyhytJqlJw
// Denied error=access_denied
private static final Pattern TITLE = Pattern.compile("(code|error)=(.+)\\Z");

public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.auth_activity);
Expand All @@ -51,7 +63,7 @@ public void onReceivedError(WebView view, int errorCode, String description, Str
@Override
@TargetApi(Build.VERSION_CODES.FROYO)
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
Log.w(App.TAG, "onReceiveSslError(" + error + ")");
Log.w(TAG, "onReceiveSslError(" + error + ")");
// pre-froyo devices don't trust the cert used by google
// see https://knowledge.verisign.com/support/mpki-for-ssl-support/index?page=content&id=SO17511&actp=AGENT_REFERAL
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO &&
Expand All @@ -65,32 +77,45 @@ public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError e

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (App.LOCAL_LOGV) Log.d(App.TAG, "onPageStarted(" + url + ")");
if (App.LOCAL_LOGV) Log.d(TAG, "onPageStarted(" + url + ")");
if (!isFinishing()) mProgress.show();
}

@Override
public void onPageFinished(WebView view, String url) {
if (!isFinishing()) mProgress.dismiss();
}
final String pageTitle = view.getTitle();
final Matcher matcher = TITLE.matcher(pageTitle);
if (matcher.find()) {
String status = matcher.group(1);
String value = matcher.group(2);

@Override
public boolean shouldOverrideUrlLoading(final WebView view, String url) {
if (url.startsWith(Consts.CALLBACK_URL)) {
setResult(RESULT_OK, new Intent().setData(Uri.parse(url)));
finish();
return true;
} else {
return false;
if ("code".equals(status)) {
onCodeReceived(value);
} else if ("error".equals(status)) {
onError(value);
}
}
if (!isFinishing()) mProgress.dismiss();
}
});
removeAllCookies();

// finally load url
mWebview.loadUrl(urlToLoad.toString());
}

private void onCodeReceived(String code) {
if (!TextUtils.isEmpty(code)) {
setResult(RESULT_OK, new Intent().putExtra(EXTRA_CODE, code));
finish();
}
}

private void onError(String error) {
Log.e(TAG, "onError("+error+")");
setResult(RESULT_OK, new Intent().putExtra(EXTRA_ERROR, error));
finish();
}

private void showConnectionError(final String message) {
if (isFinishing()) return;
new AlertDialog.Builder(this).
Expand Down
Loading

0 comments on commit 925d348

Please sign in to comment.