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