Search in sources :

Example 1 with Tenant

use of org.wso2.carbon.user.api.Tenant in project carbon-apimgt by wso2.

the class RestCallUtilImpl method rsaSignedFetchUserRequest.

/**
 * {@inheritDoc}
 */
@Override
public HttpResponse rsaSignedFetchUserRequest(URI uri, String username, String userTenantDomain, String rsaSignedToken, MediaType acceptContentType) throws APIManagementException {
    if (uri == null) {
        throw new IllegalArgumentException("The URI must not be null");
    }
    if (username == null) {
        throw new IllegalArgumentException("UserName must not be null");
    }
    if (userTenantDomain == null) {
        throw new IllegalArgumentException("User tenant domain must not be null");
    }
    if (rsaSignedToken == null) {
        throw new IllegalArgumentException("RSA signed token must not be null");
    }
    HttpURLConnection httpConnection = null;
    try {
        JSONObject loginInfoJsonObj = new JSONObject();
        loginInfoJsonObj.put(APIMgtConstants.FunctionsConstants.USERNAME, username);
        loginInfoJsonObj.put(APIMgtConstants.FunctionsConstants.USER_TENANT_DOMAIN, userTenantDomain);
        httpConnection = (HttpURLConnection) uri.toURL().openConnection();
        httpConnection.setRequestMethod(APIMgtConstants.FunctionsConstants.POST);
        httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON);
        httpConnection.setDoOutput(true);
        httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.RSA_SIGNED_TOKEN, rsaSignedToken);
        if (acceptContentType != null) {
            httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.ACCEPT, acceptContentType.toString());
        }
        OutputStream outputStream = httpConnection.getOutputStream();
        outputStream.write(loginInfoJsonObj.toString().getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();
        return getResponse(httpConnection);
    } catch (IOException e) {
        throw new APIManagementException("Connection not established properly ", e);
    } finally {
        if (httpConnection != null) {
            httpConnection.disconnect();
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 2 with Tenant

use of org.wso2.carbon.user.api.Tenant in project core-util by WSO2Telco.

the class BasicAuthenticator method isAuthenticatedUser.

public boolean isAuthenticatedUser(String userName, String password) {
    PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    RealmService realmService = (RealmService) carbonContext.getOSGiService(RealmService.class, null);
    RegistryService registryService = (RegistryService) carbonContext.getOSGiService(RegistryService.class, null);
    String tenantDomain = MultitenantUtils.getTenantDomain(userName);
    try {
        UserRealm userRealm = null;
        userRealm = AnonymousSessionUtil.getRealmByTenantDomain(registryService, realmService, tenantDomain);
        if (userRealm == null) {
            log.error("invalid domain or unactivated tenant login");
            return false;
        }
        String tenantAwareUsername = MultitenantUtils.getTenantAwareUsername(userName);
        if (userRealm.getUserStoreManager().authenticate(tenantAwareUsername, password)) {
            return true;
        } else {
            log.error("authentication failed. please check your username/password");
            return false;
        }
    } catch (CarbonException | UserStoreException e) {
        log.error("authentication failed for user : " + userName, e);
        return false;
    }
}
Also used : UserRealm(org.wso2.carbon.user.core.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) CarbonException(org.wso2.carbon.CarbonException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) RegistryService(org.wso2.carbon.registry.core.service.RegistryService)

Example 3 with Tenant

use of org.wso2.carbon.user.api.Tenant in project airavata by apache.

the class MigrationManager method getUserProfilesFromWso2IS.

/* Method used to fetch all the user profiles from the registered tenants */
public List<UserProfileDAO> getUserProfilesFromWso2IS() {
    ArrayList<UserProfileDAO> userProfileList = new ArrayList<UserProfileDAO>();
    for (Wso2ISLoginCredentialsDAO creds : adminCredentials) {
        RemoteUserStoreManagerServiceStub isClient = Wso2IdentityServerClient.getAdminServiceClient(creds.getLoginUserName(), creds.getLoginPassword(), "RemoteUserStoreManagerService");
        String[] userList;
        System.out.println("Fetching User Profiles for " + creds.getGateway() + " tenant ...");
        try {
            userList = isClient.getUserList("http://wso2.org/claims/givenname", "*", "default");
            System.out.println("FirstName\tLastName\tEmail\t\t\tuserName\tCountry\tOrganization\tphone\tRoles");
            String[] claims = { "http://wso2.org/claims/givenname", "http://wso2.org/claims/lastname", "http://wso2.org/claims/emailaddress", "http://wso2.org/claims/country", "http://wso2.org/claims/organization", "http://wso2.org/claims/mobile", "http://wso2.org/claims/telephone", "http://wso2.org/claims/streetaddress", "http://wso2.org/claims/role", "http://wso2.org/claims/identity/accountLocked" };
            for (String user : userList) {
                UserProfileDAO userProfile = new UserProfileDAO();
                ClaimValue[] retrievedClaimValues = isClient.getUserClaimValuesForClaims(user, claims, null);
                List<String> phones = new ArrayList<String>();
                for (ClaimValue claim : retrievedClaimValues) {
                    if (claim.getClaimURI().equals(claims[0])) {
                        userProfile.setFirstName(claim.getValue());
                    } else if (claim.getClaimURI().equals(claims[1])) {
                        userProfile.setLastName(claim.getValue());
                    } else if (claim.getClaimURI().equals(claims[2])) {
                        userProfile.setEmail(claim.getValue());
                    } else if (claim.getClaimURI().equals(claims[3])) {
                        userProfile.setCountry(claim.getValue());
                    } else if (claim.getClaimURI().equals(claims[4])) {
                        userProfile.setOrganization(claim.getValue());
                    } else if (claim.getClaimURI().equals(claims[5]) || claim.getClaimURI().equals(claims[6])) {
                        phones.add(claim.getValue());
                    } else if (claim.getClaimURI().equals(claims[7])) {
                        userProfile.setAddress(claim.getValue());
                    } else if (claim.getClaimURI().equals(claims[8])) {
                        userProfile.setRoles(convertCommaSeparatedRolesToList(claim.getValue()));
                    } else if (claim.getClaimURI().equals(claims[9])) {
                        userProfile.setAccountLocked(claim.getValue().equals("true"));
                    }
                }
                // Lowercase all usernames as required by Keycloak and User Profile service
                userProfile.setUserName(user.toLowerCase());
                userProfile.setGatewayID(creds.getGateway());
                userProfile.setPhones(phones);
                if (!userProfile.isAccountLocked()) {
                    System.out.println(userProfile.getFirstName() + "\t" + userProfile.getLastName() + "\t" + userProfile.getUserName() + "\t" + userProfile.getEmail() + "\t" + userProfile.getCountry() + "\t" + userProfile.getOrganization() + "\t" + userProfile.getAddress() + "\t" + userProfile.getRoles());
                    userProfileList.add(userProfile);
                } else {
                    System.out.println("Skipping locked account for user " + user + "!");
                }
            }
        } catch (RemoteException e) {
            System.out.println(e.getMessage());
            System.out.println(e.getCause());
            e.printStackTrace();
        } catch (RemoteUserStoreManagerServiceUserStoreExceptionException e) {
            System.out.println(e.getMessage());
            System.out.println(e.getCause());
            e.printStackTrace();
        }
    }
    System.out.println("User profiles from all the tenant are retrieved ...");
    return userProfileList;
}
Also used : ClaimValue(org.wso2.carbon.um.ws.api.stub.ClaimValue) RemoteUserStoreManagerServiceStub(org.wso2.carbon.um.ws.api.stub.RemoteUserStoreManagerServiceStub) RemoteException(java.rmi.RemoteException) RemoteUserStoreManagerServiceUserStoreExceptionException(org.wso2.carbon.um.ws.api.stub.RemoteUserStoreManagerServiceUserStoreExceptionException)

Example 4 with Tenant

use of org.wso2.carbon.user.api.Tenant in project carbon-business-process by wso2.

the class BPMNAppDeployer method undeployArtifacts.

/**
 * Check the artifact type and if it is a BPMN, delete the file from the BPMN
 * deployment hot folder
 *
 * @param carbonApp  - CarbonApplication instance to check for BPMN artifacts
 * @param axisConfig - - axisConfig of the current tenant
 */
public void undeployArtifacts(CarbonApplication carbonApp, AxisConfiguration axisConfig) {
    List<Artifact.Dependency> artifacts = carbonApp.getAppConfig().getApplicationArtifact().getDependencies();
    // loop through all dependencies
    for (Artifact.Dependency dep : artifacts) {
        Deployer deployer;
        Artifact artifact = dep.getArtifact();
        if (artifact == null) {
            continue;
        }
        if (BPMN_TYPE.equals(artifact.getType())) {
            deployer = AppDeployerUtils.getArtifactDeployer(axisConfig, BPMN_DIR, "bar");
        } else {
            continue;
        }
        List<CappFile> files = artifact.getFiles();
        if (files.size() != 1) {
            log.error("A BPMN artifact must have a single file. But " + files.size() + " files found.");
            continue;
        }
        if (deployer != null && AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED.equals(artifact.getDeploymentStatus())) {
            String fileName = artifact.getFiles().get(0).getName();
            String artifactPath = artifact.getExtractedPath() + File.separator + fileName;
            try {
                deployer.undeploy(artifactPath);
                artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_PENDING);
            } catch (DeploymentException e) {
                artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED);
                log.error("Error occured while trying to un deploy : " + artifact.getName());
            }
        }
    }
}
Also used : DeploymentException(org.apache.axis2.deployment.DeploymentException) Artifact(org.wso2.carbon.application.deployer.config.Artifact) Deployer(org.apache.axis2.deployment.Deployer) CappFile(org.wso2.carbon.application.deployer.config.CappFile)

Example 5 with Tenant

use of org.wso2.carbon.user.api.Tenant in project carbon-business-process by wso2.

the class HumanTaskAppDeployer method deployArtifacts.

/**
 * Check the artifact type and if it is a HumanTask artifact, copy it to the HumanTask deployment hot folder
 *
 * @param carbonApp  - CarbonApplication instance to check for HumanTask artifacts
 * @param axisConfig - AxisConfiguration of the current tenant
 */
public void deployArtifacts(CarbonApplication carbonApp, AxisConfiguration axisConfig) throws DeploymentException {
    List<Artifact.Dependency> artifacts = carbonApp.getAppConfig().getApplicationArtifact().getDependencies();
    // loop through all dependencies
    for (Artifact.Dependency dep : artifacts) {
        Deployer deployer;
        Artifact artifact = dep.getArtifact();
        if (artifact == null) {
            continue;
        }
        if (!isAccepted(artifact.getType())) {
            log.warn("Can't deploy artifact : " + artifact.getName() + " of type : " + artifact.getType() + ". Required features are not installed in the system");
            continue;
        }
        if (HUMANTASK_TYPE.equals(artifact.getType())) {
            deployer = AppDeployerUtils.getArtifactDeployer(axisConfig, HUMANTASK_DIR, "zip");
        } else {
            continue;
        }
        List<CappFile> files = artifact.getFiles();
        if (files.size() != 1) {
            log.error("HumanTask artifacts must have a single file to " + "be deployed. But " + files.size() + " files found.");
            continue;
        }
        if (deployer != null) {
            String fileName = artifact.getFiles().get(0).getName();
            String artifactPath = artifact.getExtractedPath() + File.separator + fileName;
            try {
                deployer.deploy(new DeploymentFileData(new File(artifactPath), deployer));
                artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED);
                File artifactFile = new File(artifactPath);
                if (artifactFile.exists() && !artifactFile.delete()) {
                    log.warn("Couldn't delete App artifact file : " + artifactPath);
                }
            } catch (DeploymentException e) {
                artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED);
                throw e;
            }
        }
    }
}
Also used : DeploymentFileData(org.apache.axis2.deployment.repository.util.DeploymentFileData) DeploymentException(org.apache.axis2.deployment.DeploymentException) File(java.io.File) CappFile(org.wso2.carbon.application.deployer.config.CappFile) Artifact(org.wso2.carbon.application.deployer.config.Artifact) Deployer(org.apache.axis2.deployment.Deployer) CappFile(org.wso2.carbon.application.deployer.config.CappFile)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)180 UserStoreException (org.wso2.carbon.user.api.UserStoreException)88 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)83 ArrayList (java.util.ArrayList)79 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)70 PreparedStatement (java.sql.PreparedStatement)51 SQLException (java.sql.SQLException)50 IOException (java.io.IOException)49 Connection (java.sql.Connection)49 HashMap (java.util.HashMap)44 ResultSet (java.sql.ResultSet)43 JSONObject (org.json.simple.JSONObject)41 Resource (org.wso2.carbon.registry.core.Resource)40 Registry (org.wso2.carbon.registry.core.Registry)38 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)34 API (org.wso2.carbon.apimgt.api.model.API)34 Test (org.junit.Test)33 File (java.io.File)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)32 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)30