{"id":337,"date":"2013-06-19T14:05:23","date_gmt":"2013-06-19T14:05:23","guid":{"rendered":"http:\/\/blog.law.gmu.edu\/webservices\/?p=337"},"modified":"2013-06-19T14:05:23","modified_gmt":"2013-06-19T14:05:23","slug":"simple-cas-authentication","status":"publish","type":"post","link":"https:\/\/sls.gmu.edu\/web\/2013\/06\/19\/simple-cas-authentication\/","title":{"rendered":"Simple CAS Authentication"},"content":{"rendered":"<p>The following script makes a private web site accessible only to authenticated users. For example, if you have a career services web site and you only want students to access the materials using\u00a0their email (university)\u00a0credentials, this script would do the trick. You don&#8217;t need to create\u00a0separate\u00a0accounts\u00a0\u00a0for the site.<\/p>\n<p>1. Download phpCAS and unzip\/untar it:\u00a0<a href=\"https:\/\/wiki.jasig.org\/display\/CASC\/phpCAS\">https:\/\/wiki.jasig.org\/display\/CASC\/phpCAS<\/a>.<\/p>\n<p>2. Place the entire phpCAS directory on your server.<\/p>\n<p>3. Place the following codes in at the top of any .php file that you want the contents to be private. If you want to make the entire site private, just add the script to an include file in the header. The following script is based on a <a href=\"https:\/\/github.com\/Jasig\/phpCAS\/blob\/master\/docs\/examples\/example_simple.php\">simple CAS client<\/a>:<\/p>\n<p><code>&lt;?php<\/p>\n<p>\/**<br \/>\n* Example for a simple cas 2.0 client<br \/>\n*<br \/>\n* PHP Version 5<br \/>\n*<br \/>\n* @file example_simple.php<br \/>\n* @category Authentication<br \/>\n* @package PhpCAS<br \/>\n* @author Joachim Fritschi &lt;jfritschi@freenet.de&gt;<br \/>\n* @author Adam Franco &lt;afranco@middlebury.edu&gt;<br \/>\n* @license http:\/\/www.apache.org\/licenses\/LICENSE-2.0 Apache License 2.0<br \/>\n* @link https:\/\/wiki.jasig.org\/display\/CASC\/phpCAS<br \/>\n*\/<\/p>\n<p>\/\/ Load the settings from the central config file<br \/>\n\/\/ require_once 'config.php'; \/*commented out by Donny*\/<br \/>\n\/\/ Load the CAS lib<br \/>\nrequire_once $phpcas_path . '\/CAS.php'; \/*This is the path that to the CAS directory on your server, not the URL*\/<\/p>\n<p>\/\/ Uncomment to enable debugging<br \/>\nphpCAS::setDebug();<\/p>\n<p>\/\/ Initialize phpCAS<br \/>\nphpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context);<br \/>\n\/\/example: phpCAS::client(CAS_VERSION_1_0, 'login.gmu.edu', 443,'');<\/p>\n<p>\/\/ For production use set the CA certificate that is the issuer of the cert<br \/>\n\/\/ on the CAS server and uncomment the line below<br \/>\n\/\/ phpCAS::setCasServerCACert($cas_server_ca_cert_path);<\/p>\n<p>\/\/ For quick testing you can disable SSL validation of the CAS server.<br \/>\n\/\/ THIS SETTING IS NOT RECOMMENDED FOR PRODUCTION.<br \/>\n\/\/ VALIDATING THE CAS SERVER IS CRUCIAL TO THE SECURITY OF THE CAS PROTOCOL!<br \/>\nphpCAS::setNoCasServerValidation();<\/p>\n<p>\/\/ force CAS authentication<br \/>\nphpCAS::forceAuthentication();<\/p>\n<p>\/\/ at this step, the user has been authenticated by the CAS server<br \/>\n\/\/ and the user's login name can be read with phpCAS::getUser().<\/p>\n<p>\/\/ logout if desired<br \/>\nif (isset($_REQUEST['logout'])) {<br \/>\nphpCAS::logout();<br \/>\n}<\/p>\n<p>\/\/ for this test, simply print that the authentication was successfull<br \/>\n?&gt;<br \/>\n<\/code><\/p>\n<p>4. Add the following script to your HTML file to indicate if the user is logged in and also to the allow the user to sign off.<\/p>\n<p><code>&lt;div&gt;Not &lt;strong&gt;&lt;?php echo phpCAS::getUser(); ?&gt;&lt;\/strong&gt;? &lt;a href=\"?logout=\"&gt;Sign out&lt;\/a&gt;&lt;\/div&gt;<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following script makes a private web site accessible only to authenticated users. For example, if you have a career services web site and you only want students to access the materials using\u00a0their email (university)\u00a0credentials, this script would do the trick. You don&#8217;t need to create\u00a0separate\u00a0accounts\u00a0\u00a0for the site. 1. Download phpCAS and unzip\/untar it:\u00a0https:\/\/wiki.jasig.org\/display\/CASC\/phpCAS. 2. &hellip; <a href=\"https:\/\/sls.gmu.edu\/web\/2013\/06\/19\/simple-cas-authentication\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Simple CAS Authentication&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[25],"class_list":["post-337","post","type-post","status-publish","format-standard","hentry","category-web-development","tag-phpcas"],"_links":{"self":[{"href":"https:\/\/sls.gmu.edu\/web\/wp-json\/wp\/v2\/posts\/337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sls.gmu.edu\/web\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sls.gmu.edu\/web\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sls.gmu.edu\/web\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sls.gmu.edu\/web\/wp-json\/wp\/v2\/comments?post=337"}],"version-history":[{"count":0,"href":"https:\/\/sls.gmu.edu\/web\/wp-json\/wp\/v2\/posts\/337\/revisions"}],"wp:attachment":[{"href":"https:\/\/sls.gmu.edu\/web\/wp-json\/wp\/v2\/media?parent=337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sls.gmu.edu\/web\/wp-json\/wp\/v2\/categories?post=337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sls.gmu.edu\/web\/wp-json\/wp\/v2\/tags?post=337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}