This is an automated email from the ASF dual-hosted git repository.
jbonofre pushed a commit to branch karaf-4.1.x
in repository https://gitbox.apache.org/repos/asf/karaf.git
The following commit(s) were added to refs/heads/karaf-4.1.x by this push:
new df41ea3 [KARAF-5495] Support Syncope 2.x in SyncopeBackingEngine
df41ea3 is described below
commit df41ea3d0ec7fe50708a4db12be861089a21f5dd
Author: Jean-Baptiste Onofré <[email protected]>
AuthorDate: Sun Nov 26 06:42:29 2017 +0100
[KARAF-5495] Support Syncope 2.x in SyncopeBackingEngine
---
.../jaas/modules/syncope/SyncopeBackingEngine.java | 97 +++++++++++++++++++++-
.../syncope/SyncopeBackingEngineFactory.java | 3 +-
2 files changed, 97 insertions(+), 3 deletions(-)
diff --git
a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/syncope/SyncopeBackingEngine.java
b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/syncope/SyncopeBackingEngine.java
index d3121ca..2da4acd 100644
---
a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/syncope/SyncopeBackingEngine.java
+++
b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/syncope/SyncopeBackingEngine.java
@@ -15,6 +15,7 @@
*/
package org.apache.karaf.jaas.modules.syncope;
+import org.apache.felix.utils.json.JSONParser;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
@@ -43,11 +44,13 @@ public class SyncopeBackingEngine implements BackingEngine {
private final Logger logger =
LoggerFactory.getLogger(SyncopeBackingEngine.class);
private String address;
+ private boolean version2;
private DefaultHttpClient client;
- public SyncopeBackingEngine(String address, String adminUser, String
adminPassword) {
+ public SyncopeBackingEngine(String address, String version, String
adminUser, String adminPassword) {
this.address = address;
+ version2 = version != null && (version.equals("2.x") ||
version.equals("2"));
client = new DefaultHttpClient();
Credentials creds = new UsernamePasswordCredentials(adminUser,
adminPassword);
@@ -58,6 +61,14 @@ public class SyncopeBackingEngine implements BackingEngine {
if (username.startsWith(GROUP_PREFIX)) {
throw new IllegalArgumentException("Group prefix " + GROUP_PREFIX
+ " not permitted with Syncope backend");
}
+ if (version2) {
+ addUserSyncope2(username, password);
+ } else {
+ addUserSyncope1(username, password);
+ }
+ }
+
+ private void addUserSyncope1(String username, String password) {
HttpPost request = new HttpPost(address + "/users");
request.setHeader("Content-Type", "application/xml");
String userTO = "<?xml version=\"1.0\" encoding=\"UTF-8\"
standalone=\"yes\"?>" +
@@ -80,12 +91,40 @@ public class SyncopeBackingEngine implements BackingEngine {
}
}
+ private void addUserSyncope2(String username, String password) {
+ HttpPost request = new HttpPost(address + "/users");
+ request.setHeader("Content-Type", "application/json");
+ String userTO = "{" +
+ "\"@class\": \"org.apache.syncope.common.lib.to.UserTO\"," +
+ "\"type\": \"USER\"," +
+ "\"realm\": \"/\"," +
+ "\"username\": \"" + username + "\"," +
+ "\"password\": \"" + password + "\"," +
+ "\"plainAttrs\": [" +
+ "{ \"schema\": \"surname\", \"values\": [\"" + username +
"\"] }," +
+ "{ \"schema\": \"fullname\", \"values\": [\"" + username +
"\"] }," +
+ "{ \"schema\": \"userId\", \"value\": [\"" + username +
"@karaf.apache.org\"] }" +
+ "}";
+ try {
+ StringEntity entity = new StringEntity(userTO);
+ request.setEntity(entity);
+ HttpResponse response = client.execute(request);
+ } catch (Exception e) {
+ logger.error("Can't add user {}", username, e);
+ throw new RuntimeException("Can't add user " + username, e);
+ }
+ }
+
public void deleteUser(String username) {
if (username.startsWith(GROUP_PREFIX)) {
throw new IllegalArgumentException("Group prefix " + GROUP_PREFIX
+ " not permitted with Syncope backend");
}
HttpDelete request = new HttpDelete(address + "/users/" + username);
- request.setHeader("Content-Type", "application/xml");
+ if (version2) {
+ request.setHeader("Content-Type", "application/json");
+ } else {
+ request.setHeader("Content-Type", "application/xml");
+ }
try {
client.execute(request);
} catch (Exception e) {
@@ -95,6 +134,14 @@ public class SyncopeBackingEngine implements BackingEngine {
}
public List<UserPrincipal> listUsers() {
+ if (version2) {
+ return listUsersSyncope2();
+ } else {
+ return listUsersSyncope1();
+ }
+ }
+
+ private List<UserPrincipal> listUsersSyncope1() {
List<UserPrincipal> users = new ArrayList<>();
HttpGet request = new HttpGet(address + "/users");
request.setHeader("Content-Type", "application/xml");
@@ -122,7 +169,33 @@ public class SyncopeBackingEngine implements BackingEngine
{
return users;
}
+ private List<UserPrincipal> listUsersSyncope2() {
+ List<UserPrincipal> users = new ArrayList<>();
+ HttpGet request = new HttpGet(address + "/users");
+ request.setHeader("Content-Type", "application/json");
+ try {
+ HttpResponse httpResponse = client.execute(request);
+ String response = EntityUtils.toString(httpResponse.getEntity());
+ JSONParser parser = new JSONParser(response);
+ List<Map<String, Object>> results = (List<Map<String, Object>>)
parser.getParsed().get("result");
+ for (Map<String, Object> result : results) {
+ users.add(new UserPrincipal((String) result.get("username")));
+ }
+ } catch (Exception e) {
+ throw new RuntimeException("Error listing users", e);
+ }
+ return users;
+ }
+
public List<RolePrincipal> listRoles(Principal principal) {
+ if (version2) {
+ return listRolesSyncope2(principal);
+ } else {
+ return listRolesSyncope1(principal);
+ }
+ }
+
+ private List<RolePrincipal> listRolesSyncope1(Principal principal) {
List<RolePrincipal> roles = new ArrayList<>();
HttpGet request = new HttpGet(address + "/users?username=" +
principal.getName());
request.setHeader("Content-Type", "application/xml");
@@ -150,6 +223,26 @@ public class SyncopeBackingEngine implements BackingEngine
{
return roles;
}
+ private List<RolePrincipal> listRolesSyncope2(Principal principal) {
+ List<RolePrincipal> result = new ArrayList<>();
+ HttpGet request = new HttpGet(address + "/users/" +
principal.getName());
+ request.setHeader("Content-Type", "application/json");
+ try {
+ HttpResponse httpResponse = client.execute(request);
+ String response = EntityUtils.toString(httpResponse.getEntity());
+ if (response != null && !response.isEmpty()) {
+ JSONParser parser = new JSONParser(response);
+ List<String> roles = (List<String>)
parser.getParsed().get("roles");
+ for (String role : roles) {
+ result.add(new RolePrincipal(role));
+ }
+ }
+ } catch (Exception e) {
+ throw new RuntimeException("Error listing roles", e);
+ }
+ return result;
+ }
+
public void addRole(String username, String role) {
throw new RuntimeException("Roles management should be done on the
Syncope side");
}
diff --git
a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/syncope/SyncopeBackingEngineFactory.java
b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/syncope/SyncopeBackingEngineFactory.java
index f3a85a5..3bef2de 100644
---
a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/syncope/SyncopeBackingEngineFactory.java
+++
b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/syncope/SyncopeBackingEngineFactory.java
@@ -31,9 +31,10 @@ public class SyncopeBackingEngineFactory implements
BackingEngineFactory {
String address = (String) options.get(SyncopeLoginModule.ADDRESS);
String adminUser = (String) options.get(SyncopeLoginModule.ADMIN_USER);
String adminPassword = (String)
options.get(SyncopeLoginModule.ADMIN_PASSWORD);
+ String version = (String) options.get(SyncopeLoginModule.VERSION);
try {
- instance = new SyncopeBackingEngine(address, adminUser,
adminPassword);
+ instance = new SyncopeBackingEngine(address, version, adminUser,
adminPassword);
} catch (Exception e) {
LOGGER.error("Error creating the Syncope backing engine", e);
}
--
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].