|
| 1 | +// ------------------------------------------------------------------------------ |
| 2 | +// Copyright (c) 2016 Microsoft Corporation |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | +// ------------------------------------------------------------------------------ |
| 22 | + |
| 23 | +package com.onedrive.sdk.authentication.adal; |
| 24 | + |
| 25 | +import android.content.Context; |
| 26 | +import android.content.pm.PackageManager; |
| 27 | +import android.support.v4.content.ContextCompat; |
| 28 | + |
| 29 | +import com.microsoft.aad.adal.AuthenticationSettings; |
| 30 | +import com.onedrive.sdk.authentication.ClientAuthenticatorException; |
| 31 | +import com.onedrive.sdk.core.OneDriveErrorCodes; |
| 32 | +import com.onedrive.sdk.logger.ILogger; |
| 33 | + |
| 34 | +/** |
| 35 | + * Checks if the ADAL broker has the required permissions to be used |
| 36 | + */ |
| 37 | +public class BrokerPermissionsChecker { |
| 38 | + |
| 39 | + /** |
| 40 | + * The url to the ADAL project for reference |
| 41 | + */ |
| 42 | + @SuppressWarnings("FieldCanBeLocal") |
| 43 | + private final String mAdalProjectUrl = "https://github.com/AzureAD/azure-activedirectory-library-for-android"; |
| 44 | + |
| 45 | + /** |
| 46 | + * The permissions need to use a the account broker with ADAL |
| 47 | + */ |
| 48 | + private final String[] mBrokerRequirePermissions = new String[] { |
| 49 | + "android.permission.GET_ACCOUNTS", |
| 50 | + "android.permission.MANAGE_ACCOUNTS", |
| 51 | + "android.permission.USE_CREDENTIALS" |
| 52 | + }; |
| 53 | + |
| 54 | + /** |
| 55 | + * The current context |
| 56 | + */ |
| 57 | + private final Context mContext; |
| 58 | + |
| 59 | + /** |
| 60 | + * The logger to use |
| 61 | + */ |
| 62 | + private final ILogger mLogger; |
| 63 | + |
| 64 | + /** |
| 65 | + * Creates a BrokerPermissionsChecker |
| 66 | + * @param context The current context to check permissions against |
| 67 | + * @param logger The logger context |
| 68 | + */ |
| 69 | + public BrokerPermissionsChecker(final Context context, final ILogger logger) { |
| 70 | + mContext = context; |
| 71 | + mLogger = logger; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Checks if the Broker has the permissions needed be used. |
| 76 | + * |
| 77 | + * @throws ClientAuthenticatorException If the required permissions are not available |
| 78 | + */ |
| 79 | + public void check() throws ClientAuthenticatorException { |
| 80 | + if (!AuthenticationSettings.INSTANCE.getSkipBroker()) { |
| 81 | + mLogger.logDebug("Checking permissions for use with the ADAL Broker."); |
| 82 | + for (final String permission : mBrokerRequirePermissions) { |
| 83 | + if (ContextCompat.checkSelfPermission(mContext, permission) == PackageManager.PERMISSION_DENIED) { |
| 84 | + final String message = String.format( |
| 85 | + "Required permissions to use the Broker are denied: %s, see %s for more details.", |
| 86 | + permission, |
| 87 | + mAdalProjectUrl); |
| 88 | + mLogger.logDebug(message); |
| 89 | + throw new ClientAuthenticatorException(message, OneDriveErrorCodes.AuthenicationPermissionsDenied); |
| 90 | + } |
| 91 | + } |
| 92 | + mLogger.logDebug("All required permissions found."); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments