@@ -30,10 +30,21 @@ class BasicAuth {
3030
3131 private $ authUserFile ;
3232 private $ passwordEncryption ;
33+ private $ authUserDomain ;
3334 private $ authenticated = false ;
3435
3536 const DEFAULT_AUTH_DOMAIN = 'default ' ;
3637
38+ public static function getCurrentUser ()
39+ {
40+ if (isset ($ _SERVER ["PHP_AUTH_USER " ])) {
41+ return $ _SERVER ["PHP_AUTH_USER " ];
42+ } else if (BasicAuth \HTML ::isAvailable ()) {
43+ return BasicAuth \HTML \Session::getUser ();
44+ } else {
45+ return "" ;
46+ }
47+ }
3748 /**
3849 * construct your auth object
3950 *
@@ -45,10 +56,12 @@ public function __construct($authUserDomain = null, $passwordEncryption = 'crypt
4556 {
4657 $ authUserFile = null ;
4758 if (is_null ($ authUserDomain )) {
59+ $ this ->authUserDomain = self ::DEFAULT_AUTH_DOMAIN ;
4860 if (file_exists (self ::getDefaultAuthFilename ())) {
4961 $ authUserFile = self ::getDefaultAuthFilename ();
5062 }
5163 } else {
64+ $ this ->authUserDomain = $ authUserDomain ;
5265 $ authUserFile = self ::getAuthFilename ($ authUserDomain );
5366 }
5467 if (!file_exists ($ authUserFile )) {
@@ -136,47 +149,11 @@ public function checkAuthentication()
136149 } else {
137150 $ auth = false ;
138151 if (isset ($ _SERVER ["PHP_AUTH_USER " ]) && $ _SERVER ["PHP_AUTH_PW " ]) {
139- $ fp = fopen ($ this ->authUserFile , 'r ' );
140- while ($ line = fgets ($ fp )) {
141- $ line = trim ($ line );
142- $ parts = explode (': ' , $ line );
143- if (count ($ parts )==2 ) {
144- list ($ username , $ password ) = $ parts ;
145- if ($ username == $ _SERVER ['PHP_AUTH_USER ' ]) {
146- switch (true ) {
147- case strpos ($ password , '{SHA} ' ) === 0 :
148- //trigger_error("sha " . $password . " " . $_SERVER["PHP_AUTH_PW"] . " " . base64_encode(sha1($_SERVER["PHP_AUTH_PW"], true)), E_USER_WARNING);
149- // inline sha
150- $ this ->passwordEncryption = "sha1 " ;
151- break ;
152- }
153- switch ($ this ->passwordEncryption ) {
154- case 'crypt ' :
155- // Get the salt from $password. It is always the first
156- // two characters of a DES-encrypted string.
157- $ salt = substr ($ password , 0 , 2 );
158- // Encrypt $PHP_AUTH_PW based on $salt
159- $ hashedPassword = crypt ($ _SERVER ["PHP_AUTH_PW " ], $ salt );
160- break ;
161- case 'sha1 ' :
162- $ hashedPassword = "{SHA} " . base64_encode (sha1 ($ _SERVER ["PHP_AUTH_PW " ], true ));
163- break ;
164- default :
165- $ hashedPassword = null ;
166- trigger_error ("unsupported password hashing algorithm " , E_USER_ERROR );
167- }
168- if ($ password == $ hashedPassword ) {
169- // A match is found, meaning the user is authenticated.
170- // Stop the search.
171- $ auth = true ;
172- break ;
173- }
174- }
175- } else {
176- trigger_error ("fishy line in basic auth file " , E_USER_WARNING );
177- }
152+ $ auth = $ this ->checkCredentials ($ _SERVER ["PHP_AUTH_USER " ], $ _SERVER ["PHP_AUTH_PW " ]);
153+ if (!$ auth && BasicAuth \HTML ::isAvailable ()) {
154+ // token fallback
155+ $ auth = in_array ($ this ->authUserDomain , BasicAuth \Token::useToken ($ _SERVER ["PHP_AUTH_USER " ], $ _SERVER ["PHP_AUTH_PW " ]));
178156 }
179- fclose ($ fp );
180157 }
181158 if (!$ auth ) {
182159 return false ;
@@ -186,6 +163,55 @@ public function checkAuthentication()
186163 }
187164 }
188165 }
166+ private function checkCredentials ($ user , $ password )
167+ {
168+ $ auth = false ;
169+ $ fp = fopen ($ this ->authUserFile , 'r ' );
170+ while ($ line = fgets ($ fp )) {
171+ $ line = trim ($ line );
172+ $ parts = explode (': ' , $ line );
173+ if (count ($ parts )==2 ) {
174+ list ($ fileUserName , $ filePasswordHash ) = $ parts ;
175+ if ($ fileUserName == $ user ) {
176+ switch (true ) {
177+ case strpos ($ filePasswordHash , '{SHA} ' ) === 0 :
178+ // inline sha
179+ $ this ->passwordEncryption = "sha1 " ;
180+ break ;
181+ }
182+ switch ($ this ->passwordEncryption ) {
183+ case 'crypt ' :
184+ // Get the salt from $password. It is always the first
185+ // two characters of a DES-encrypted string.
186+ $ salt = substr ($ filePasswordHash , 0 , 2 );
187+ $ hashedPassword = crypt ($ password , $ salt );
188+ break ;
189+ case 'sha1 ' :
190+ $ hashedPassword = "{SHA} " . base64_encode (sha1 ($ password , true ));
191+ break ;
192+ default :
193+ $ hashedPassword = null ;
194+ trigger_error ("unsupported password hashing algorithm " , E_USER_ERROR );
195+ }
196+ if ($ filePasswordHash == $ hashedPassword ) {
197+ // A match is found, meaning the user is authenticated.
198+ // Stop the search.
199+ $ auth = true ;
200+ break ;
201+ }
202+ }
203+ } else {
204+ trigger_error ("fishy line in basic auth file " , E_USER_WARNING );
205+ }
206+ }
207+ fclose ($ fp );
208+ return $ auth ;
209+ }
210+ public static function checkCredentialsForDomain ($ user , $ password , $ domain )
211+ {
212+ $ inst = new self ($ domain );
213+ return $ inst ->checkCredentials ($ user , $ password );
214+ }
189215 /**
190216 * check if you are authenticated use @see checkAuthentication instead
191217 *
@@ -197,8 +223,11 @@ public function getAuthenticated()
197223 return $ this ->checkAuthentication ();
198224 }
199225
200- public function logout ()
226+ public static function logout ()
201227 {
202-
228+ if (isset ($ _SERVER ["PHP_AUTH_USER " ])) {
229+ header ('WWW-Authenticate: Basic realm=" ' . \Foomo \Frontend::BASIC_AUTH_REALM . '", true, 401 ' );
230+ }
231+ BasicAuth \HTML ::logout ();
203232 }
204233}
0 commit comments