Skip to content

Instantly share code, notes, and snippets.

@mladoux
Created March 27, 2019 18:14
Show Gist options
  • Save mladoux/05b4b60dd8dcd839280b0e1eea3652b5 to your computer and use it in GitHub Desktop.
Save mladoux/05b4b60dd8dcd839280b0e1eea3652b5 to your computer and use it in GitHub Desktop.
Simple authentication class. Does not handle permissions or anything like that. Just something put together real quick and dirty.
<?php
/**
* Auth
*
* Verifies authentication credentials
*
* @author Mark LaDoux <[email protected]>
*/
class Auth
{
/**
* Create new credentials.
*
* @access public
* @param string $email email address for user.
* @param string $password password to hash.
* @return array Array of values to store in database.
*/
public function create(string $email, string $password)
{
// Create a UTC Timestamp of now for the created field.
$now = new DateTime;
$now->setTimezone(new DateTimeZone("UTC"));
$created = $now->format('Y-m-d H:i:s');
// check inputs ( Will build better error handling later. )
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \Exception("Error Processing Request: Invalid E-Mail address.", 1);
}
// Create password hash
$hash = password_hash($password, PASSWORD_DEFAULT);
return [
'created' => $now,
'email' => $email,
'password' => $hash
];
}
/**
* Verify password against stored hash.
*
* @access public
* @param string $password cleartext password.
* @param string $stored_hash stored password hash.
* @return array results.
*/
public function verify(string $password, string $stored_hash)
{
// check if passord needs rehash
$rehash = password_needs_rehash($stored_hash, PASSWORD_DEFAULT);
$valid = password_verify($password, $stored_hash);
return [
'rehash' => $rehash,
'valid' => $valid
];
}
/**
* Create new password hash using current standards.
*
* @access public
* @param string $password cleartext password.
* @return string hashed password.
*/
public function update(string $password)
{
return password_hash($password, PASSWORD_DEFAULT);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment