Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

CFID-273: add remote scim provisioning

Change-Id: Ia2d4ba0951d90f542c9f3706ea643c56a1ef7abe
  • Loading branch information...
commit 0df579407413d49cfa6e93d59531cbf75f9eff4f 1 parent 11ec86a
@dsyer authored
View
94 common/src/main/java/org/cloudfoundry/identity/uaa/scim/RemoteScimUserProvisioning.java
@@ -0,0 +1,94 @@
+/*
+ * Cloud Foundry 2012.02.03 Beta
+ * Copyright (c) [2009-2012] VMware, Inc. All Rights Reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0 (the "License").
+ * You may not use this product except in compliance with the License.
+ *
+ * This product includes a number of subcomponents with
+ * separate copyright notices and license terms. Your use of these
+ * subcomponents is subject to the terms and conditions of the
+ * subcomponent's license, as noted in the LICENSE file.
+ */
+
+package org.cloudfoundry.identity.uaa.scim;
+
+import java.util.List;
+
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.web.client.RestOperations;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * Remote implementation of {@link ScimUserProvisioning} using the Scim endpoints on a remote server.
+ *
+ * @author Dave Syer
+ *
+ */
+public class RemoteScimUserProvisioning implements ScimUserProvisioning {
+
+ private RestOperations restTemplate = new RestTemplate();
+
+ private String baseUrl = "https://uaa.cloudfoundry.com";
+
+ /**
+ * @param restTemplate the rest template to set
+ */
+ public void setRestTemplate(RestOperations restTemplate) {
+ this.restTemplate = restTemplate;
+ }
+
+ /**
+ * @param baseUrl the base url to set to the SCIM server
+ */
+ public void setBaseUrl(String baseUrl) {
+ this.baseUrl = baseUrl;
+ }
+
+ @Override
+ public ScimUser retrieveUser(String id) throws UserNotFoundException {
+ return restTemplate.getForObject(baseUrl + "/User/{id}", ScimUser.class, id);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public List<ScimUser> retrieveUsers() {
+ return restTemplate.getForObject(baseUrl + "/Users", List.class);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public List<ScimUser> retrieveUsers(String filter) {
+ return restTemplate.getForObject(baseUrl + "/Users?filter={filter}", List.class, filter);
+ }
+
+ @Override
+ public ScimUser createUser(ScimUser user, String password) throws InvalidPasswordException, InvalidUserException {
+ user.setPassword(password);
+ return restTemplate.postForObject(baseUrl + "/User", user, ScimUser.class);
+ }
+
+ @Override
+ public ScimUser updateUser(String id, ScimUser user) throws InvalidUserException, UserNotFoundException {
+ restTemplate.put(baseUrl + "/User/{id}", user, id);
+ return user;
+ }
+
+ @Override
+ public boolean changePassword(String id, String oldPassword, String newPassword) throws UserNotFoundException {
+ PasswordChangeRequest request = new PasswordChangeRequest();
+ request.setOldPassword(oldPassword);
+ request.setPassword(newPassword);
+ restTemplate.put(baseUrl + "/User/{id}/password", request, id);
+ return true;
+ }
+
+ @Override
+ public ScimUser removeUser(String id, int version) throws UserNotFoundException {
+ HttpHeaders headers = new HttpHeaders();
+ headers.set("If-Match", String.format("%d", version));
+ return restTemplate.exchange(baseUrl + "/User/{id}", HttpMethod.DELETE, new HttpEntity<Void>(headers), ScimUser.class, id).getBody();
+ }
+}
View
4 common/src/main/java/org/cloudfoundry/identity/uaa/scim/ScimUser.java
@@ -231,6 +231,10 @@ public String getUserName() {
public String getPassword() {
return password;
}
+
+ protected void setPassword(String password) {
+ this.password = password;
+ }
public void setUserName(String userName) {
this.userName = userName.toLowerCase();
View
120 common/src/test/java/org/cloudfoundry/identity/uaa/scim/RemoteScimUserProvisioningTests.java
@@ -0,0 +1,120 @@
+/*
+ * Cloud Foundry 2012.02.03 Beta
+ * Copyright (c) [2009-2012] VMware, Inc. All Rights Reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0 (the "License").
+ * You may not use this product except in compliance with the License.
+ *
+ * This product includes a number of subcomponents with
+ * separate copyright notices and license terms. Your use of these
+ * subcomponents is subject to the terms and conditions of the
+ * subcomponent's license, as noted in the LICENSE file.
+ */
+
+package org.cloudfoundry.identity.uaa.scim;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentMatcher;
+import org.mockito.Mockito;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestOperations;
+
+/**
+ * @author Dave Syer
+ *
+ */
+public class RemoteScimUserProvisioningTests {
+
+ private RemoteScimUserProvisioning service = new RemoteScimUserProvisioning();
+
+ private RestOperations restTemplate = Mockito.mock(RestOperations.class);
+
+ private ScimUser user;
+
+ @Before
+ public void start() {
+ service.setRestTemplate(restTemplate);
+ service.setBaseUrl("http://base");
+ user = new ScimUser("1234", "foo", "Foo", "Bar");
+ user.addEmail("foo@bar.com");
+ }
+
+ @After
+ public void stop() {
+ }
+
+ @Test
+ public void testRetrieveUser() {
+ service.retrieveUser("1234");
+ Mockito.verify(restTemplate).getForObject("http://base/User/{id}", ScimUser.class, "1234");
+ }
+
+ @Test
+ public void testRetrieveUsers() {
+ service.retrieveUsers();
+ Mockito.verify(restTemplate).getForObject("http://base/Users", List.class);
+ }
+
+ @Test
+ public void testFilterUsers() {
+ service.retrieveUsers("name eq 'foo'");
+ Mockito.verify(restTemplate).getForObject("http://base/Users?filter={filter}", List.class, "name eq 'foo'");
+ }
+
+ @Test
+ public void testCreateUser() {
+ service.createUser(user, "password");
+ assertEquals("password", user.getPassword());
+ Mockito.verify(restTemplate).postForObject("http://base/User", user, ScimUser.class);
+ }
+
+ @Test
+ public void testChangePassword() {
+ service.changePassword("1234", "oldPassword", "newPassword");
+ Mockito.verify(restTemplate).put(Mockito.eq("http://base/User/{id}/password"),
+ Mockito.any(PasswordChangeRequest.class), Mockito.eq("1234"));
+ }
+
+ @Test
+ public void testUpdateUser() {
+ service.updateUser("1234", user);
+ Mockito.verify(restTemplate).put("http://base/User/{id}", user, "1234");
+ }
+
+ @Test
+ public void testRemoveUser() {
+ HttpHeaders headers = new HttpHeaders();
+ headers.set("If-Match", "123456789");
+ Mockito.when(
+ restTemplate.exchange(Mockito.eq("http://base/User/{id}"), Mockito.eq(HttpMethod.DELETE),
+ Mockito.argThat(new HttpHeadersMatcher()), Mockito.eq(ScimUser.class), Mockito.eq("1234")))
+ .thenReturn(new ResponseEntity<ScimUser>(user, HttpStatus.OK));
+ service.removeUser("1234", 123456789);
+ }
+
+ private static class HttpHeadersMatcher extends ArgumentMatcher<HttpEntity<Void>> {
+
+ private String key = "If-Match";
+
+ private String value = "123456789";
+
+ @Override
+ public boolean matches(Object argument) {
+ @SuppressWarnings("unchecked")
+ String actual = ((HttpEntity<Void>) argument).getHeaders().getFirst(key);
+ return actual.equals(value);
+ }
+
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.