2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
37 require_once('modules/Users/authentication/AuthenticationController.php');
38 require_once('tests/modules/Users/AuthenticateTest.php');
42 * This tests that we can correctly load a custom settings.php file for SAMLAuthentication when called from
43 * modules/Users/authentication/SAMLAuthenticate/index.php
45 * This tests mimics the contents of modules/Users/authentication/SAMLAuthenticate/index.php by placing it
46 * in a custom directory minus the header() function call. We can't include that because it'd just cause other issues
48 class Bug50936Test extends Sugar_PHPUnit_Framework_OutputTestCase
52 public function setUp()
54 $GLOBALS['app'] = new SugarApplication();
55 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
56 $this->sugar_config_old = $GLOBALS['sugar_config'];
57 $_REQUEST['user_name'] = 'foo';
58 $_REQUEST['user_password'] = 'bar';
59 $_SESSION['authenticated_user_id'] = true;
60 $_SESSION['hasExpiredPassword'] = false;
61 $_SESSION['isMobile'] = null;
62 $GLOBALS['sugar_config']['authenticationClass'] = 'SAMLAuthenticate';
63 $GLOBALS['sugar_config']['SAML_X509Cert'] = 'Bug50936_X509Cert';
65 //Create the custom directory if it does not exist
66 if(!is_dir('custom/modules/Users/authentication/SAMLAuthenticate')) {
67 mkdir_recursive('custom/modules/Users/authentication/SAMLAuthenticate');
72 require('modules/Users/authentication/SAMLAuthenticate/lib/onelogin/saml.php');
73 require(get_custom_file_if_exists('modules/Users/authentication/SAMLAuthenticate/settings.php'));
75 \$authrequest = new SamlAuthRequest(\$settings);
76 \$url = \$authrequest->create();
80 file_put_contents('custom/modules/Users/authentication/SAMLAuthenticate/index.php', $contents);
84 // this function should be modified to return the SAML settings for the current use
85 \$settings = new SamlSettings();
86 // when using Service Provider Initiated SSO (starting at index.php), this URL asks the IdP to authenticate the user.
87 \$settings->idp_sso_target_url = 'www.sugarcrm.com';
89 // the certificate for the users account in the IdP
90 \$settings->x509certificate = \$GLOBALS['sugar_config']['SAML_X509Cert'];
92 // The URL where to the SAML Response/SAML Assertion will be posted
93 \$settings->assertion_consumer_service_url = \$GLOBALS['sugar_config']['site_url']. "/index.php?module=Users&action=Authenticate";
95 // Name of this application
96 \$settings->issuer = "php-saml";
98 // Tells the IdP to return the email address of the current user
99 \$settings->name_identifier_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress";
104 if(file_exists('custom/modules/Users/authentication/SAMLAuthenticate/settings.php')) {
105 $this->customContents = file_get_contents('custom/modules/Users/authentication/SAMLAuthenticate/settings.php');
108 file_put_contents('custom/modules/Users/authentication/SAMLAuthenticate/settings.php', $contents);
111 public function tearDown()
113 //If we had a custom settings.php file already, just restore it
114 if(!empty($this->customContents))
116 file_put_contents('custom/modules/Users/authentication/SAMLAuthenticate/settings.php', $this->customContents);
118 unlink('custom/modules/Users/authentication/SAMLAuthenticate/settings.php');
121 //Remove the test index.php file
122 if(file_exists('custom/modules/Users/authentication/SAMLAuthenticate/index.php'))
124 unlink('custom/modules/Users/authentication/SAMLAuthenticate/index.php');
127 unset($GLOBALS['current_user']);
128 $GLOBALS['sugar_config'] = $this->sugar_config_old;
129 unset($_REQUEST['login_module']);
130 unset($_REQUEST['login_action']);
131 unset($_REQUEST['login_record']);
132 unset($_REQUEST['user_name']);
133 unset($_REQUEST['user_password']);
134 unset($_SESSION['authenticated_user_id']);
135 unset($_SESSION['hasExpiredPassword']);
136 unset($_SESSION['isMobile']);
139 public function testLoadCustomSettingsFromIndex()
141 require('custom/modules/Users/authentication/SAMLAuthenticate/index.php');
142 $this->expectOutputRegex('/www\.sugarcrm\.com/', 'Failed to override custom/modules/Users/authentication/SAMLAuthenticate/settings.php');