Skip to content

Commit

Permalink
Add PdoSource, add setError, setResult in Source
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Nov 16, 2024
1 parent 553e28f commit d561111
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 37 deletions.
20 changes: 2 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ $source->setTableName('users');
> [!NOTE]
> If the `setTableName` is not specified, it always refer to the `users` table.
There may be a scenario that there are other fields to use besides `username` and `password`. With this, kindly use the `setData` method:
When using `PdoSource`, the value in the `password` field will be assumed as a hash (e.g., `$2y$10...`). If this is not the case, kindly add the `withoutHash` method:

``` php
// index.php
Expand All @@ -257,27 +257,11 @@ use Rougin\Authsum\Source\PdoSource;

$source = new PdoSource($pdo);

$data = array('type' => 'admin');

$source->setData($data);
$source->withoutHash();

// ...
```

If `setData` is defined, the provided data will be added as `WHERE` queries to the SQL query:

**Before**

``` sql
SELECT u.* FROM users u WHERE u.username = ?
```

**After**

``` sql
SELECT u.* FROM users u WHERE u.username = ? AND u.type = ?
```

#### `JwtSource`

The `JwtSource` class is a special class that checks a user's authentication using [JSON Web Token](https://en.wikipedia.org/wiki/JSON_Web_Token):
Expand Down
32 changes: 32 additions & 0 deletions src/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,36 @@ public function getResult()

return $this->result;
}

/**
* @param string $text
*
* @return boolean
*/
protected function setError($text = self::CREDENTIALS_INVALID)
{
$error = new Error;

$error->setText($text);

$this->error = $error;

return false;
}

/**
* @param string $text
*
* @return boolean
*/
protected function setResult($text = self::CREDENTIALS_MATCHED)
{
$result = new Result;

$result->setText($text);

$this->result = $result;

return true;
}
}
24 changes: 5 additions & 19 deletions src/Source/BasicSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Rougin\Authsum\Source;

use Rougin\Authsum\Error;
use Rougin\Authsum\Result;
use Rougin\Authsum\Source;

/**
Expand Down Expand Up @@ -61,28 +59,16 @@ public function __construct($username, $password)
*/
public function isValid()
{
$sameUsername = $this->sourceUsername === $this->usernameValue;
$sameUser = $this->sourceUsername === $this->usernameValue;

$samePassword = $this->sourcePassword === $this->passwordValue;
$samePass = $this->sourcePassword === $this->passwordValue;

if ($sameUsername && $samePassword)
if ($sameUser && $samePass)
{
$result = new Result;

$result->setText('Credentials matched!');

$this->result = $result;

return true;
return $this->setResult();
}

$error = new Error;

$error->setText('Invalid credentials given.');

$this->error = $error;

return false;
return $this->setError();
}

/**
Expand Down
192 changes: 192 additions & 0 deletions src/Source/PdoSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

namespace Rougin\Authsum\Source;

use Rougin\Authsum\Error;
use Rougin\Authsum\Source;

/**
* @package Authsum
*
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class PdoSource extends Source implements WithUsername, WithPassword
{
/**
* @var string
*/
protected $passwordField;

/**
* @var string
*/
protected $passwordValue;

/**
* @var \PDO
*/
protected $pdo;

/**
* @var string
*/
protected $table = 'users';

/**
* @var string
*/
protected $usernameField;

/**
* @var string
*/
protected $usernameValue;

/**
* @var boolean
*/
protected $withHash = true;

/**
* @param \PDO $pdo
*/
public function __construct(\PDO $pdo)
{
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

$this->pdo = $pdo;
}

/**
* Checks if it exists from the source.
*
* @return boolean
*/
public function isValid()
{
$username = $this->usernameField;

$table = $this->table;

$query = "SELECT * FROM $table WHERE $username = ?";

$error = new Error;

try
{
/** @var \PDOStatement */
$stmt = $this->pdo->prepare($query);

$stmt->execute(array($this->usernameValue));

/** @var array<string, string> */
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
}
catch (\Exception $e)
{
return $this->setError($e->getMessage());
}

$hash = $row[$this->passwordField];

$value = $this->passwordValue;

$samePass = password_verify($value, $hash);

if (! $this->withHash)
{
$samePass = $row[$this->passwordField] === $value;
}

$value = $this->usernameValue;

$sameUser = $row[$this->usernameField] === $value;

if ($sameUser && $samePass)
{
return $this->setResult();
}

return $this->setError();
}

/**
* Sets the password field.
*
* @param string $password
*
* @return self
*/
public function setPasswordField($password)
{
$this->passwordField = $password;

return $this;
}

/**
* Sets the password value.
*
* @param string $password
*
* @return self
*/
public function setPasswordValue($password)
{
$this->passwordValue = $password;

return $this;
}

/**
* Sets the table name.
*
* @param string $table
*
* @return self
*/
public function setTableName($table)
{
$this->table = $table;

return $this;
}

/**
* Sets the username field.
*
* @param string $username
*
* @return self
*/
public function setUsernameField($username)
{
$this->usernameField = $username;

return $this;
}

/**
* Sets the username value.
*
* @param string $username
*
* @return self
*/
public function setUsernameValue($username)
{
$this->usernameValue = $username;

return $this;
}

/**
* @return self
*/
public function withoutHash()
{
$this->withHash = false;

return $this;
}
}
4 changes: 4 additions & 0 deletions src/Source/SourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
*/
interface SourceInterface
{
const CREDENTIALS_MATCHED = 'Credentials matched!';

const CREDENTIALS_INVALID = 'Invalid credentials given.';

/**
* Returns the error after validation.
*
Expand Down
Binary file added tests/Fixture/Storage/athsm.s3db
Binary file not shown.
Loading

0 comments on commit d561111

Please sign in to comment.