5. External login

Starting with Cyclos 4.2, using web services together with the right configuration, it is possible to add a Cyclos login form to an external website. The user types in his/hers Cyclos username and password in that form and, after a successful login, is redirected to Cyclos, where the session will be already valid, and the user can perform the operations as usual. After the user clicks logout, or his/hers session expires, the user is redirected back to the external website.

5.1. The following aspects should be considered:

  • It is needed to have an administrator whose group is granted the permission "Login users via web services". This is needed because the website will relay logins from users their clients to Cyclos.

  • The website needs to have that administrator's username and password configured in order to make the web services call. It is planned for Cyclos 4.3 the creation of access clients, which will allow using a separated key instead of the username / password.

  • It is a good practice to create a separated configuration for that administrator. That configuration should have an IP address whitelist for the web services channel. Doing that, no other server, even if the adminitrator username / password is known by someone else, will be able to perform such operations.

  • The Cyclos configuration for users needs the following settings:

    • Redirect login to URL: This is the URL of the external website which contains the login form. This is used to redirect the user when his session expires and a new login is needed, or when the user navigates directly to some URL in Cyclos (as guest) and then clicks "Login";

    • URL to redirect after logout: This is the URL where the user will be redirected after clicking "Logout" in Cyclos. It might be the same URL as the one for redirect login, but not necessarily.

  • Finally, the web service code needs to be created, and deployed to the website. Here is an example, which receives the username and password parameters, calls the web service to create a session for the user (passing his remote address), redirecting the user to Cyclos.

    <?php
    
    // Configure Cyclos and obtain an instance of LoginService 
    require_once 'configureCyclos.php';
    $loginService = new Cyclos\LoginService();
    
    // Set the parameters
    $params = new stdclass();
    $params->user = array("principal" => $_POST['username']);
    $params->password = $_POST['password'];
    $params->remoteAddress = $_SERVER['REMOTE_ADDR'];
    
    // Perform the login
    try {
    	$result = $loginService->loginUser($params);
    } catch (Cyclos\ConnectionException $e) {
    	echo("Cyclos server couldn't be contacted");
    	die();
    } catch (Cyclos\ServiceException $e) {
    	switch ($e->errorCode) {
    		case 'VALIDATION':
    			echo("Missing username / password");
    			break;
    		case 'LOGIN':
    			echo("Invalid username / password");
    			break;
    		case 'REMOTE_ADDRESS_BLOCKED':
    			echo("Your access is blocked by exceeding invalid login attempts");
    			break;
    		default:
    			echo("Error while performing login: {$e->errorCode}");
    			break;
    	}
    	die();
    }
    
    // Redirect the user to Cyclos with the returned session token
    header("Location: "
    	. Cyclos\Configuration::getRootUrl()
    	. "?sessionToken="
    	. $result->sessionToken);