Continued:
[core.git] / inc / main / classes / auth / class_CookieAuth.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Auth;
4
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7 use CoreFramework\Registry\Registerable;
8
9 /**
10  * A cookie-bases authorization class
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
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
17  *
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.
22  *
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.
27  *
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/>.
30  */
31 class CookieAuth extends BaseFrameworkSystem implements Authorizeable, Registerable {
32         /**
33          * Protected constructor
34          *
35          * @return      void
36          */
37         protected function __construct () {
38                 // Call parent constructor
39                 parent::__construct(__CLASS__);
40         }
41
42         /**
43          * Creates an instance of this class by the given response instance
44          *
45          * @param       $responseInstance       An instance of a Responseable class
46          * @return      $loginInstance          An instance of this login class
47          */
48         public static final function createCookieAuth (Responseable $responseInstance) {
49                 // Get a new instance
50                 $loginInstance = new CookieAuth();
51
52                 // Set the response instance
53                 $loginInstance->setResponseInstance($responseInstance);
54
55                 // Return the prepared instance
56                 return $loginInstance;
57         }
58
59         /**
60          * "Setter" for username auth data
61          *
62          * @param       $userName       The username from request we shall set
63          * @return      void
64          */
65         public function setUserAuth ($userName) {
66                 $this->getResponseInstance()->addCookie('username', $userName);
67         }
68
69         /**
70          * "Setter" for password hash auth data
71          *
72          * @param       $passHash       The hashed password from request we shall set
73          * @return      void
74          */
75         public function setPasswordAuth ($passHash) {
76                 $this->getResponseInstance()->addCookie('u_hash', $passHash);
77         }
78
79         /**
80          * Getter for user auth cookie
81          *
82          * @return      $userName       Username to get from cookie
83          */
84         public function getUserAuth () {
85                 // Get the username from cookie
86                 $userName = $this->getRequestInstance()->readCookie('username');
87
88                 // Return the username
89                 return $userName;
90         }
91
92         /**
93          * Getter for password hash auth cookie
94          *
95          * @return      $passHash       Password hash to get from cookie
96          */
97         public function getPasswordAuth () {
98                 // Get the username from cookie
99                 $passHash = $this->getRequestInstance()->readCookie('u_hash');
100
101                 // Return the username
102                 return $passHash;
103         }
104
105         /**
106          * Destroy the authorization data
107          *
108          * @return      void
109          */
110         public function destroyAuthData () {
111                 // Expire both cookies
112                 $this->getResponseInstance()->expireCookie('username');
113                 $this->getResponseInstance()->expireCookie('u_hash');
114         }
115
116         /**
117          * Updates the authorization data and/or sets additional tracking data
118          *
119          * @return      void
120          */
121         public function updateAuthData () {
122                 $this->getResponseInstance()->refreshCookie('username');
123                 $this->getResponseInstance()->refreshCookie('u_hash');
124         }
125
126 }