RHSSO-106: Migration of ResourceOwnerPasswordCredentialsGrantTest
This commit is contained in:
parent
5f16f0ede8
commit
bbe7ff1dce
4 changed files with 128 additions and 106 deletions
|
@ -20,83 +20,41 @@ package org.keycloak.testsuite.oauth;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
import org.junit.ClassRule;
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.keycloak.OAuth2Constants;
|
import org.keycloak.OAuth2Constants;
|
||||||
|
import org.keycloak.admin.client.resource.RealmResource;
|
||||||
import org.keycloak.authentication.authenticators.client.ClientIdAndSecretAuthenticator;
|
import org.keycloak.authentication.authenticators.client.ClientIdAndSecretAuthenticator;
|
||||||
|
import org.keycloak.common.util.Time;
|
||||||
import org.keycloak.events.Details;
|
import org.keycloak.events.Details;
|
||||||
import org.keycloak.events.Errors;
|
import org.keycloak.events.Errors;
|
||||||
import org.keycloak.models.*;
|
import org.keycloak.models.PasswordPolicy;
|
||||||
|
import org.keycloak.models.UserModel;
|
||||||
import org.keycloak.models.utils.KeycloakModelUtils;
|
import org.keycloak.models.utils.KeycloakModelUtils;
|
||||||
import org.keycloak.models.utils.TimeBasedOTP;
|
import org.keycloak.models.utils.TimeBasedOTP;
|
||||||
import org.keycloak.representations.AccessToken;
|
import org.keycloak.representations.AccessToken;
|
||||||
import org.keycloak.representations.RefreshToken;
|
import org.keycloak.representations.RefreshToken;
|
||||||
import org.keycloak.representations.idm.CredentialRepresentation;
|
import org.keycloak.representations.idm.ClientRepresentation;
|
||||||
import org.keycloak.services.managers.RealmManager;
|
import org.keycloak.representations.idm.RealmRepresentation;
|
||||||
|
import org.keycloak.representations.idm.UserRepresentation;
|
||||||
|
import org.keycloak.testsuite.AbstractKeycloakTest;
|
||||||
import org.keycloak.testsuite.AssertEvents;
|
import org.keycloak.testsuite.AssertEvents;
|
||||||
import org.keycloak.testsuite.OAuthClient;
|
import org.keycloak.testsuite.util.ClientBuilder;
|
||||||
import org.keycloak.testsuite.rule.KeycloakRule;
|
import org.keycloak.testsuite.util.ClientManager;
|
||||||
import org.keycloak.testsuite.rule.WebResource;
|
import org.keycloak.testsuite.util.OAuthClient;
|
||||||
import org.keycloak.testsuite.rule.WebRule;
|
import org.keycloak.testsuite.util.RealmBuilder;
|
||||||
import org.keycloak.common.util.Time;
|
import org.keycloak.testsuite.util.RealmManager;
|
||||||
import org.openqa.selenium.WebDriver;
|
import org.keycloak.testsuite.util.UserBuilder;
|
||||||
|
import org.keycloak.testsuite.util.UserManager;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
||||||
*/
|
*/
|
||||||
public class ResourceOwnerPasswordCredentialsGrantTest {
|
public class ResourceOwnerPasswordCredentialsGrantTest extends AbstractKeycloakTest {
|
||||||
|
|
||||||
@ClassRule
|
|
||||||
public static KeycloakRule keycloakRule = new KeycloakRule(new KeycloakRule.KeycloakSetup() {
|
|
||||||
@Override
|
|
||||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
|
||||||
ClientModel app = KeycloakModelUtils.createClient(appRealm, "resource-owner");
|
|
||||||
app.setDirectAccessGrantsEnabled(true);
|
|
||||||
app.setSecret("secret");
|
|
||||||
|
|
||||||
ClientModel app2 = KeycloakModelUtils.createClient(appRealm, "resource-owner-public");
|
|
||||||
app2.setDirectAccessGrantsEnabled(true);
|
|
||||||
app2.setPublicClient(true);
|
|
||||||
|
|
||||||
UserModel user = session.users().addUser(appRealm, "direct-login");
|
|
||||||
user.setEmail("direct-login@localhost");
|
|
||||||
user.setEnabled(true);
|
|
||||||
|
|
||||||
session.users().updateCredential(appRealm, user, UserCredentialModel.password("password"));
|
|
||||||
|
|
||||||
userId = user.getId();
|
|
||||||
|
|
||||||
UserModel user2 = session.users().addUser(appRealm, "direct-login-otp");
|
|
||||||
user2.setEnabled(true);
|
|
||||||
|
|
||||||
UserCredentialModel credentials = new UserCredentialModel();
|
|
||||||
credentials.setType(CredentialRepresentation.TOTP);
|
|
||||||
credentials.setValue("totpSecret");
|
|
||||||
user2.updateCredential(credentials);
|
|
||||||
|
|
||||||
user2.setOtpEnabled(true);
|
|
||||||
|
|
||||||
session.users().updateCredential(appRealm, user2, UserCredentialModel.password("password"));
|
|
||||||
|
|
||||||
userId2 = user2.getId();
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
@Rule
|
|
||||||
public AssertEvents events = new AssertEvents(keycloakRule);
|
|
||||||
|
|
||||||
@Rule
|
|
||||||
public WebRule webRule = new WebRule(this);
|
|
||||||
|
|
||||||
@WebResource
|
|
||||||
protected WebDriver driver;
|
|
||||||
|
|
||||||
@WebResource
|
|
||||||
protected OAuthClient oauth;
|
|
||||||
|
|
||||||
private static String userId;
|
private static String userId;
|
||||||
|
|
||||||
|
@ -104,6 +62,65 @@ public class ResourceOwnerPasswordCredentialsGrantTest {
|
||||||
|
|
||||||
private TimeBasedOTP totp = new TimeBasedOTP();
|
private TimeBasedOTP totp = new TimeBasedOTP();
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public AssertEvents events = new AssertEvents(this);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void beforeAbstractKeycloakTest() throws Exception {
|
||||||
|
super.beforeAbstractKeycloakTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addTestRealms(List<RealmRepresentation> testRealms) {
|
||||||
|
RealmBuilder realm = RealmBuilder.create().name("test")
|
||||||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||||||
|
|
||||||
|
ClientRepresentation app = ClientBuilder.create()
|
||||||
|
.id(KeycloakModelUtils.generateId())
|
||||||
|
.clientId("resource-owner")
|
||||||
|
.directAccessGrants()
|
||||||
|
.secret("secret")
|
||||||
|
.build();
|
||||||
|
realm.client(app);
|
||||||
|
|
||||||
|
ClientRepresentation app2 = ClientBuilder.create()
|
||||||
|
.id(KeycloakModelUtils.generateId())
|
||||||
|
.clientId("resource-owner-public")
|
||||||
|
.directAccessGrants()
|
||||||
|
.publicClient()
|
||||||
|
.build();
|
||||||
|
realm.client(app2);
|
||||||
|
|
||||||
|
UserBuilder defaultUser = UserBuilder.create()
|
||||||
|
.id(KeycloakModelUtils.generateId())
|
||||||
|
.username("test-user@localhost")
|
||||||
|
.password("password");
|
||||||
|
realm.user(defaultUser);
|
||||||
|
|
||||||
|
userId = KeycloakModelUtils.generateId();
|
||||||
|
UserRepresentation user = UserBuilder.create()
|
||||||
|
.id(userId)
|
||||||
|
.username("direct-login")
|
||||||
|
.email("direct-login@localhost")
|
||||||
|
.password("password")
|
||||||
|
.build();
|
||||||
|
realm.user(user);
|
||||||
|
|
||||||
|
userId2 = KeycloakModelUtils.generateId();
|
||||||
|
UserRepresentation user2 = UserBuilder.create()
|
||||||
|
.id(userId2)
|
||||||
|
.username("direct-login-otp")
|
||||||
|
.password("password")
|
||||||
|
.totpSecret("totpSecret")
|
||||||
|
.build();
|
||||||
|
realm.user(user2);
|
||||||
|
|
||||||
|
testRealms.add(realm.build());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void grantAccessTokenUsername() throws Exception {
|
public void grantAccessTokenUsername() throws Exception {
|
||||||
grantAccessToken("direct-login", "resource-owner");
|
grantAccessToken("direct-login", "resource-owner");
|
||||||
|
@ -279,13 +296,8 @@ public class ResourceOwnerPasswordCredentialsGrantTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void grantAccessTokenClientNotAllowed() throws Exception {
|
public void grantAccessTokenClientNotAllowed() throws Exception {
|
||||||
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
|
|
||||||
@Override
|
ClientManager.realm(adminClient.realm("test")).clientId("resource-owner").directAccessGrant(false);
|
||||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
|
||||||
ClientModel client = appRealm.getClientByClientId("resource-owner");
|
|
||||||
client.setDirectAccessGrantsEnabled(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
oauth.clientId("resource-owner");
|
oauth.clientId("resource-owner");
|
||||||
|
|
||||||
|
@ -303,25 +315,15 @@ public class ResourceOwnerPasswordCredentialsGrantTest {
|
||||||
.user((String) null)
|
.user((String) null)
|
||||||
.assertEvent();
|
.assertEvent();
|
||||||
|
|
||||||
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
|
ClientManager.realm(adminClient.realm("test")).clientId("resource-owner").directAccessGrant(true);
|
||||||
@Override
|
|
||||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
|
||||||
ClientModel client = appRealm.getClientByClientId("resource-owner");
|
|
||||||
client.setDirectAccessGrantsEnabled(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void grantAccessTokenVerifyEmail() throws Exception {
|
public void grantAccessTokenVerifyEmail() throws Exception {
|
||||||
|
|
||||||
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
|
RealmResource realmResource = adminClient.realm("test");
|
||||||
@Override
|
RealmManager.realm(realmResource).verifyEmail(true);
|
||||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
|
||||||
appRealm.setVerifyEmail(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
oauth.clientId("resource-owner");
|
oauth.clientId("resource-owner");
|
||||||
|
|
||||||
|
@ -340,25 +342,17 @@ public class ResourceOwnerPasswordCredentialsGrantTest {
|
||||||
.user((String) null)
|
.user((String) null)
|
||||||
.assertEvent();
|
.assertEvent();
|
||||||
|
|
||||||
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
|
RealmManager.realm(realmResource).verifyEmail(false);
|
||||||
@Override
|
UserManager.realm(realmResource).username("test-user@localhost").removeRequiredAction(UserModel.RequiredAction.VERIFY_EMAIL.toString());
|
||||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
|
||||||
appRealm.setVerifyEmail(false);
|
|
||||||
UserModel user = manager.getSession().users().getUserByEmail("test-user@localhost", appRealm);
|
|
||||||
user.removeRequiredAction(UserModel.RequiredAction.VERIFY_EMAIL);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void grantAccessTokenExpiredPassword() throws Exception {
|
public void grantAccessTokenExpiredPassword() throws Exception {
|
||||||
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
|
|
||||||
@Override
|
RealmResource realmResource = adminClient.realm("test");
|
||||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
RealmManager.realm(realmResource).passwordPolicy(
|
||||||
appRealm.setPasswordPolicy(new PasswordPolicy("forceExpiredPasswordChange(1)"));
|
new PasswordPolicy("forceExpiredPasswordChange(1)").toString());
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Time.setOffset(60 * 60 * 48);
|
Time.setOffset(60 * 60 * 48);
|
||||||
|
@ -382,18 +376,12 @@ public class ResourceOwnerPasswordCredentialsGrantTest {
|
||||||
} finally {
|
} finally {
|
||||||
Time.setOffset(0);
|
Time.setOffset(0);
|
||||||
|
|
||||||
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
|
RealmManager.realm(realmResource).passwordPolicy(new PasswordPolicy("").toString());
|
||||||
@Override
|
UserManager.realm(realmResource).username("test-user@localhost")
|
||||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
.removeRequiredAction(UserModel.RequiredAction.UPDATE_PASSWORD.toString());
|
||||||
appRealm.setPasswordPolicy(new PasswordPolicy(""));
|
|
||||||
UserModel user = manager.getSession().users().getUserByEmail("test-user@localhost", appRealm);
|
|
||||||
user.removeRequiredAction(UserModel.RequiredAction.UPDATE_PASSWORD);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void grantAccessTokenInvalidUserCredentials() throws Exception {
|
public void grantAccessTokenInvalidUserCredentials() throws Exception {
|
||||||
oauth.clientId("resource-owner");
|
oauth.clientId("resource-owner");
|
|
@ -4,7 +4,7 @@ import org.keycloak.admin.client.resource.ClientResource;
|
||||||
import org.keycloak.admin.client.resource.RealmResource;
|
import org.keycloak.admin.client.resource.RealmResource;
|
||||||
import org.keycloak.representations.idm.ClientRepresentation;
|
import org.keycloak.representations.idm.ClientRepresentation;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.LinkedHashMap;
|
||||||
|
|
||||||
import static org.keycloak.testsuite.admin.ApiUtil.findClientByClientId;
|
import static org.keycloak.testsuite.admin.ApiUtil.findClientByClientId;
|
||||||
|
|
||||||
|
@ -49,9 +49,17 @@ public class ClientManager {
|
||||||
|
|
||||||
public void updateAttribute(String attribute, String value) {
|
public void updateAttribute(String attribute, String value) {
|
||||||
ClientRepresentation app = clientResource.toRepresentation();
|
ClientRepresentation app = clientResource.toRepresentation();
|
||||||
|
if (app.getAttributes() == null) {
|
||||||
|
app.setAttributes(new LinkedHashMap<String, String>());
|
||||||
|
}
|
||||||
app.getAttributes().put(attribute, value);
|
app.getAttributes().put(attribute, value);
|
||||||
clientResource.update(app);
|
clientResource.update(app);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void directAccessGrant(Boolean enable) {
|
||||||
|
ClientRepresentation app = clientResource.toRepresentation();
|
||||||
|
app.setDirectAccessGrantsEnabled(enable);
|
||||||
|
clientResource.update(app);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,4 +23,16 @@ public class RealmManager {
|
||||||
realmRepresentation.setAccessCodeLifespan(accessCodeLifespan);
|
realmRepresentation.setAccessCodeLifespan(accessCodeLifespan);
|
||||||
realm.update(realmRepresentation);
|
realm.update(realmRepresentation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void verifyEmail(Boolean enabled) {
|
||||||
|
RealmRepresentation rep = realm.toRepresentation();
|
||||||
|
rep.setVerifyEmail(enabled);
|
||||||
|
realm.update(rep);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void passwordPolicy(String passwordPolicy) {
|
||||||
|
RealmRepresentation rep = realm.toRepresentation();
|
||||||
|
rep.setPasswordPolicy(passwordPolicy);
|
||||||
|
realm.update(rep);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -114,6 +114,20 @@ public class UserBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UserBuilder totpSecret(String totpSecret) {
|
||||||
|
if (rep.getCredentials() == null) {
|
||||||
|
rep.setCredentials(new LinkedList<CredentialRepresentation>());
|
||||||
|
}
|
||||||
|
|
||||||
|
CredentialRepresentation credential = new CredentialRepresentation();
|
||||||
|
credential.setType(CredentialRepresentation.TOTP);
|
||||||
|
credential.setValue(totpSecret);
|
||||||
|
|
||||||
|
rep.getCredentials().add(credential);
|
||||||
|
rep.setTotp(true);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public UserRepresentation build() {
|
public UserRepresentation build() {
|
||||||
return rep;
|
return rep;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue