3 namespace CoreFramework\Auth;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7 use CoreFramework\Registry\Registerable;
10 * A cookie-bases authorization class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class CookieAuth extends BaseFrameworkSystem implements Authorizeable, Registerable {
33 * Protected constructor
37 protected function __construct () {
38 // Call parent constructor
39 parent::__construct(__CLASS__);
43 * Creates an instance of this class by the given response instance
45 * @param $responseInstance An instance of a Responseable class
46 * @return $loginInstance An instance of this login class
48 public static final function createCookieAuth (Responseable $responseInstance) {
50 $loginInstance = new CookieAuth();
52 // Set the response instance
53 $loginInstance->setResponseInstance($responseInstance);
55 // Return the prepared instance
56 return $loginInstance;
60 * "Setter" for username auth data
62 * @param $userName The username from request we shall set
65 public function setUserAuth ($userName) {
66 $this->getResponseInstance()->addCookie('username', $userName);
70 * "Setter" for password hash auth data
72 * @param $passHash The hashed password from request we shall set
75 public function setPasswordAuth ($passHash) {
76 $this->getResponseInstance()->addCookie('u_hash', $passHash);
80 * Getter for user auth cookie
82 * @return $userName Username to get from cookie
84 public function getUserAuth () {
85 // Get the username from cookie
86 $userName = $this->getRequestInstance()->readCookie('username');
88 // Return the username
93 * Getter for password hash auth cookie
95 * @return $passHash Password hash to get from cookie
97 public function getPasswordAuth () {
98 // Get the username from cookie
99 $passHash = $this->getRequestInstance()->readCookie('u_hash');
101 // Return the username
106 * Destroy the authorization data
110 public function destroyAuthData () {
111 // Expire both cookies
112 $this->getResponseInstance()->expireCookie('username');
113 $this->getResponseInstance()->expireCookie('u_hash');
117 * Updates the authorization data and/or sets additional tracking data
119 * @param $requestInstance An instance of a Requestable class
122 public function updateAuthData () {
123 $this->getResponseInstance()->refreshCookie('username');
124 $this->getResponseInstance()->refreshCookie('u_hash');