mirror of https://github.com/jacekkow/uphpCAS

Jacek Kowalski
2015-09-06 a660177f51333207165dbdd0795889827adea10b
commit | author | age
36cae3 1 # uphpCAS
JK 2
3 Simple PHP library for CAS authentication
4
5 ## Introduction
6
7 This library intends to be a replacement for overly complex
8 [phpCAS](https://wiki.jasig.org/display/casc/phpcas) library.
9
10 It only supports basic [CAS protocol](http://jasig.github.io/cas/4.0.x/protocol/CAS-Protocol.html),
11 without proxying capabilities, which is enough for website authentication.
12
13 ## Usage
14
15 ### Composer
16
17 1. Add jacekkow/uphpcas dependency:
18     
19     ```bash
20     composer require jacekkow/uphpcas
21     ```
22
23 1. Include autoloader in your application:
24     
25     ```php
26     <?php
27     require_once(__DIR__ . '/vendor/autoload.php');
28     ```
29
30 1. See the usage examples below
31
32 ### Raw usage
33
34 1. Download [uphpCAS.php](https://raw.githubusercontent.com/jacekkow/uphpCAS/master/uphpCAS.php)
35 1. Include it in your application:
36     
37     ```php
38     <?php
39     require_once(__DIR__ . '/uphpCAS.php');
40     ```
41
42 1. See the usage examples below
43
44 ## Examples
45
46 ### Require authentication
47
48 To require authentication to access the page:
49
50 ```php
51 <?php
52 require_once('uphpCAS.php');
53
54 try {
55     $cas = new uphpCAS('https://cas.server.local/cas');
56     $user = $cas->authenticate();
57     
58     echo 'Authenticated as '.$user->user;
59 } catch(Exception $e) {
60     echo 'Jasig authentication failed: '.$e->getMessage();
61     die();
62 }
63 ```
64
65 ### Login and logout pages
66
67 index.php:
68
69 ```php
70 <?php
71 require_once('uphpCAS.php');
72
73 $cas = new uphpCAS();
74 if($cas->isAuthenticated()) {
75     $user = $cas->authenticate();
76     echo 'Authenticated as '.$user->user;
77 } else {
78     echo 'Not authenticated. <a href="login.php">Log in</a>';
79 }
80 ```
81
82 login.php:
83
84 ```php
85 <?php
86 require_once('uphpCAS.php');
87
88 try {
89     $cas = new uphpCAS('https://cas.server.local/cas');
90     $user = $cas->authenticate();
91     
92     header('Location: index.php');
93 } catch(Exception $e) {
94     echo 'Jasig authentication failed: '.$e->getMessage();
95     die();
96 }
97 ```
98
99 logout.php:
100
101 ```php
102 <?php
103 require_once('uphpCAS.php');
104
105 try {
106     $cas = new uphpCAS('https://cas.server.local/cas');
107     $user = $cas->logout();
108 } catch(Exception $e) {
109     echo 'Jasig authentication failed: '.$e->getMessage();
110     die();
111 }
112 ```
113
114 ## Common issues
115
116 ### Invalid redirection from CAS server
117
118 By default uphpCAS tries to guess correct URL to pass to CAS server
119 as a "service" parameter using values from $_SERVER superglobal
120 (see getCurrentUrl() method). This URL is used by CAS server
121 to redirect user back to the application after successful CAS login.
122
123 If this guess is incorrect, eg. when the server is behind proxy,
124 you can override it using setServiceUrl() method:
125
126 ```php
127 $cas = new uphpCAS('https://cas.server.local/cas');
128 $cas->setServiceUrl('https://service.local/subpage');
129 ```
130
131 or second argument of the constructor:
132
133 ```php
134 $cas = new uphpCAS('https://cas.server.local/cas'),
135     'https://service.local/subpage');
136 ```
137
138 ### HTTP POST issues
139
140 The standard method of passing "ticket" from CAS server to application
141 is by HTTP GET method. To avoid having additional "ticket" parameter
142 in the URL on single-page apps, which can expire and cause uphpCAS
143 to throw exception, this library uses POST method by default.
144
145 You can change the method back to HTTP GET with setMethod():
146
147 ```php
148 $cas = new uphpCAS('https://cas.server.local/cas');
149 $cas->setMethod('GET');
150 ```
151
152 ### CAS over HTTPS
153
154 This library enforces CAS certificate validation. The hostname
155 of the CAS server must match the one in provided SSL certificate.
156 Also the certificate must be signed by CA included in CA store
157 (or self-signed - then the certificate itself must be included).
158 By default it looks for CA store at:
159
160 - /etc/ssl/certs/ca-certificates.crt
161 - /etc/ssl/certs/ca-bundle.crt
162 - /etc/pki/tls/certs/ca-bundle.crt
163
164 You can change path to CA store file using setCaFile() method:
165
166 ```php
167 $cas = new uphpCAS('https://cas.server.local/cas');
168 $cas->setCaFile('./localStore.pem');
169 ```