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

Jef*_*man 3 android android-authenticator android-account

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

我试过这个,

    int p = context.checkCallingPermission("com.whatever.AUTH");
    if (p != PackageManager.PERMISSION_GRANTED) {
Run Code Online (Sandbox Code Playgroud)

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

<permission android:name="com.vmware.horizon.AUTH" />
Run Code Online (Sandbox Code Playgroud)

但是,在我的测试应用程序中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);
Run Code Online (Sandbox Code Playgroud)

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

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

有任何想法吗?

小智 5

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

你可以getInt()在bundle上使用你的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>
Run Code Online (Sandbox Code Playgroud)

以下是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"/>
Run Code Online (Sandbox Code Playgroud)