Skip to content

Commit 5d275bb

Browse files
committed
Fix bug in permission grant system.
Change-Id: Ic7d712e5a672c3eded16fee83235db01ab3c74fa
1 parent a218e01 commit 5d275bb

File tree

2 files changed

+89
-26
lines changed

2 files changed

+89
-26
lines changed

core/java/android/accounts/AccountManagerService.java

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,44 @@ private void onResult(IAccountManagerResponse response, Bundle result) {
802802
}
803803
}
804804

805+
void getAuthTokenLabel(final IAccountManagerResponse response,
806+
final Account account, final String authTokenType) {
807+
if (account == null) throw new IllegalArgumentException("account is null");
808+
if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
809+
810+
checkBinderPermission(Manifest.permission.USE_CREDENTIALS);
811+
812+
long identityToken = clearCallingIdentity();
813+
try {
814+
new Session(response, account.type, false,
815+
false /* stripAuthTokenFromResult */) {
816+
protected String toDebugString(long now) {
817+
return super.toDebugString(now) + ", getAuthTokenLabel"
818+
+ ", " + account
819+
+ ", authTokenType " + authTokenType;
820+
}
821+
822+
public void run() throws RemoteException {
823+
mAuthenticator.getAuthTokenLabel(this, authTokenType);
824+
}
825+
826+
public void onResult(Bundle result) {
827+
if (result != null) {
828+
String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL);
829+
Bundle bundle = new Bundle();
830+
bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label);
831+
super.onResult(bundle);
832+
return;
833+
} else {
834+
super.onResult(result);
835+
}
836+
}
837+
}.bind();
838+
} finally {
839+
restoreCallingIdentity(identityToken);
840+
}
841+
}
842+
805843
public void getAuthToken(IAccountManagerResponse response, final Account account,
806844
final String authTokenType, final boolean notifyOnAuthFailure,
807845
final boolean expectActivityLaunch, final Bundle loginOptions) {
@@ -912,36 +950,36 @@ private void createNoCredentialsPermissionNotification(Account account, Intent i
912950
.notify(getCredentialPermissionNotificationId(account, authTokenType, uid), n);
913951
}
914952

915-
private Intent newGrantCredentialsPermissionIntent(Account account, int uid,
916-
AccountAuthenticatorResponse response, String authTokenType, String authTokenLabel) {
953+
String getAccountLabel(String accountType) {
917954
RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo =
918-
mAuthenticatorCache.getServiceInfo(
919-
AuthenticatorDescription.newKey(account.type));
955+
mAuthenticatorCache.getServiceInfo(
956+
AuthenticatorDescription.newKey(accountType));
920957
if (serviceInfo == null) {
921-
throw new IllegalArgumentException("unknown account type: " + account.type);
958+
throw new IllegalArgumentException("unknown account type: " + accountType);
922959
}
923960

924961
final Context authContext;
925962
try {
926963
authContext = mContext.createPackageContext(
927-
serviceInfo.type.packageName, 0);
964+
serviceInfo.type.packageName, 0);
928965
} catch (PackageManager.NameNotFoundException e) {
929-
throw new IllegalArgumentException("unknown account type: " + account.type);
966+
throw new IllegalArgumentException("unknown account type: " + accountType);
930967
}
968+
return authContext.getString(serviceInfo.type.labelId);
969+
}
970+
971+
private Intent newGrantCredentialsPermissionIntent(Account account, int uid,
972+
AccountAuthenticatorResponse response, String authTokenType, String authTokenLabel) {
931973

932974
Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class);
933-
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
934975
intent.addCategory(
935976
String.valueOf(getCredentialPermissionNotificationId(account, authTokenType, uid)));
977+
936978
intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account);
937-
intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_LABEL, authTokenLabel);
938979
intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType);
939980
intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response);
940-
intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT_TYPE_LABEL,
941-
authContext.getString(serviceInfo.type.labelId));
942-
intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_PACKAGES,
943-
mContext.getPackageManager().getPackagesForUid(uid));
944981
intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid);
982+
945983
return intent;
946984
}
947985

core/java/android/accounts/GrantCredentialsPermissionActivity.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import android.app.Activity;
1919
import android.os.Bundle;
20+
import android.os.RemoteException;
2021
import android.widget.TextView;
2122
import android.widget.LinearLayout;
2223
import android.widget.ImageView;
@@ -26,6 +27,7 @@
2627
import android.content.Context;
2728
import android.content.Intent;
2829
import android.content.pm.PackageManager;
30+
import android.content.pm.RegisteredServicesCache;
2931
import android.text.TextUtils;
3032
import android.graphics.drawable.Drawable;
3133
import com.android.internal.R;
@@ -46,6 +48,7 @@ public class GrantCredentialsPermissionActivity extends Activity implements View
4648
private int mUid;
4749
private Bundle mResultBundle = null;
4850
protected LayoutInflater mInflater;
51+
private final AccountManagerService accountManagerService = AccountManagerService.getSingleton();
4952

5053
protected void onCreate(Bundle savedInstanceState) {
5154
requestWindowFeature(Window.FEATURE_NO_TITLE);
@@ -55,27 +58,56 @@ protected void onCreate(Bundle savedInstanceState) {
5558
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
5659

5760
final Bundle extras = getIntent().getExtras();
61+
62+
// Grant 'account'/'type' to mUID
5863
mAccount = extras.getParcelable(EXTRAS_ACCOUNT);
5964
mAuthTokenType = extras.getString(EXTRAS_AUTH_TOKEN_TYPE);
65+
mUid = extras.getInt(EXTRAS_REQUESTING_UID);
66+
final PackageManager pm = getPackageManager();
67+
final String[] packages = pm.getPackagesForUid(mUid);
6068

61-
if (mAccount == null || mAuthTokenType == null) {
69+
if (mAccount == null || mAuthTokenType == null || packages == null) {
6270
// we were somehow started with bad parameters. abort the activity.
6371
setResult(Activity.RESULT_CANCELED);
6472
finish();
6573
return;
6674
}
6775

68-
mUid = extras.getInt(EXTRAS_REQUESTING_UID);
69-
final String accountTypeLabel = extras.getString(EXTRAS_ACCOUNT_TYPE_LABEL);
70-
final String[] packages = extras.getStringArray(EXTRAS_PACKAGES);
71-
final String authTokenLabel = extras.getString(EXTRAS_AUTH_TOKEN_LABEL);
76+
final String accountTypeLabel = accountManagerService.getAccountLabel(mAccount.type);
77+
78+
79+
final TextView authTokenTypeView = (TextView) findViewById(R.id.authtoken_type);
80+
authTokenTypeView.setVisibility(View.GONE);
81+
82+
/** Handles the responses from the AccountManager */
83+
IAccountManagerResponse response = new IAccountManagerResponse.Stub() {
84+
public void onResult(Bundle bundle) {
85+
final String authTokenLabel =
86+
bundle.getString(AccountManager.KEY_AUTH_TOKEN_LABEL);
87+
if (!TextUtils.isEmpty(authTokenLabel)) {
88+
runOnUiThread(new Runnable() {
89+
public void run() {
90+
if (!isFinishing()) {
91+
authTokenTypeView.setText(authTokenLabel);
92+
authTokenTypeView.setVisibility(View.VISIBLE);
93+
}
94+
}
95+
});
96+
}
97+
}
98+
99+
public void onError(int code, String message) {
100+
}
101+
};
102+
103+
accountManagerService.getAuthTokenLabel(
104+
response, mAccount, mAuthTokenType);
72105

73106
findViewById(R.id.allow_button).setOnClickListener(this);
74107
findViewById(R.id.deny_button).setOnClickListener(this);
75108

76109
LinearLayout packagesListView = (LinearLayout) findViewById(R.id.packages_list);
77110

78-
final PackageManager pm = getPackageManager();
79111
for (String pkg : packages) {
80112
String packageLabel;
81113
try {
@@ -88,12 +120,6 @@ protected void onCreate(Bundle savedInstanceState) {
88120

89121
((TextView) findViewById(R.id.account_name)).setText(mAccount.name);
90122
((TextView) findViewById(R.id.account_type)).setText(accountTypeLabel);
91-
TextView authTokenTypeView = (TextView) findViewById(R.id.authtoken_type);
92-
if (TextUtils.isEmpty(authTokenLabel)) {
93-
authTokenTypeView.setVisibility(View.GONE);
94-
} else {
95-
authTokenTypeView.setText(authTokenLabel);
96-
}
97123
}
98124

99125
private View newPackageView(String packageLabel) {
@@ -103,7 +129,6 @@ private View newPackageView(String packageLabel) {
103129
}
104130

105131
public void onClick(View v) {
106-
final AccountManagerService accountManagerService = AccountManagerService.getSingleton();
107132
switch (v.getId()) {
108133
case R.id.allow_button:
109134
accountManagerService.grantAppPermission(mAccount, mAuthTokenType, mUid);

0 commit comments

Comments
 (0)