-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8583cd7
Showing
9 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
* text=auto | ||
|
||
*.php diff=php | ||
|
||
/.github export-ignore | ||
/tests export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
phpunit.xml.dist export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: tests | ||
|
||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
tests: | ||
runs-on: ubuntu-20.04 | ||
|
||
strategy: | ||
fail-fast: true | ||
matrix: | ||
php: [7.4, 8.0, 8.1] | ||
laravel: [^6.0, ^7.0, ^8.0] | ||
|
||
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick | ||
tools: composer:v2 | ||
coverage: none | ||
|
||
- name: Install dependencies | ||
run: | | ||
composer require "illuminate/contracts=${{ matrix.laravel }}" --no-update | ||
composer update --prefer-dist --no-interaction --no-progress | ||
- name: Execute tests | ||
run: vendor/bin/phpunit --verbose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/vendor | ||
composer.lock | ||
.DS_Store | ||
.phpunit.result.cache | ||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "goedemiddag/auth", | ||
"license": "MIT", | ||
"require": { | ||
"php": "^7.4|^8.0", | ||
"illuminate/auth": "^6.0|^7.0|^8.0" | ||
}, | ||
"require-dev": { | ||
"mockery/mockery": "^1.0", | ||
"phpunit/phpunit": "^8.5" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Goedemiddag\\Auth\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Goedemiddag\\Auth\\Tests\\": "tests/" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Goedemiddag\\Auth\\ServiceProvider" | ||
] | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
beStrictAboutTestsThatDoNotTestAnything="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertDeprecationsToExceptions="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Unit"> | ||
<directory suffix="Test.php">./tests/Unit</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Auth library | ||
|
||
## Simple | ||
|
||
Configure a single username/password to login with. Useful in combination with basic auth middleware (`auth.basic:simple`). | ||
```php | ||
// config/auth.php | ||
|
||
'guards' => [ | ||
'simple' => [ | ||
'driver' => 'session', | ||
'provider' => 'simple', | ||
], | ||
], | ||
|
||
'providers' => [ | ||
'simple' => [ | ||
'driver' => 'gm:simple', | ||
'email' => env('SIMPLE_AUTH_USER'), | ||
'password' => env('SIMPLE_AUTH_PASS'), | ||
], | ||
], | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Goedemiddag\Auth; | ||
|
||
use Illuminate\Support\Facades\Auth; | ||
|
||
class ServiceProvider extends \Illuminate\Support\ServiceProvider | ||
{ | ||
|
||
public function boot() | ||
{ | ||
Auth::provider('gm:simple', function ($app, array $config) { | ||
return new SimpleUserProvider($config['email'], $config['password']); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Goedemiddag\Auth; | ||
|
||
use Illuminate\Auth\GenericUser; | ||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Contracts\Auth\UserProvider; | ||
|
||
class SimpleUserProvider implements UserProvider | ||
{ | ||
|
||
private string $email; | ||
private string $password; | ||
|
||
|
||
public function __construct(string $email, string $password) | ||
{ | ||
$this->email = $email; | ||
$this->password = $password; | ||
} | ||
|
||
|
||
public function retrieveById($identifier) | ||
{ | ||
if ($identifier === $this->email) { | ||
return $this->getGenericUser(); | ||
} | ||
} | ||
|
||
|
||
public function retrieveByToken($identifier, $token) | ||
{ | ||
// Not implemented | ||
} | ||
|
||
|
||
public function updateRememberToken(Authenticatable $user, $token) | ||
{ | ||
// Not implemented | ||
} | ||
|
||
|
||
public function retrieveByCredentials(array $credentials) | ||
{ | ||
if ($credentials['email'] === $this->email) { | ||
return $this->getGenericUser(); | ||
} | ||
} | ||
|
||
|
||
public function validateCredentials(Authenticatable $user, array $credentials) | ||
{ | ||
return hash_equals($user->getAuthPassword(), $credentials['password']); | ||
} | ||
|
||
|
||
private function getGenericUser() | ||
{ | ||
return new GenericUser([ | ||
'id' => $this->email, | ||
'password' => $this->password, | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Goedemiddag\Auth\Tests\Unit; | ||
|
||
use Goedemiddag\Auth\SimpleUserProvider; | ||
use Illuminate\Auth\GenericUser; | ||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SimpleUserProviderTest extends TestCase | ||
{ | ||
|
||
public function testRetrieveByIDReturnsUserWhenUserIsFound() | ||
{ | ||
$provider = new SimpleUserProvider('foo', 'bar'); | ||
$user = $provider->retrieveById('foo'); | ||
|
||
$this->assertInstanceOf(GenericUser::class, $user); | ||
$this->assertSame('foo', $user->getAuthIdentifier()); | ||
} | ||
|
||
|
||
public function testRetrieveByIDReturnsNullWhenUserIsNotFound() | ||
{ | ||
$provider = new SimpleUserProvider('foo', 'bar'); | ||
$user = $provider->retrieveById(1); | ||
|
||
$this->assertNull($user); | ||
} | ||
|
||
|
||
public function testRetrieveByCredentialsReturnsUserWhenUserIsFound() | ||
{ | ||
$provider = new SimpleUserProvider('foo', 'bar'); | ||
$user = $provider->retrieveByCredentials(['email' => 'foo', 'password' => 'bar']); | ||
|
||
$this->assertInstanceOf(GenericUser::class, $user); | ||
$this->assertSame('foo', $user->getAuthIdentifier()); | ||
} | ||
|
||
|
||
public function testRetrieveByCredentialsReturnsNullWhenUserIsFound() | ||
{ | ||
$provider = new SimpleUserProvider('foo', 'bar'); | ||
$user = $provider->retrieveByCredentials(['email' => 'dayle']); | ||
|
||
$this->assertNull($user); | ||
} | ||
|
||
|
||
public function testCredentialValidation() | ||
{ | ||
$provider = new SimpleUserProvider('foo', 'bar'); | ||
$user = m::mock(Authenticatable::class); | ||
$user->shouldReceive('getAuthPassword')->once()->andReturn('bar'); | ||
$result = $provider->validateCredentials($user, ['password' => 'bar']); | ||
|
||
$this->assertTrue($result); | ||
} | ||
|
||
|
||
public function testCredentialValidationWithWrongPassword() | ||
{ | ||
$provider = new SimpleUserProvider('foo', 'bar'); | ||
$user = m::mock(Authenticatable::class); | ||
$user->shouldReceive('getAuthPassword')->once()->andReturn('bar'); | ||
$result = $provider->validateCredentials($user, ['password' => 'wrong']); | ||
|
||
$this->assertFalse($result); | ||
} | ||
|
||
} |