Authentication example using a secure hash (password_hash)

90 views
Skip to first unread message

John Ranson

unread,
Jun 30, 2017, 9:39:04 AM6/30/17
to Xataface
I came up with a solution for using password_hash and password_verify for authentication. It is a bit of a kludge, but it seems to work so far. (I've made a few cleanup edits, but I don't think I've introduced any typos.) I thought I'd share so that other people can benefit from it or point out issues.

Any thoughts?

--John

<?php
class tables_users {

function password__serialize($password) {

   if (empty($password)) return "-"; // Possibly unnecessary

   $query = "SELECT password FROM users where username='".$_POST['UserName']."'";
   $result = xf_db_query($query,df_db());
   if ( !$result ) throw new Exception(xf_db_error($app->db()));
   $line = xf_db_fetch_array($result);
   xf_db_free_result($result);

   $tmp_hash = password_hash($password, PASSWORD_DEFAULT);

   if (empty($line['password'])) return $tmp_hash; // Possibly unnecessary

   if (password_verify($password, $line[password])) return $line['password'];

   return $tmp_hash;
}
}

Steve Hannah

unread,
Jul 6, 2017, 12:53:44 PM7/6/17
to xata...@googlegroups.com
Looks good.  Thanks for sharing.  You might also want to refer to this doc

Which uses a similar approach to authenticate against the Joomla users table.

Steve

--
You received this message because you are subscribed to the Google Groups "Xataface" group.
Visit this group at https://groups.google.com/group/xataface.
To view this discussion on the web visit https://groups.google.com/d/msgid/xataface/566a5f61-d1da-4e06-aa7b-fed81eff000b%40googlegroups.com.



--
Steve Hannah
Web Lite Solutions Corp.

ge...@77webtechnology.co.uk

unread,
Aug 9, 2017, 3:30:56 AM8/9/17
to Xataface
This is good timing - it may be a kludge but I've recently had to upgrade to PHP 7 and my Joomla auth solution started throwing a deprecated warning concerning the fixed salt (extracted from the stored hash) used in the manual comparison. This solution gets around this by (properly) using the 'password_verify' function with a nice bit of lateral thinking on getting it working with the existing Xataface functionality. I have incorporated it into my script as mine also copes with older Joomla hashing routines - probably not necessary but a 'belt & braces' for those people that haven't had their password re-hashed via logging into Joomla first.

I would say the one PHP warning I was seeing wasn't so bad, but this was suddenly on top of over 50 Xataface core files needing fix-ups to prevent their own PHP7 generated 'Methods with the same name as their class will not be constructors in a future version of PHP' warning, which was creating large error logs (I don't have control over the granuality of error reporting on my production server). Anyway, these two fixes have meant all warnings have gone (for the time being!).

If anyone wants more on either of these issues, feel free to get in touch...

Steve Hannah

unread,
Aug 9, 2017, 9:58:18 AM8/9/17
to xata...@googlegroups.com
If you update Xataface to the latest from the github master, it should get rid of the PHP7 warnings.

--
You received this message because you are subscribed to the Google Groups "Xataface" group.
Visit this group at https://groups.google.com/group/xataface.

ge...@77webtechnology.co.uk

unread,
Aug 10, 2017, 7:19:04 AM8/10/17
to Xataface
Hi Steve,

I used what I thought was the latest master from Github as a base for an overall PHP upgrade (ver 2.2.0 4805?) in order to avoid as many problems as possible, but the class name/constructor warning is prevalent. I found a simple '__construct' renaming of the function is part of the solution, but it was quite a game getting the most commonly called scripts changed (example attached).
GlanceList.php

Steve Hannah

unread,
Aug 10, 2017, 10:17:30 AM8/10/17
to xata...@googlegroups.com
Please post any warning messages you get so I can fix them.

ge...@77webtechnology.co.uk

unread,
Aug 11, 2017, 11:44:58 AM8/11/17
to Xataface
So this was the warning:

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; foo has a deprecated constructor in example.php on line 3

referred to here:

http://php.net/manual/en/migration70.deprecated.php


On Friday, June 30, 2017 at 2:39:04 PM UTC+1, John Ranson wrote:

Steve Hannah

unread,
Aug 11, 2017, 11:47:23 AM8/11/17
to xata...@googlegroups.com
I need exact warnings to see where they occur to fix them

--
You received this message because you are subscribed to the Google Groups "Xataface" group.
Visit this group at https://groups.google.com/group/xataface.

ge...@77webtechnology.co.uk

unread,
Aug 11, 2017, 1:28:03 PM8/11/17
to Xataface
I think this error log shows most I initially encountered. For completeness, the other file lists the file edits I did to eliminate the errors.


On Friday, June 30, 2017 at 2:39:04 PM UTC+1, John Ranson wrote:
error_log
xataface_edits.txt

Steve Hannah

unread,
Aug 12, 2017, 10:28:35 AM8/12/17
to xata...@googlegroups.com
Does that xataface_edits.txt file suggest that you have already fixed those files locally?  If so, can you share?  (ideally, submit a pull request - but if not, just providing the changes some how).  

Best regards

Steve

--
You received this message because you are subscribed to the Google Groups "Xataface" group.
Visit this group at https://groups.google.com/group/xataface.

ge...@77webtechnology.co.uk

unread,
Aug 13, 2017, 6:16:33 AM8/13/17
to Xataface
I've used a fork from Github and pushed the edited files. Not done this stuff for a while so please forgive any gaffs. I see some possible versions diffs, but I've only changed the initial function statement (where same as class name) of each file...
PS Obvious point is that these are only the files that have been called by my apps - no doubt others. Also there's one file in module depselect which I can follow up


On Friday, June 30, 2017 at 2:39:04 PM UTC+1, John Ranson wrote:
Reply all
Reply to author
Forward
0 new messages