You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by is...@apache.org on 2023/02/08 21:14:01 UTC

[airavata-custos] branch baremetal updated: Implement listWithPost method

This is an automated email from the ASF dual-hosted git repository.

isjarana pushed a commit to branch baremetal
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git


The following commit(s) were added to refs/heads/baremetal by this push:
     new 4ab012b05 Implement listWithPost method
     new bab85021d Merge pull request #349 from isururanawaka/baremetal
4ab012b05 is described below

commit 4ab012b05dae836da2335c533bfbfa96e1289b15
Author: Isuru Ranawaka <ir...@gmail.com>
AuthorDate: Wed Feb 8 16:13:19 2023 -0500

    Implement listWithPost method
---
 .../NotAuthorizedException.java                    | 12 +--
 .../apache/custos/scim/resource/UserResource.java  |  2 +-
 .../scim/resource/manager/ResourceManager.java     | 95 ++++++++++------------
 .../org/apache/custos/scim/utils/AuthHandler.java  |  4 +-
 4 files changed, 52 insertions(+), 61 deletions(-)

diff --git a/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/utils/NotAuthorizedException.java b/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/exception/NotAuthorizedException.java
similarity index 68%
rename from custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/utils/NotAuthorizedException.java
rename to custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/exception/NotAuthorizedException.java
index 5ced61b0c..d224198ba 100644
--- a/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/utils/NotAuthorizedException.java
+++ b/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/exception/NotAuthorizedException.java
@@ -7,17 +7,17 @@
  * "License"); you may not use this file except in compliance
  * with the License. You may obtain a copy of the License at
  *
- * http://www.apache.org/licenses/LICENSE-2.0
+ *  http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied. See the License for the
  *  specific language governing permissions and limitations
  *  under the License.
  */
 
-package org.apache.custos.scim.utils;
+package org.apache.custos.scim.exception;
 
 import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.ResponseStatus;
diff --git a/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/resource/UserResource.java b/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/resource/UserResource.java
index dd9b4cd9e..28cd9288b 100644
--- a/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/resource/UserResource.java
+++ b/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/resource/UserResource.java
@@ -234,7 +234,7 @@ public class UserResource extends AbstractResource {
             newObj.put(Constants.ACCESS_TOKEN, authHandler.getToken(authorizationHeader));
         }
 
-        custosExtention.put(Constants.CUSTOS_EXTENSION, newObj);
+        custosExtention.put(Constants.DOMAIN, newObj.toString());
 
         UserResourceManager userResourceManager = new UserResourceManager();
 
diff --git a/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/resource/manager/ResourceManager.java b/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/resource/manager/ResourceManager.java
index 0a9d7613e..d72bf2660 100644
--- a/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/resource/manager/ResourceManager.java
+++ b/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/resource/manager/ResourceManager.java
@@ -19,7 +19,6 @@
 
 package org.apache.custos.scim.resource.manager;
 
-import io.grpc.Status;
 import org.apache.custos.credential.store.client.CredentialStoreServiceClient;
 import org.apache.custos.iam.admin.client.IamAdminServiceClient;
 import org.apache.custos.iam.service.*;
@@ -54,10 +53,8 @@ import org.wso2.charon3.core.schema.SCIMResourceTypeSchema;
 import org.wso2.charon3.core.utils.codeutils.SearchRequest;
 
 import java.time.Instant;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.stream.Collectors;
 
 import static org.wso2.charon3.core.protocol.endpoints.AbstractResourceManager.getDecoder;
 import static org.wso2.charon3.core.protocol.endpoints.AbstractResourceManager.getEncoder;
@@ -307,51 +304,47 @@ public class ResourceManager implements UserManager {
     @Override
     public List<Object> listUsersWithPost(SearchRequest searchRequest, Map<String, Boolean> map) throws CharonException, NotImplementedException, BadRequestException {
 
-//        Object obj = map.get(Constants.CUSTOS_EXTENSION);
-//        String clientId = ((String) ((JSONObject) obj).get(Constants.CLIENT_ID));
-//        String clientSec = ((String) ((JSONObject) obj).get(Constants.CLIENT_SEC));
-//        String decodedId = ((String) ((JSONObject) obj).get(Constants.ID));
-//        String tenantId = ((String) ((JSONObject) obj).get(Constants.TENANT_ID));
-//
-//        long tenant = Long.valueOf(tenantId);
-//
-//        GetUserManagementSATokenRequest userManagementSATokenRequest = GetUserManagementSATokenRequest
-//                .newBuilder()
-//                .setClientId(clientId)
-//                .setClientSecret(clientSec)
-//                .setTenantId(tenant)
-//                .build();
-//        AuthToken token = identityClient.getUserManagementSATokenRequest(userManagementSATokenRequest);
-//
-//        if (token != null && token.getAccessToken() != null) {
-//
-//            UserSearchMetadata metada = UserSearchMetadata.newBuilder().setUsername(decodedId).build();
-//
-//            UserSearchRequest request = UserSearchRequest
-//                    .newBuilder()
-//                    .setTenantId(tenant)
-//                    .setAccessToken(token.getAccessToken())
-//                    .setUser(metada)
-//                    .build();
-//
-//            UserRepresentation userRep = iamAdminServiceClient.getUser(request);
-//
-//            if (userRep == null || userRep.getUsername().equals("")) {
-//                throw new NotFoundException("User not found");
-//            }
-//
-//            try {
-//                return convert(userRep);
-//            } catch (InternalErrorException e) {
-//                throw new CharonException(SCIMConstants.USER);
-//            }
-//
-//        } else {
-//            String msg = "Token not found ";
-//            LOGGER.error(msg);
-//            throw new NotFoundException(msg);
-//        }
-        return null;
+        try {
+            JSONObject obj = new JSONObject(searchRequest.getDomainName());
+            String clientId = ((String) ((JSONObject) obj).get(Constants.CLIENT_ID));
+            String clientSec = ((String) ((JSONObject) obj).get(Constants.CLIENT_SEC));
+            String tenantId = ((String) ((JSONObject) obj).get(Constants.TENANT_ID));
+
+            long tenant = Long.valueOf(tenantId);
+
+            GetUserManagementSATokenRequest userManagementSATokenRequest = GetUserManagementSATokenRequest
+                    .newBuilder()
+                    .setClientId(clientId)
+                    .setClientSecret(clientSec)
+                    .setTenantId(tenant)
+                    .build();
+            AuthToken token = identityClient.getUserManagementSATokenRequest(userManagementSATokenRequest);
+
+            if (token != null && token.getAccessToken() != null) {
+
+                FindUsersRequest findUsersRequest = FindUsersRequest.newBuilder().setAccessToken(token.getAccessToken())
+                        .setOffset(searchRequest.getStartIndex() - 1).setOffset(searchRequest.getCount()).build();
+
+                FindUsersResponse userRep = iamAdminServiceClient.getUsers(findUsersRequest);
+
+                List<Object> userList = userRep.getUsersList().stream().map(user-> {
+                    try {
+                        return convert(user);
+                    } catch (Exception e) {
+                        throw new RuntimeException(e);
+                    }
+                }).collect(Collectors.toList());
+
+                for (UserRepresentation representation : userRep.getUsersList()) {
+                    userList.add(convert(representation));
+                }
+                return userList;
+            } else {
+                throw new CustosSCIMException("Invalid Credentials", new UnauthorizedException());
+            }
+        } catch (Exception ex) {
+            throw new CustosSCIMException(" Error occurred while fetching users ", ex);
+        }
     }
 
     @Override
@@ -777,7 +770,7 @@ public class ResourceManager implements UserManager {
         Instant instant = Instant.ofEpochMilli(Double.doubleToLongBits(representation.getCreationTime()));
         JSONObject meta = new JSONObject();
         meta.put("created", instant.toString());
-        String location =  AbstractResourceManager.getResourceEndpointURL(SCIMConstants.USER_ENDPOINT)+ representation.getUsername();
+        String location = AbstractResourceManager.getResourceEndpointURL(SCIMConstants.USER_ENDPOINT) + representation.getUsername();
         meta.put("location", location);
         meta.put("resourceType", SCIMConstants.USER);
 
diff --git a/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/utils/AuthHandler.java b/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/utils/AuthHandler.java
index 3baf19cb3..600e82324 100644
--- a/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/utils/AuthHandler.java
+++ b/custos-services/custos-integration-services/scim-service/src/main/java/org/apache/custos/scim/utils/AuthHandler.java
@@ -24,13 +24,11 @@ import org.apache.custos.credential.store.client.CredentialStoreServiceClient;
 import org.apache.custos.identity.client.IdentityClient;
 import org.apache.custos.integration.services.commons.interceptors.AuthInterceptor;
 import org.apache.custos.integration.services.commons.model.AuthClaim;
+import org.apache.custos.scim.exception.NotAuthorizedException;
 import org.apache.custos.tenant.profile.client.async.TenantProfileClient;
-import org.json.JSONObject;
 import org.springframework.stereotype.Component;
 import org.springframework.web.client.HttpStatusCodeException;
 
-import javax.swing.text.html.Option;
-import java.util.Map;
 import java.util.Optional;
 
 @Component