Skip to content

Commit

Permalink
🍺 Pint
Browse files Browse the repository at this point in the history
  • Loading branch information
Daynnnnn committed Feb 1, 2025
1 parent 0aa92eb commit 2124da0
Show file tree
Hide file tree
Showing 13 changed files with 938 additions and 1,583 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"phpstan/phpstan": "^1.8",
"pestphp/pest": "^2.6",
"orchestra/testbench": "^8.5",
"directorytree/ldaprecord": "^3.7"
"directorytree/ldaprecord": "^3.7",
"laravel/pint": "^1.20"
},
"config": {
"allow-plugins": {
Expand Down
2,403 changes: 882 additions & 1,521 deletions composer.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions config/forward-authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'driver' => 'http',

'address' => env('STATAMIC_FORWARD_AUTH_HTTP_ADDRESS'),

'response' => [
'success' => 'result',
'name' => 'data.name',
Expand All @@ -50,15 +50,15 @@
],

'port' => env('STATAMIC_FORWARD_AUTH_LDAP_PORT', 389),

'use_ssl' => env('STATAMIC_FORWARD_AUTH_LDAP_SSL', false),

'base_dn' => env('STATAMIC_FORWARD_AUTH_BASE_DN'),

'username' => env('STATAMIC_FORWARD_AUTH_BIND_USERNAME'),

'password' => env('STATAMIC_FORWARD_AUTH_BIND_PASSWORD'),

'queryCallback' => fn (Connection $connection, array $credentials) => $connection->query()->where('mail', '=', $credentials['email'])->first(),
],

Expand Down
4 changes: 2 additions & 2 deletions src/AuthServices/AuthServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ interface AuthServiceContract
public function checkCredentialsAgainstForwardAuth(array $credentials): array|false;

public function credentialsValidAgainstForwardAuth(): bool;

public function userData(): array;
}
}
18 changes: 12 additions & 6 deletions src/AuthServices/HttpAuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,33 @@
class HttpAuthService implements AuthServiceContract
{
protected $config;

protected $data;

protected $forwardAuthUser = false;

public function __construct() {
public function __construct()
{
$service = config('statamic.forward-authentication.default');
$this->config = config("statamic.forward-authentication.services.$service");
$this->data = config("statamic.forward-authentication.data");
$this->data = config('statamic.forward-authentication.data');
}

public function checkCredentialsAgainstForwardAuth(array $credentials): array|false {
public function checkCredentialsAgainstForwardAuth(array $credentials): array|false
{
return $this->forwardAuthUser = Http::post($this->config['address'], $credentials)->json();
}

public function credentialsValidAgainstForwardAuth(): bool {
public function credentialsValidAgainstForwardAuth(): bool
{
return Arr::get($this->forwardAuthUser, $this->config['response']['success']);
}

public function userData(): array {
public function userData(): array
{
return array_merge($this->data, [
'name' => Arr::get($this->forwardAuthUser, $this->config['response']['name']),
'forward_auth' => true,
]);
}
}
}
25 changes: 15 additions & 10 deletions src/AuthServices/LdapAuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@

namespace Daynnnnn\Statamic\Auth\ForwardAuth\AuthServices;

use LdapRecord\Connection;
use Daynnnnn\Statamic\Auth\ForwardAuth\Exceptions\LdapRecordNotFoundException;
use Illuminate\Support\Arr;
use LdapRecord\Connection;

class LdapAuthService implements AuthServiceContract
{
protected $config;

protected $data;

protected $forwardAuthUser = false;

public function __construct() {
if (!class_exists(Connection::class)) {
throw new LdapRecordNotFoundException("Using the LDAP service requires the LdapRecord package", 1);
public function __construct()
{
if (! class_exists(Connection::class)) {
throw new LdapRecordNotFoundException('Using the LDAP service requires the LdapRecord package', 1);
}

$service = config('statamic.forward-authentication.default');
$this->config = config("statamic.forward-authentication.services.$service");
$this->data = config("statamic.forward-authentication.data");
$this->data = config('statamic.forward-authentication.data');
}

public function checkCredentialsAgainstForwardAuth(array $credentials): array|false {
public function checkCredentialsAgainstForwardAuth(array $credentials): array|false
{
$connectionConfig = [
'hosts' => $this->config['hosts'],
'port' => $this->config['port'],
Expand Down Expand Up @@ -54,19 +57,21 @@ public function checkCredentialsAgainstForwardAuth(array $credentials): array|fa

$userConnection->connect();

// Connection will throw an exception if it fails, so if we get to
// Connection will throw an exception if it fails, so if we get to
// this step then set the forwardAuthUser to the returned user
$this->forwardAuthUser = $user;
} finally {
return $this->forwardAuthUser;
}
}

public function credentialsValidAgainstForwardAuth(): bool {
public function credentialsValidAgainstForwardAuth(): bool
{
return $this->forwardAuthUser !== false;
}

public function userData(): array {
public function userData(): array
{
return array_merge($this->data, [
'name' => $this->forwardAuthUser['displayname'][0],
'forward_auth' => true,
Expand Down
3 changes: 1 addition & 2 deletions src/Exceptions/LdapRecordNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace Daynnnnn\Statamic\Auth\ForwardAuth\Exceptions;

use Exception;
use Facade\IgnitionContracts\Solution;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\ProvidesSolution;
use Facade\IgnitionContracts\Solution;

class LdapRecordNotFoundException extends Exception implements ProvidesSolution
{
/** @return \Facade\IgnitionContracts\Solution */
public function getSolution(): Solution
{
return BaseSolution::create('Install LdapRecord')
Expand Down
5 changes: 3 additions & 2 deletions src/ForwardAuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Daynnnnn\Statamic\Auth\ForwardAuth;

use Daynnnnn\Statamic\Auth\ForwardAuth\AuthServices;
use Illuminate\Support\Facades\Auth;
use Statamic\Providers\AddonServiceProvider;

Expand All @@ -15,6 +14,7 @@ public function register()
$this->app->bind(AuthServices\AuthServiceContract::class, function () {
$service = config('statamic.forward-authentication.default');
$class = $this->lookupType(config("statamic.forward-authentication.services.$service.driver"));

return new $class;
});
}
Expand All @@ -30,7 +30,8 @@ public function boot()
});
}

protected function lookupType($type) {
protected function lookupType($type)
{
$types = [
'http' => AuthServices\HttpAuthService::class,
'ldap' => AuthServices\LdapAuthService::class,
Expand Down
12 changes: 6 additions & 6 deletions src/ForwardAuthUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public function retrieveByCredentials(array $credentials)

if (AuthService::credentialsValidAgainstForwardAuth()) {
return User::make()
->email($credentials['email'])
->password($credentials['password'])
->data(AuthService::userData())
->save();
->email($credentials['email'])
->password($credentials['password'])
->data(AuthService::userData())
->save();
}
}

Expand All @@ -35,9 +35,9 @@ public function validateCredentials(Authenticatable $user, array $credentials)
if ($user->forward_auth === true) {
AuthService::checkCredentialsAgainstForwardAuth($credentials);

if (!AuthService::credentialsValidAgainstForwardAuth()) {
if (! AuthService::credentialsValidAgainstForwardAuth()) {
$localCredentialsValid = false;
} elseif (!$localCredentialsValid) {
} elseif (! $localCredentialsValid) {
$user->password($credentials['password'])->save();
$localCredentialsValid = true;
}
Expand Down
9 changes: 0 additions & 9 deletions tests/Feature/ForwardAuthUserProviderTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
<?php

use Daynnnnn\Statamic\Auth\ForwardAuth\ForwardAuthUserProvider;
use Daynnnnn\Statamic\Auth\ForwardAuth\AuthServices\AuthServiceContract;
use Daynnnnn\Statamic\Auth\ForwardAuth\AuthServices\HttpAuthService;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Http;
use Statamic\Auth\File\UserGroupRepository as FileUserGroupRepository;
use Statamic\Facades\User;
use Statamic\Contracts\Auth\UserGroupRepository;
use Statamic\Contracts\Auth\UserRepository;
use Statamic\Stache\Stache;
use Statamic\Stache\Stores\UsersStore;
use Statamic\Stache\Repositories\UserRepository as StacheUserRepository;

test('creates user if not found locally and is valid against forward authentication', function () {
$forwardAuthUser = setupHttpAuthService(true);
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/HttpAuthServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$this->assertEquals($mockRepsonse, $forwardAuthUser->checkCredentialsAgainstForwardAuth($credentials));
});

test('credentialsValidAgainstForwardAuth returns true with valid user', function() {
test('credentialsValidAgainstForwardAuth returns true with valid user', function () {
$credentials = [
'email' => '[email protected]',
'password' => 'supersecure',
Expand All @@ -29,7 +29,7 @@
$this->assertTrue($forwardAuthUser->credentialsValidAgainstForwardAuth());
});

test('userData returns expected data array', function() {
test('userData returns expected data array', function () {
$expectedData = [
'super' => true,
'forward_auth' => true,
Expand All @@ -44,4 +44,4 @@
$forwardAuthUser = tap(setupHttpAuthService(true))->checkCredentialsAgainstForwardAuth($credentials);

$this->assertEquals($expectedData, $forwardAuthUser->userData());
});
});
9 changes: 5 additions & 4 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
|
*/

function setupHttpAuthService($status) {
function setupHttpAuthService($status)
{
$mockRepsonse = [
'success' => $status,
'data' => [
Expand All @@ -60,7 +61,7 @@ function setupHttpAuthService($status) {
'success' => 'success',
'name' => 'data.name',
],
]
],
],
'data' => [
'super' => true,
Expand All @@ -70,11 +71,11 @@ function setupHttpAuthService($status) {
$authConfig = [
'driver' => 'forward',
];

config(['statamic.forward-authentication' => $forwardAuthConfig]);
config(['auth.providers.users' => $authConfig]);

Http::fake(Http::response($mockRepsonse, 200));

return new HttpAuthService;
}
}
12 changes: 1 addition & 11 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@

namespace Daynnnnn\Statamic\Auth\ForwardAuth\Tests;

use Daynnnnn\Statamic\Auth\ForwardAuth\AuthServices\AuthServiceContract;
use Daynnnnn\Statamic\Auth\ForwardAuth\AuthServices\HttpAuthService;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Statamic\Auth\File\UserGroupRepository as FileUserGroupRepository;
use Statamic\Contracts\Auth\UserGroupRepository;
use Statamic\Contracts\Auth\UserRepository;
use Statamic\Facades\Stache;
use Statamic\Stache\Stores\UsersStore;
use Statamic\Stache\Repositories\UserRepository as StacheUserRepository;
use Statamic\Facades\UserGroup;

class TestCase extends BaseTestCase
{

protected function getPackageProviders($app)
{
return [
Expand All @@ -30,4 +20,4 @@ protected function setUp(): void

config(['statamic.users.repository' => 'file']);
}
}
}

0 comments on commit 2124da0

Please sign in to comment.