Skip to content

Commit f2c881a

Browse files
jscott1989pfmaggi
authored andcommitted
No public description
PiperOrigin-RevId: 583370474
1 parent 72306c3 commit f2c881a

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/main/java/com/afwsamples/testdpc/ShellCommand.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import android.annotation.TargetApi;
2525
import android.app.admin.ConnectEvent;
26+
import android.app.admin.DevicePolicyManager;
2627
import android.app.admin.DnsEvent;
2728
import android.app.admin.NetworkEvent;
2829
import android.app.admin.SecurityLog.SecurityEvent;
@@ -44,6 +45,7 @@
4445
import androidx.annotation.Nullable;
4546
import com.afwsamples.testdpc.common.Util;
4647
import com.afwsamples.testdpc.policy.SecurityLogsFragment;
48+
import com.afwsamples.testdpc.policy.resetpassword.ResetPasswordWithTokenFragment;
4749
import com.afwsamples.testdpc.util.flags.Flags;
4850
import java.io.File;
4951
import java.io.PrintWriter;
@@ -589,6 +591,12 @@ public void run() {
589591
command("get-delegate-packages", this::getDelegatePackages,
590592
ordinalParam(String.class, "scope"))
591593
.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"));
592600

593601
// Separator for S / pre-S commands - do NOT remove line to avoid cherry-pick conflicts
594602

@@ -1423,6 +1431,50 @@ private void getDelegatePackages(String delegationScope) {
14231431
printCollection("package", packages);
14241432
}
14251433

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+
14261478
private void setDelegatedScopes(String delegatePackage, String[] scopesArray) {
14271479
List<String> scopes = Arrays.asList(scopesArray);
14281480
mDevicePolicyManagerGateway.setDelegatedScopes(delegatePackage, scopes,
@@ -1579,4 +1631,8 @@ public String value() {
15791631
return value;
15801632
}
15811633
}
1634+
1635+
private DevicePolicyManager dpm() {
1636+
return mDevicePolicyManagerGateway.getDevicePolicyManager();
1637+
}
15821638
}

0 commit comments

Comments
 (0)