Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bonroyage committed Nov 25, 2021
0 parents commit 8583cd7
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitattributes
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
36 changes: 36 additions & 0 deletions .github/workflows/tests.yml
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor
composer.lock
.DS_Store
.phpunit.result.cache
/.idea
32 changes: 32 additions & 0 deletions composer.json
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
}
}
19 changes: 19 additions & 0 deletions phpunit.xml.dist
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>
23 changes: 23 additions & 0 deletions readme.md
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'),
],
],
```
17 changes: 17 additions & 0 deletions src/ServiceProvider.php
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']);
});
}

}
65 changes: 65 additions & 0 deletions src/SimpleUserProvider.php
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,
]);
}

}
73 changes: 73 additions & 0 deletions tests/Unit/SimpleUserProviderTest.php
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);
}

}

0 comments on commit 8583cd7

Please sign in to comment.