coheigea commented on a change in pull request #52:
URL: https://github.com/apache/cxf-fediz/pull/52#discussion_r439369489



##########
File path: 
services/oidc/src/main/java/org/apache/cxf/fediz/service/oidc/clients/ClientRegistrationService.java
##########
@@ -310,66 +317,130 @@ public Response registerForm(@FormParam("client_name") 
String appName,
             // Client Registration Time
             newClient.setRegisteredAt(System.currentTimeMillis() / 1000);
 
-            // Client Realm
-            if (homeRealm != null) {
-                newClient.setHomeRealm(homeRealm);
-                if (homeRealms.containsKey(homeRealm)) {
-                    newClient.getProperties().put("homeRealmAlias", 
homeRealms.get(homeRealm));
-                }
+            updateClientDetails(newClient, audience, redirectURI, logoutURI, 
homeRealm);
+
+            // Client Scopes
+            if (clientScopes != null && !clientScopes.isEmpty()) {
+                newClient.setRegisteredScopes(new 
ArrayList<>(clientScopes.keySet()));
             }
 
-            // Client Redirect URIs
-            if (!StringUtils.isEmpty(redirectURI)) {
-                String[] allUris = redirectURI.trim().split(" ");
-                List<String> redirectUris = new ArrayList<>(allUris.length);
-                for (String uri : allUris) {
-                    if (!StringUtils.isEmpty(uri)) {
-                        if (!isValidURI(uri, false)) {
-                            throwInvalidRegistrationException("An invalid 
redirect URI was specified: "
-                                + StringEscapeUtils.escapeHtml4(uri));
-                        }
-                        redirectUris.add(uri);
+            return Response.ok(registerNewClient(newClient)).build();
+        } catch (InvalidRegistrationException ex) {
+            // For the view handlers to handle it
+            return Response.ok(new 
InvalidRegistration(ex.getMessage())).build();
+        }
+    }
+
+    @POST
+    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
+    @Produces(MediaType.TEXT_HTML)
+    @Path("/{id}")
+    public Response editForm(@PathParam("id") String clientId,
+                             @FormParam("client_name") String appName,
+                             @FormParam("client_audience") String audience,
+                             @FormParam("client_redirectURI") String 
redirectURI,
+                             @FormParam("client_logoutURI") String logoutURI,
+                             @FormParam("client_homeRealm") String homeRealm,
+                             @FormParam("client_csrfToken") String csrfToken
+    ) {
+        try {
+            // CSRF
+            checkCSRFToken(csrfToken);
+            // checkSecurityContext();
+            Client client = getRegisteredClient(clientId);
+
+            // Client Name
+            if (StringUtils.isEmpty(appName)) {
+                throw new InvalidRegistrationException("The client name must 
not be empty");
+            }
+
+            updateClientDetails(client, audience, redirectURI, logoutURI, 
homeRealm);
+
+            if (!client.getApplicationName().equals(appName)) {
+                Collection<Client> clientRegistrations = 
getClientRegistrations(
+                    client.getResourceOwnerSubject().getLogin());
+                for (Iterator<Client> it = clientRegistrations.iterator(); 
it.hasNext();) {
+                    Client c = it.next();
+                    if (c.getClientId().equals(clientId)) {
+                        it.remove();
+                        break;
                     }
                 }
-                newClient.setRedirectUris(redirectUris);
+                client.setApplicationName(appName);
+                updateClientApplicationName(client, clientRegistrations);
+                clientRegistrations.add(client);
             }
-            // Client Logout URI
-            if (!StringUtils.isEmpty(logoutURI)) {
-                String[] logoutUris = logoutURI.split(" ");
-                for (String uri : logoutUris) {
+
+            clientProvider.setClient(client);
+
+            return Response.ok(client).build();
+        } catch (InvalidRegistrationException ex) {
+            // For the view handlers to handle it
+            return Response.ok(new 
InvalidRegistration(ex.getMessage())).build();
+        }
+    }
+
+    private void updateClientDetails(final Client client,
+        String audience, String redirectURI, String logoutURI, String 
homeRealm) {
+        // Client Redirect URIs
+        if (!StringUtils.isEmpty(redirectURI)) {
+            String[] allUris = redirectURI.trim().split(" ");
+            List<String> redirectUris = new ArrayList<>(allUris.length);
+            for (String uri : allUris) {
+                if (!StringUtils.isEmpty(uri)) {
                     if (!isValidURI(uri, false)) {
-                        throwInvalidRegistrationException("An invalid logout 
URI was specified: "
+                        throw new InvalidRegistrationException("An invalid 
redirect URI was specified: "
                             + StringEscapeUtils.escapeHtml4(uri));
                     }
+                    redirectUris.add(uri);
                 }
-                //TODO: replace this code with newClient.setLogoutUri() once 
it becomes available
-                newClient.getProperties().put("post_logout_redirect_uris", 
logoutURI);
             }
+            client.setRedirectUris(redirectUris);
+        } else {
+            client.setRedirectUris(null);

Review comment:
       Change null to an empty list here.

##########
File path: 
services/oidc/src/main/java/org/apache/cxf/fediz/service/oidc/clients/ClientRegistrationService.java
##########
@@ -310,66 +317,130 @@ public Response registerForm(@FormParam("client_name") 
String appName,
             // Client Registration Time
             newClient.setRegisteredAt(System.currentTimeMillis() / 1000);
 
-            // Client Realm
-            if (homeRealm != null) {
-                newClient.setHomeRealm(homeRealm);
-                if (homeRealms.containsKey(homeRealm)) {
-                    newClient.getProperties().put("homeRealmAlias", 
homeRealms.get(homeRealm));
-                }
+            updateClientDetails(newClient, audience, redirectURI, logoutURI, 
homeRealm);
+
+            // Client Scopes
+            if (clientScopes != null && !clientScopes.isEmpty()) {
+                newClient.setRegisteredScopes(new 
ArrayList<>(clientScopes.keySet()));
             }
 
-            // Client Redirect URIs
-            if (!StringUtils.isEmpty(redirectURI)) {
-                String[] allUris = redirectURI.trim().split(" ");
-                List<String> redirectUris = new ArrayList<>(allUris.length);
-                for (String uri : allUris) {
-                    if (!StringUtils.isEmpty(uri)) {
-                        if (!isValidURI(uri, false)) {
-                            throwInvalidRegistrationException("An invalid 
redirect URI was specified: "
-                                + StringEscapeUtils.escapeHtml4(uri));
-                        }
-                        redirectUris.add(uri);
+            return Response.ok(registerNewClient(newClient)).build();
+        } catch (InvalidRegistrationException ex) {
+            // For the view handlers to handle it
+            return Response.ok(new 
InvalidRegistration(ex.getMessage())).build();
+        }
+    }
+
+    @POST
+    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
+    @Produces(MediaType.TEXT_HTML)
+    @Path("/{id}")
+    public Response editForm(@PathParam("id") String clientId,
+                             @FormParam("client_name") String appName,
+                             @FormParam("client_audience") String audience,
+                             @FormParam("client_redirectURI") String 
redirectURI,
+                             @FormParam("client_logoutURI") String logoutURI,
+                             @FormParam("client_homeRealm") String homeRealm,
+                             @FormParam("client_csrfToken") String csrfToken
+    ) {
+        try {
+            // CSRF
+            checkCSRFToken(csrfToken);
+            // checkSecurityContext();
+            Client client = getRegisteredClient(clientId);
+
+            // Client Name
+            if (StringUtils.isEmpty(appName)) {
+                throw new InvalidRegistrationException("The client name must 
not be empty");
+            }
+
+            updateClientDetails(client, audience, redirectURI, logoutURI, 
homeRealm);
+
+            if (!client.getApplicationName().equals(appName)) {
+                Collection<Client> clientRegistrations = 
getClientRegistrations(
+                    client.getResourceOwnerSubject().getLogin());
+                for (Iterator<Client> it = clientRegistrations.iterator(); 
it.hasNext();) {
+                    Client c = it.next();
+                    if (c.getClientId().equals(clientId)) {
+                        it.remove();
+                        break;
                     }
                 }
-                newClient.setRedirectUris(redirectUris);
+                client.setApplicationName(appName);
+                updateClientApplicationName(client, clientRegistrations);
+                clientRegistrations.add(client);
             }
-            // Client Logout URI
-            if (!StringUtils.isEmpty(logoutURI)) {
-                String[] logoutUris = logoutURI.split(" ");
-                for (String uri : logoutUris) {
+
+            clientProvider.setClient(client);
+
+            return Response.ok(client).build();
+        } catch (InvalidRegistrationException ex) {
+            // For the view handlers to handle it
+            return Response.ok(new 
InvalidRegistration(ex.getMessage())).build();
+        }
+    }
+
+    private void updateClientDetails(final Client client,
+        String audience, String redirectURI, String logoutURI, String 
homeRealm) {
+        // Client Redirect URIs
+        if (!StringUtils.isEmpty(redirectURI)) {
+            String[] allUris = redirectURI.trim().split(" ");
+            List<String> redirectUris = new ArrayList<>(allUris.length);
+            for (String uri : allUris) {
+                if (!StringUtils.isEmpty(uri)) {
                     if (!isValidURI(uri, false)) {
-                        throwInvalidRegistrationException("An invalid logout 
URI was specified: "
+                        throw new InvalidRegistrationException("An invalid 
redirect URI was specified: "
                             + StringEscapeUtils.escapeHtml4(uri));
                     }
+                    redirectUris.add(uri);
                 }
-                //TODO: replace this code with newClient.setLogoutUri() once 
it becomes available
-                newClient.getProperties().put("post_logout_redirect_uris", 
logoutURI);
             }
+            client.setRedirectUris(redirectUris);
+        } else {
+            client.setRedirectUris(null);
+        }
 
-            // Client Audience URIs
-            if (!StringUtils.isEmpty(audience)) {
-                String[] auds = audience.trim().split(" ");
-                List<String> registeredAuds = new ArrayList<>(auds.length);
-                for (String aud : auds) {
-                    if (!StringUtils.isEmpty(aud)) {
-                        if (!isValidURI(aud, true)) {
-                            throwInvalidRegistrationException("An invalid 
audience URI was specified: "
-                                + StringEscapeUtils.escapeHtml4(aud));
-                        }
-                        registeredAuds.add(aud);
+        // Client Logout URI
+        if (!StringUtils.isEmpty(logoutURI)) {
+            String[] logoutUris = logoutURI.split(" ");
+            for (String uri : logoutUris) {
+                if (!isValidURI(uri, false)) {
+                    throw new InvalidRegistrationException("An invalid logout 
URI was specified: "
+                        + StringEscapeUtils.escapeHtml4(uri));
+                }
+            }
+            //TODO: replace this code with newClient.setLogoutUri() once it 
becomes available
+            client.getProperties().put("post_logout_redirect_uris", logoutURI);
+        } else {
+            client.getProperties().remove("post_logout_redirect_uris");
+        }
+
+        // Client Audience URIs
+        if (!StringUtils.isEmpty(audience)) {
+            String[] auds = audience.trim().split(" ");
+            List<String> registeredAuds = new ArrayList<>(auds.length);
+            for (String aud : auds) {
+                if (!StringUtils.isEmpty(aud)) {
+                    if (!isValidURI(aud, true)) {
+                        throw new InvalidRegistrationException("An invalid 
audience URI was specified: "
+                            + StringEscapeUtils.escapeHtml4(aud));
                     }
+                    registeredAuds.add(aud);
                 }
-                newClient.setRegisteredAudiences(registeredAuds);
             }
+            client.setRegisteredAudiences(registeredAuds);
+        } else {
+            client.setRegisteredAudiences(null);

Review comment:
       Change null to an empty list here.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to