Merge pull request #1990 from raehalme/KEYCLOAK-2257

KEYCLOAK-2257: Store user attribute when accepting terms and conditions
This commit is contained in:
Stian Thorgersen 2016-01-11 09:03:58 +01:00
commit 7c8bc5814e
2 changed files with 42 additions and 3 deletions

View file

@ -1,9 +1,14 @@
package org.keycloak.authentication.requiredactions;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import org.keycloak.Config;
import org.keycloak.authentication.RequiredActionContext;
import org.keycloak.authentication.RequiredActionFactory;
import org.keycloak.authentication.RequiredActionProvider;
import org.keycloak.common.util.Time;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
@ -14,8 +19,8 @@ import javax.ws.rs.core.Response;
* @version $Revision: 1 $
*/
public class TermsAndConditions implements RequiredActionProvider, RequiredActionFactory {
public static final String PROVIDER_ID = "terms_and_conditions";
public static final String USER_ATTRIBUTE = PROVIDER_ID;
@Override
public RequiredActionProvider create(KeycloakSession session) {
@ -53,11 +58,14 @@ public class TermsAndConditions implements RequiredActionProvider, RequiredActio
@Override
public void processAction(RequiredActionContext context) {
if (context.getHttpRequest().getDecodedFormParameters().containsKey("cancel")) {
context.getUser().removeAttribute(USER_ATTRIBUTE);
context.failure();
return;
}
context.success();
context.getUser().setAttribute(USER_ATTRIBUTE, Arrays.asList(Integer.toString(Time.currentTime())));
context.success();
}
@Override

View file

@ -32,6 +32,7 @@ import org.keycloak.events.Errors;
import org.keycloak.events.EventType;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.pages.AppPage;
@ -44,6 +45,11 @@ import org.keycloak.testsuite.rule.WebResource;
import org.keycloak.testsuite.rule.WebRule;
import org.openqa.selenium.WebDriver;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
@ -96,6 +102,23 @@ public class TermsAndConditionsTest {
Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
events.expectLogin().session(sessionId).assertEvent();
// assert user attribute is properly set
UserRepresentation user = keycloakRule.getUser("test", "test-user@localhost");
Map<String,List<String>> attributes = user.getAttributesAsListValues();
assertNotNull("timestamp for terms acceptance was not stored in user attributes", attributes);
List<String> termsAndConditions = attributes.get(TermsAndConditions.USER_ATTRIBUTE);
assertTrue("timestamp for terms acceptance was not stored in user attributes as "
+ TermsAndConditions.USER_ATTRIBUTE, termsAndConditions.size() == 1);
String timestamp = termsAndConditions.get(0);
assertNotNull("expected non-null timestamp for terms acceptance in user attribute "
+ TermsAndConditions.USER_ATTRIBUTE, timestamp);
try {
Integer.parseInt(timestamp);
}
catch (NumberFormatException e) {
fail("timestamp for terms acceptance is not a valid integer: '" + timestamp + "'");
}
}
@Test
@ -113,6 +136,14 @@ public class TermsAndConditionsTest {
.removeDetail(Details.CONSENT)
.assertEvent();
// assert user attribute is properly removed
UserRepresentation user = keycloakRule.getUser("test", "test-user@localhost");
Map<String,List<String>> attributes = user.getAttributesAsListValues();
if (attributes != null) {
assertNull("expected null for terms acceptance user attribute " + TermsAndConditions.USER_ATTRIBUTE,
attributes.get(TermsAndConditions.USER_ATTRIBUTE));
}
}