Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix client admin rest call with empty secret - alternative PR #2572

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public static PasswordValidator validator(GenericPasswordPolicy<?> policy,
MessageResolver messageResolver) {
List<Rule> rules = new ArrayList<>();

//length is always a rule. We do not allow blank password
int minLength = Math.max(1, policy.getMinLength());
int minLength = policy.getMinLength()>0 ? policy.getMinLength() : 0;
int maxLength = policy.getMaxLength()>0 ? policy.getMaxLength() : Integer.MAX_VALUE;
rules.add(new LengthRule(minLength, maxLength));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,11 @@ void setUp() {
}

@Test
void min_password_length_is_always_1_if_set_to_0() {
policy.setMinLength(0);
void min_password_length_is_1() {
policy.setMinLength(1);
validatePassword("", "Password must be at least 1 characters in length.");
validatePassword(null, "Password must be at least 1 characters in length.");
}

@Test
void min_password_length_is_always_1_if_not_set() {
policy.setMinLength(-1);
validatePassword("", "Password must be at least 1 characters in length.");
validatePassword(null, "Password must be at least 1 characters in length.");
}


@Test
void testValidateSuccess() {
validatePassword("Password2&");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ZoneAwareClientSecretPolicyValidatorTests {
private ClientSecretPolicy defaultPolicy = new ClientSecretPolicy(0,255,0,0,0,0,6);
private ClientSecretPolicy strictPolicy = new ClientSecretPolicy(6,10,1,1,1,1,6);

private static final String TEST_SECRET_1 = "";
private static final String TEST_EMPTY_SECRET = "";
private static final String TEST_SECRET_2 = "testsecret";
private static final String TEST_SECRET_3 = "VFNTTDEgMB4GA1UEAxMXZnNzLnN0YWdlLmdlY29tcGFueIb3DQEBAQUADDwDG6wkBY" +
"sJSqbSYpw0c76bUB1x5e46eiroRZdU2BEWiQJ9yxV95gGNsdLH1105iubzc9dbxavGIYM9s/+qJRf6WfwDU7VQsURCqIN8eUtnPU808" +
Expand All @@ -37,9 +37,15 @@ void setUp() {
}

@Test
void testEmptyClientSecret() {
void defaultPolicyAcceptsEmptySecret() {
zone.getConfig().setClientSecretPolicy(defaultPolicy);
assertThrows(InvalidClientSecretException.class, () -> validator.validate(TEST_SECRET_1));
validator.validate(TEST_EMPTY_SECRET);
}

@Test
void strictPolicyRejectsEmptySecret() {
zone.getConfig().setClientSecretPolicy(strictPolicy);
assertThrows(InvalidClientSecretException.class, () -> validator.validate(TEST_EMPTY_SECRET));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.cloudfoundry.identity.uaa.resources.SearchResults;
import org.cloudfoundry.identity.uaa.test.TestAccountSetup;
import org.cloudfoundry.identity.uaa.test.UaaTestAccounts;
import org.cloudfoundry.identity.uaa.util.UaaStringUtils;
import org.cloudfoundry.identity.uaa.zone.ClientSecretPolicy;
import org.cloudfoundry.identity.uaa.zone.IdentityZone;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneConfiguration;
Expand Down Expand Up @@ -170,6 +171,21 @@ public void createClientWithSecondarySecret() {
assertEquals(HttpStatus.CREATED, result.getStatusCode());
}

@Test
public void createClientWithEmptySecret() {
OAuth2AccessToken token = getClientCredentialsAccessToken("clients.admin");
HttpHeaders headers = getAuthenticatedHeaders(token);
var client = new ClientDetailsCreation();
client.setClientId(new RandomValueStringGenerator().generate());
client.setClientSecret(UaaStringUtils.EMPTY_STRING);
client.setAuthorizedGrantTypes(List.of("password"));

ResponseEntity<Void> result = serverRunning.getRestTemplate()
.exchange(serverRunning.getUrl("/oauth/clients"), HttpMethod.POST,
new HttpEntity<>(client, headers), Void.class);
assertEquals(HttpStatus.CREATED, result.getStatusCode());
}

@Test
public void testCreateClients() throws Exception {
doCreateClients();
Expand Down
Loading