Parse OAuth2 consent data

This commit is contained in:
mar-v-in 2015-03-23 22:30:41 +01:00
parent a1a27167e6
commit 15d772c195
3 changed files with 56 additions and 11 deletions

View File

@ -24,6 +24,7 @@ import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@ -47,6 +48,8 @@ import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
import static android.accounts.AccountManager.KEY_ANDROID_PACKAGE_NAME;
import static android.accounts.AccountManager.KEY_AUTHTOKEN;
import static android.accounts.AccountManager.KEY_CALLER_UID;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
public class AskPermissionActivity extends AccountAuthenticatorActivity {
public static final String EXTRA_FROM_ACCOUNT_MANAGER = "from_account_manager";
@ -79,9 +82,11 @@ public class AskPermissionActivity extends AccountAuthenticatorActivity {
if (getIntent().hasExtra(EXTRA_CONSENT_DATA)) {
try {
consentData = new Wire().parseFrom(getIntent().getByteArrayExtra(EXTRA_CONSENT_DATA), ConsentData.class);
Log.d(TAG, "Consent: " + consentData);
} catch (Exception ignored) {
} catch (Exception e) {
Log.w(TAG, e);
}
} else {
Log.d(TAG, "No Consent details attached");
}
if (getIntent().hasExtra(EXTRA_FROM_ACCOUNT_MANAGER)) fromAccountManager = true;
int callerUid = getIntent().getIntExtra(KEY_CALLER_UID, 0);
@ -146,8 +151,8 @@ public class AskPermissionActivity extends AccountAuthenticatorActivity {
authManager.setPermitted(true);
findViewById(android.R.id.button1).setEnabled(false);
findViewById(android.R.id.button2).setEnabled(false);
findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
findViewById(R.id.no_progress_bar).setVisibility(View.GONE);
findViewById(R.id.progress_bar).setVisibility(VISIBLE);
findViewById(R.id.no_progress_bar).setVisibility(GONE);
new Thread(new Runnable() {
@Override
public void run() {
@ -205,6 +210,17 @@ public class AskPermissionActivity extends AccountAuthenticatorActivity {
return "unknown";
}
private String getScopeDescription(String scope) {
if (consentData != null) {
for (ConsentData.ScopeDetails scopeDetails : consentData.scopes) {
if (scope.equals(scopeDetails.id)) {
return scopeDetails.description;
}
}
}
return null;
}
private String getServiceLabel(String service) {
int labelResource = getResources().getIdentifier("permission_service_" + service + "_label", "string", getPackageName());
if (labelResource != 0) {
@ -241,16 +257,26 @@ public class AskPermissionActivity extends AccountAuthenticatorActivity {
public View getView(int position, View convertView, ViewGroup parent) {
String item = getItem(position);
String label;
String description;
if (isOAuth()) {
label = getScopeLabel(item);
description = getScopeDescription(item);
} else {
label = getServiceLabel(item);
description = null;
}
View view = convertView;
if (view == null) {
view = LayoutInflater.from(AskPermissionActivity.this).inflate(R.layout.ask_permission_list_entry, null);
}
((TextView) view.findViewById(android.R.id.text1)).setText(label);
TextView textView = (TextView) view.findViewById(android.R.id.text2);
if (description != null && !description.isEmpty()) {
textView.setText(Html.fromHtml(description.trim().replace("\n","<br>")));
textView.setVisibility(VISIBLE);
} else {
textView.setVisibility(GONE);
}
return view;
}
}

View File

@ -60,7 +60,8 @@ public class AuthManagerServiceImpl extends IAuthManagerService.Stub {
@Override
public Bundle getToken(String accountName, String scope, Bundle extras) throws RemoteException {
String packageName = extras.getString(KEY_ANDROID_PACKAGE_NAME, extras.getString(KEY_CLIENT_PACKAGE_NAME, null));
String packageName = extras.getString(KEY_ANDROID_PACKAGE_NAME);
if (packageName == null || packageName.isEmpty()) packageName = extras.getString(KEY_CLIENT_PACKAGE_NAME);
int callerUid = extras.getInt(KEY_CALLER_UID, 0);
PackageUtils.checkPackageUid(context, packageName, callerUid, getCallingUid());
boolean notify = extras.getBoolean(KEY_HANDLE_NOTIFICATION, false);
@ -85,7 +86,7 @@ public class AuthManagerServiceImpl extends IAuthManagerService.Stub {
i.putExtra(KEY_ACCOUNT_NAME, accountName);
i.putExtra(KEY_AUTHTOKEN, scope);
if (res.consentDataBase64 != null)
i.putExtra(EXTRA_CONSENT_DATA, Base64.decode(res.consentDataBase64, Base64.DEFAULT));
i.putExtra(EXTRA_CONSENT_DATA, Base64.decode(res.consentDataBase64, Base64.URL_SAFE));
if (notify) {
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
@ -96,7 +97,7 @@ public class AuthManagerServiceImpl extends IAuthManagerService.Stub {
}
} catch (Exception e) {
Log.w(TAG, e);
throw new RemoteException(e.getMessage());
throw new RuntimeException(e);
}
}
@ -107,6 +108,11 @@ public class AuthManagerServiceImpl extends IAuthManagerService.Stub {
@Override
public Bundle clearToken(String token, Bundle extras) throws RemoteException {
String packageName = extras.getString(KEY_ANDROID_PACKAGE_NAME, extras.getString(KEY_CLIENT_PACKAGE_NAME));
int callerUid = extras.getInt(KEY_CALLER_UID, 0);
PackageUtils.checkPackageUid(context, packageName, callerUid, getCallingUid());
Log.d(TAG, "clearToken: token:" + token + " extras:" + extras);
return null;
}
}

View File

@ -26,10 +26,23 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
<LinearLayout
android:padding="6dp"
android:textSize="16sp"
android:id="@android:id/text1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content">
<TextView
android:textSize="16sp"
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textSize="12sp"
android:visibility="gone"
android:id="@android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>