|
| 1 | +package com.descope; |
| 2 | + |
| 3 | +import com.descope.client.DescopeClient; |
| 4 | +import com.descope.exception.DescopeException; |
| 5 | +import com.descope.model.passwordsettings.PasswordSettings; |
| 6 | + |
| 7 | +public class CheckPwdPolicy { |
| 8 | + public static void main(String[] args) { |
| 9 | + if (args.length != 2) { |
| 10 | + System.err.println("Usage: CheckPwdPolicy <loginId> <password>"); |
| 11 | + System.exit(1); |
| 12 | + } |
| 13 | + try { |
| 14 | + var client = new DescopeClient(); |
| 15 | + var userService = client.getManagementServices().getUserService(); |
| 16 | + var pwdPolicyService = client.getManagementServices().getPasswordSettingsService(); |
| 17 | + var res = userService.load(args[0]); |
| 18 | + var commonPasswordSettings = PasswordSettings.builder() |
| 19 | + .enabled(true) |
| 20 | + .minLength(Integer.MIN_VALUE) |
| 21 | + .expirationWeeks(Integer.MAX_VALUE) |
| 22 | + .reuseAmount(Integer.MIN_VALUE) |
| 23 | + .lockAttempts(Integer.MAX_VALUE) |
| 24 | + .build(); |
| 25 | + if (res.getUser() != null && res.getUser().getUserTenants() != null) { |
| 26 | + for (var tenant : res.getUser().getUserTenants()) { |
| 27 | + var policy = pwdPolicyService.getSettings(tenant.getTenantId()); |
| 28 | + if (policy != null && policy.getEnabled()) { |
| 29 | + if (policy.getMinLength() != null && policy.getMinLength() > commonPasswordSettings.getMinLength()) { |
| 30 | + commonPasswordSettings.setMinLength(policy.getMinLength()); |
| 31 | + } |
| 32 | + if (policy.getLowercase() != null && policy.getLowercase()) { |
| 33 | + commonPasswordSettings.setLowercase(policy.getLowercase()); |
| 34 | + } |
| 35 | + if (policy.getUppercase() != null && policy.getUppercase()) { |
| 36 | + commonPasswordSettings.setUppercase(policy.getUppercase()); |
| 37 | + } |
| 38 | + if (policy.getNumber() != null && policy.getNumber()) { |
| 39 | + commonPasswordSettings.setNumber(policy.getNumber()); |
| 40 | + } |
| 41 | + if (policy.getNonAlphanumeric() != null && policy.getNonAlphanumeric()) { |
| 42 | + commonPasswordSettings.setNonAlphanumeric(policy.getNonAlphanumeric()); |
| 43 | + } |
| 44 | + if (policy.getExpiration() != null && policy.getExpiration()) { |
| 45 | + commonPasswordSettings.setExpiration(policy.getExpiration()); |
| 46 | + if (policy.getExpirationWeeks() != null |
| 47 | + && policy.getExpirationWeeks() < commonPasswordSettings.getExpirationWeeks()) { |
| 48 | + commonPasswordSettings.setExpirationWeeks(policy.getExpirationWeeks()); |
| 49 | + } |
| 50 | + } |
| 51 | + if (policy.getReuse() != null && policy.getReuse()) { |
| 52 | + commonPasswordSettings.setReuse(policy.getReuse()); |
| 53 | + if (policy.getReuseAmount() != null |
| 54 | + && policy.getReuseAmount() > commonPasswordSettings.getReuseAmount()) { |
| 55 | + commonPasswordSettings.setReuseAmount(policy.getReuseAmount()); |
| 56 | + } |
| 57 | + } |
| 58 | + if (policy.getLock() != null && policy.getLock()) { |
| 59 | + commonPasswordSettings.setLock(policy.getLock()); |
| 60 | + if (policy.getLockAttempts() != null |
| 61 | + && policy.getLockAttempts() < commonPasswordSettings.getLockAttempts()) { |
| 62 | + commonPasswordSettings.setLockAttempts(policy.getLockAttempts()); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + if (args[1].length() < commonPasswordSettings.getMinLength()) { |
| 68 | + System.out.println("Password does not meet the minimum length requirement"); |
| 69 | + } |
| 70 | + if (commonPasswordSettings.getLowercase() && !args[1].matches(".*[a-z].*")) { |
| 71 | + System.out.println("Password does not contain lowercase characters"); |
| 72 | + } |
| 73 | + if (commonPasswordSettings.getUppercase() && !args[1].matches(".*[A-Z].*")) { |
| 74 | + System.out.println("Password does not contain uppercase characters"); |
| 75 | + } |
| 76 | + if (commonPasswordSettings.getNumber() && !args[1].matches(".*[0-9].*")) { |
| 77 | + System.out.println("Password does not contain numeric characters"); |
| 78 | + } |
| 79 | + if (commonPasswordSettings.getNonAlphanumeric() && args[1].matches("[a-zA-Z0-9]*")) { |
| 80 | + System.out.println("Password does not contain non-alphanumeric characters"); |
| 81 | + } |
| 82 | + } |
| 83 | + } catch (DescopeException de) { |
| 84 | + System.err.println(String.format("%s - %s", de.getCode(), de.getMessage())); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments