3 namespace CoreFramework\Auth;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7 use CoreFramework\Registry\Registerable;
8 use CoreFramework\Response\Responseable;
11 * A cookie-bases authorization class
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class CookieAuth extends BaseFrameworkSystem implements Authorizeable, Registerable {
34 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Creates an instance of this class by the given response instance
46 * @param $responseInstance An instance of a Responseable class
47 * @return $loginInstance An instance of this login class
49 public static final function createCookieAuth (Responseable $responseInstance) {
51 $loginInstance = new CookieAuth();
53 // Set the response instance
54 $loginInstance->setResponseInstance($responseInstance);
56 // Return the prepared instance
57 return $loginInstance;
61 * "Setter" for username auth data
63 * @param $userName The username from request we shall set
66 public function setUserAuth ($userName) {
67 $this->getResponseInstance()->addCookie('username', $userName);
71 * "Setter" for password hash auth data
73 * @param $passHash The hashed password from request we shall set
76 public function setPasswordAuth ($passHash) {
77 $this->getResponseInstance()->addCookie('u_hash', $passHash);
81 * Getter for user auth cookie
83 * @return $userName Username to get from cookie
85 public function getUserAuth () {
86 // Get the username from cookie
87 $userName = $this->getRequestInstance()->readCookie('username');
89 // Return the username
94 * Getter for password hash auth cookie
96 * @return $passHash Password hash to get from cookie
98 public function getPasswordAuth () {
99 // Get the username from cookie
100 $passHash = $this->getRequestInstance()->readCookie('u_hash');
102 // Return the username
107 * Destroy the authorization data
111 public function destroyAuthData () {
112 // Expire both cookies
113 $this->getResponseInstance()->expireCookie('username');
114 $this->getResponseInstance()->expireCookie('u_hash');
118 * Updates the authorization data and/or sets additional tracking data
122 public function updateAuthData () {
123 $this->getResponseInstance()->refreshCookie('username');
124 $this->getResponseInstance()->refreshCookie('u_hash');