Properly retreive account from AccountManager for auth

This commit is contained in:
YuviPanda 2012-10-17 17:53:30 +05:30
parent e3a2384956
commit 76fc431532
5 changed files with 176 additions and 31 deletions

View file

@ -0,0 +1,143 @@
package org.wikimedia.commons.auth;
import java.io.IOException;
import android.accounts.*;
import android.app.Activity;
import android.content.*;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
public class AuthenticatedActivity extends Activity {
String accountType;
public AuthenticatedActivity(String accountType) {
this.accountType = accountType;
}
private class GetAuthCookieTask extends AsyncTask<String, String, String> {
private Account account;
private AccountManager accountManager;
public GetAuthCookieTask(Account account, AccountManager accountManager) {
this.account = account;
this.accountManager = accountManager;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result != null) {
onAuthCookieAcquired(result);
} else {
onAuthFailure();
}
}
@Override
protected String doInBackground(String... params) {
try {
return accountManager.blockingGetAuthToken(account, "", false);
} catch (OperationCanceledException e) {
e.printStackTrace();
return null;
} catch (AuthenticatorException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
// private class onTokenAcquired implements AccountManagerCallback<Bundle> {
//
// @Override
// public void run(AccountManagerFuture<Bundle> result) {
// Bundle bundle;
// try {
// bundle = result.getResult();
// } catch (OperationCanceledException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// throw new RuntimeException(e);
// } catch (AuthenticatorException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// throw new RuntimeException(e);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// throw new RuntimeException(e);
// }
// Log.d("Commons", "Token Found!");
// if(bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
// String authCookie = bundle.getString(AccountManager.KEY_AUTHTOKEN);
// onAuthCookieAcquired(authCookie);
// } else {
// if(bundle.containsKey(AccountManager.KEY_INTENT)) {
// Intent launchIntent = (Intent) bundle.get(AccountManager.KEY_INTENT);
// startActivityForResult(launchIntent, 0);
// } else {
//
// }
// }
//
// }
// }
//
//
// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
// Log.d("Commons", "Result of the loginactivity!");
// if(resultCode == Activity.RESULT_OK) {
// requestAuthToken();
// }
// }
//
// private void requestAuthToken() {
// AccountManager accountManager = AccountManager.get(this);
// Account[] allAccounts =accountManager.getAccountsByType(accountType);
// if(allAccounts.length == 0) {
// Log.d("Commons", "No accounts yet!");
// // No Commons Accounts yet!
// accountManager.addAccount(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE, "", null, null, this, new onTokenAcquired(), null);
// return;
// }
//
// Log.d("Commons", "Accounts found!");
// // For now - always pick the first account
// Account curAccount = allAccounts[0];
// Bundle cookieOptions = new Bundle();
// accountManager.getAuthToken(curAccount, "", cookieOptions, this, new onTokenAcquired(), null);
// }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AccountManager accountManager = AccountManager.get(this);
Account[] allAccounts =accountManager.getAccountsByType(accountType);
if(allAccounts.length == 0) {
// No Commons Accounts yet!
accountManager.addAccount(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE, "", null, null, this, null, null);
return;
}
// For now - always pick the first account
Account curAccount = allAccounts[0];
GetAuthCookieTask task = new GetAuthCookieTask(curAccount, accountManager);
task.execute("");
}
protected void onAuthCookieAcquired(String authCookie) {
}
protected void onAuthFailure() {
}
}

View file

@ -14,6 +14,7 @@ import android.accounts.NetworkErrorException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
@ -47,7 +48,7 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
}
private String getAuthCookie(String username, String password) throws IOException {
MWApi api = ((CommonsApplication)context.getApplicationContext()).createMWApi();
MWApi api = CommonsApplication.createMWApi();
String result = api.login(username, password);
if(result.equals("Success")) {
return api.getAuthCookie();
@ -57,14 +58,6 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
}
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
// If the caller requested an authToken type we don't support, then
// return an error
if (!authTokenType.equals(COMMONS_ACCOUNT_TYPE)) {
final Bundle result = new Bundle();
result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
return result;
}
// Extract the username and password from the Account Manager, and ask
// the server for an appropriate AuthToken.
final AccountManager am = AccountManager.get(context);
@ -75,6 +68,7 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
authCookie = getAuthCookie(account.name, password);
} catch (IOException e) {
// Network error!
e.printStackTrace();
throw new NetworkErrorException(e);
}
if (authCookie != null) {