1 /* 2 * Copyright 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.net.module.util; 18 19 import static android.Manifest.permission.NETWORK_STACK; 20 import static android.content.pm.PackageManager.PERMISSION_GRANTED; 21 import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK; 22 23 import android.annotation.NonNull; 24 import android.content.Context; 25 26 import java.util.ArrayList; 27 import java.util.Arrays; 28 29 /** 30 * Collection of permission utilities. 31 * @hide 32 */ 33 public final class PermissionUtils { 34 /** 35 * Return true if the context has one of given permission. 36 */ checkAnyPermissionOf(@onNull Context context, @NonNull String... permissions)37 public static boolean checkAnyPermissionOf(@NonNull Context context, 38 @NonNull String... permissions) { 39 for (String permission : permissions) { 40 if (context.checkCallingOrSelfPermission(permission) == PERMISSION_GRANTED) { 41 return true; 42 } 43 } 44 return false; 45 } 46 47 /** 48 * Enforce permission check on the context that should have one of given permission. 49 */ enforceAnyPermissionOf(@onNull Context context, @NonNull String... permissions)50 public static void enforceAnyPermissionOf(@NonNull Context context, 51 @NonNull String... permissions) { 52 if (!checkAnyPermissionOf(context, permissions)) { 53 throw new SecurityException("Requires one of the following permissions: " 54 + String.join(", ", permissions) + "."); 55 } 56 } 57 58 /** 59 * If the NetworkStack, MAINLINE_NETWORK_STACK are not allowed for a particular process, throw a 60 * {@link SecurityException}. 61 * 62 * @param context {@link android.content.Context} for the process. 63 */ enforceNetworkStackPermission(final @NonNull Context context)64 public static void enforceNetworkStackPermission(final @NonNull Context context) { 65 enforceNetworkStackPermissionOr(context); 66 } 67 68 /** 69 * If the NetworkStack, MAINLINE_NETWORK_STACK or other specified permissions are not allowed 70 * for a particular process, throw a {@link SecurityException}. 71 * 72 * @param context {@link android.content.Context} for the process. 73 * @param otherPermissions The set of permissions that could be the candidate permissions , or 74 * empty string if none of other permissions needed. 75 */ enforceNetworkStackPermissionOr(final @NonNull Context context, final @NonNull String... otherPermissions)76 public static void enforceNetworkStackPermissionOr(final @NonNull Context context, 77 final @NonNull String... otherPermissions) { 78 ArrayList<String> permissions = new ArrayList<String>(Arrays.asList(otherPermissions)); 79 permissions.add(NETWORK_STACK); 80 permissions.add(PERMISSION_MAINLINE_NETWORK_STACK); 81 enforceAnyPermissionOf(context, permissions.toArray(new String[0])); 82 } 83 } 84