|
23 | 23 |
|
24 | 24 | import android.annotation.TargetApi;
|
25 | 25 | import android.app.admin.ConnectEvent;
|
| 26 | +import android.app.admin.DevicePolicyManager; |
26 | 27 | import android.app.admin.DnsEvent;
|
27 | 28 | import android.app.admin.NetworkEvent;
|
28 | 29 | import android.app.admin.SecurityLog.SecurityEvent;
|
|
44 | 45 | import androidx.annotation.Nullable;
|
45 | 46 | import com.afwsamples.testdpc.common.Util;
|
46 | 47 | import com.afwsamples.testdpc.policy.SecurityLogsFragment;
|
| 48 | +import com.afwsamples.testdpc.policy.resetpassword.ResetPasswordWithTokenFragment; |
47 | 49 | import com.afwsamples.testdpc.util.flags.Flags;
|
48 | 50 | import java.io.File;
|
49 | 51 | import java.io.PrintWriter;
|
@@ -589,6 +591,12 @@ public void run() {
|
589 | 591 | command("get-delegate-packages", this::getDelegatePackages,
|
590 | 592 | ordinalParam(String.class, "scope"))
|
591 | 593 | .setDescription("Gets the apps that were delegate a given scope."));
|
| 594 | + flags.addCommand( |
| 595 | + command("set-password", this::setPassword, ordinalParam(String.class, "newPassword")) |
| 596 | + .setDescription("Resets password to a given one. Requires an active token")); |
| 597 | + flags.addCommand( |
| 598 | + command("clear-password", this::clearPassword) |
| 599 | + .setDescription("Resets password to an empty one. Requires an active token")); |
592 | 600 |
|
593 | 601 | // Separator for S / pre-S commands - do NOT remove line to avoid cherry-pick conflicts
|
594 | 602 |
|
@@ -1423,6 +1431,50 @@ private void getDelegatePackages(String delegationScope) {
|
1423 | 1431 | printCollection("package", packages);
|
1424 | 1432 | }
|
1425 | 1433 |
|
| 1434 | + private void setPassword(String newPassword) { |
| 1435 | + resetPasswordWithToken(newPassword); |
| 1436 | + } |
| 1437 | + |
| 1438 | + private void clearPassword() { |
| 1439 | + resetPasswordWithToken(""); |
| 1440 | + } |
| 1441 | + |
| 1442 | + private byte[] getActiveResetPasswordToken() { |
| 1443 | + byte[] token = ResetPasswordWithTokenFragment.loadPasswordResetTokenFromPreference(mContext); |
| 1444 | + if (token == null) { |
| 1445 | + return null; |
| 1446 | + } |
| 1447 | + if (!dpm().isResetPasswordTokenActive(DeviceAdminReceiver.getComponentName(mContext))) { |
| 1448 | + Log.i(TAG, "Token exists but is not activated."); |
| 1449 | + mWriter.printf("Token exists but is not activated.\n"); |
| 1450 | + return null; |
| 1451 | + } |
| 1452 | + return token; |
| 1453 | + } |
| 1454 | + |
| 1455 | + private void resetPasswordWithToken(String newPassword) { |
| 1456 | + byte[] token = getActiveResetPasswordToken(); |
| 1457 | + |
| 1458 | + if (token == null) { |
| 1459 | + Log.e(TAG, "Cannot reset password without token"); |
| 1460 | + mWriter.printf("Cannot reset password without token\n"); |
| 1461 | + return; |
| 1462 | + } |
| 1463 | + |
| 1464 | + boolean result = |
| 1465 | + dpm() |
| 1466 | + .resetPasswordWithToken( |
| 1467 | + DeviceAdminReceiver.getComponentName(mContext), newPassword, token, 0); |
| 1468 | + |
| 1469 | + if (!result) { |
| 1470 | + Log.e(TAG, "Error resetting password"); |
| 1471 | + mWriter.printf("Error resetting password\n"); |
| 1472 | + return; |
| 1473 | + } else { |
| 1474 | + mWriter.printf("Password reset to %s\n", newPassword); |
| 1475 | + } |
| 1476 | + } |
| 1477 | + |
1426 | 1478 | private void setDelegatedScopes(String delegatePackage, String[] scopesArray) {
|
1427 | 1479 | List<String> scopes = Arrays.asList(scopesArray);
|
1428 | 1480 | mDevicePolicyManagerGateway.setDelegatedScopes(delegatePackage, scopes,
|
@@ -1579,4 +1631,8 @@ public String value() {
|
1579 | 1631 | return value;
|
1580 | 1632 | }
|
1581 | 1633 | }
|
| 1634 | + |
| 1635 | + private DevicePolicyManager dpm() { |
| 1636 | + return mDevicePolicyManagerGateway.getDevicePolicyManager(); |
| 1637 | + } |
1582 | 1638 | }
|
0 commit comments