Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit d4ab04b

Browse files
committed
Merge pull request #46 from peternied/adal-permissions
Broker required permissions check
2 parents 53b3332 + fbeaaae commit d4ab04b

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

onedrivesdk/src/main/java/com/onedrive/sdk/authentication/ADALAuthenticator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.microsoft.aad.adal.AuthenticationException;
3535
import com.microsoft.aad.adal.AuthenticationResult;
3636
import com.microsoft.aad.adal.PromptBehavior;
37+
import com.onedrive.sdk.authentication.adal.BrokerPermissionsChecker;
3738
import com.microsoft.onedrivesdk.BuildConfig;
3839
import com.onedrive.sdk.concurrency.ICallback;
3940
import com.onedrive.sdk.concurrency.IExecutors;
@@ -197,6 +198,10 @@ public synchronized void init(final IExecutors executors,
197198
mHttpProvider = httpProvider;
198199
mActivity = activity;
199200
mLogger = logger;
201+
202+
final BrokerPermissionsChecker brokerPermissionsChecker = new BrokerPermissionsChecker(mActivity, mLogger);
203+
brokerPermissionsChecker.check();
204+
200205
try {
201206
mAdalContext = new AuthenticationContext(activity,
202207
LOGIN_AUTHORITY,
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

onedrivesdk/src/main/java/com/onedrive/sdk/core/OneDriveErrorCodes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public enum OneDriveErrorCodes {
3434
AsyncTaskNotCompleted,
3535
AuthenticationCancelled,
3636
AuthenticationFailure,
37+
AuthenicationPermissionsDenied,
3738
GeneralException,
3839
InvalidRange,
3940
InvalidRequest,

0 commit comments

Comments
 (0)