检查自定义帐户验证程序中的权限

时间:2013-03-11 20:35:07

标签: android android-account android-authenticator

我有一个自定义验证器,我想将用户/密码暴露给其他应用程序。为了防止任何随机应用获取凭据,我想在自定义身份验证器的getAuthToken()方法中执行类似权限检查的操作。什么是正确的方法?

我试过这个,

    int p = context.checkCallingPermission("com.whatever.AUTH");
    if (p != PackageManager.PERMISSION_GRANTED) {

其中“com.whatever.AUTH”在托管我的身份验证器的应用中定义,

<permission android:name="com.vmware.horizon.AUTH" />

但是,在我的测试应用中,当我请求该帐户时,它的清单中没有uses-permission

    AccountManagerFuture<Bundle> future = am.getAuthToken(new Account(
            "com.whatever", "com.whatever"),
            "com.whatever", new Bundle(), this,
            new AccountManagerCallback<Bundle>() {

                @Override
                public void run(AccountManagerFuture<Bundle> future) {
                                        String token  = result.getString(AccountManager.KEY_AUTHTOKEN);

                }
            }, handler);

我成功获得了身份验证令牌。调试显示调用我的身份验证器的getAuthToken()方法,但检查权限调用返回“已授予”。

编辑:如果我从用于调用checkCallingPermission()的上下文中获取包名称,则它是托管自定义身份验证器的应用程序的包名称。如果我得到调用PID,则UID分别为0和1000。

任何想法?

1 个答案:

答案 0 :(得分:5)

在查看Android源代码时,我注意到帐户管理器服务将调用者的pid和uid设置为Bundle AccountManager.KEY_CALLER_PIDAccountManager.KEY_CALLER_UID

您可以在捆绑包上使用getInt()来查找getAppToken方法中真正的来电者的pid和uid。

另一个很难找到的有用信息。由于帐户管理器服务缓存结果,因此getAppToken方法通常只调用一次。如果您希望能够更积极地管理令牌,可以通过向清单条目添加元数据来禁用缓存:

<service android:name=".authenticator.AccountAuthenticatorService">
  <meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator"/>
  <meta-data android:name="android.accounts.AccountAuthenticator.customTokens" android:value="1"/>
</service>

以下是authenticator xml的样子:

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
  android:accountType="com.your.account.type"
  android:customTokens="true"
  android:icon="@drawable/logo"
  android:smallIcon="@drawable/logo"
  android:label="@string/app_name_long"/>
相关问题