Skip to content

Commit

Permalink
Merge pull request #1 from OfficialBAMM/feature/include_db_sync
Browse files Browse the repository at this point in the history
implement test for #45
  • Loading branch information
frital authored Mar 23, 2022
2 parents 8376791 + b6e841e commit 9943d75
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/KeycloakGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function validate(array $credentials = [])
if ($this->config['load_user_from_database']) {
$methodOnProvider = $this->config['user_provider_custom_retrieve_method'] ?? null;
if ($methodOnProvider) {
$user = $this->provider->{$methodOnProvider}($this->decodedToken);
$user = $this->provider->{$methodOnProvider}($this->decodedToken, $credentials);
} else {
$user = $this->provider->retrieveByCredentials($credentials);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/AuthenticateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
namespace KeycloakGuard\Tests;

use Illuminate\Http\Request;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Support\Facades\Auth;
use KeycloakGuard\KeycloakGuard;
use KeycloakGuard\Tests\Models\User;
use KeycloakGuard\KeycloakGuardServiceProvider;
use KeycloakGuard\Tests\Controllers\FooController;
use KeycloakGuard\Tests\Extensions\CustomUserProvider;
use Illuminate\Routing\Router;
use Illuminate\Events\Dispatcher;
use KeycloakGuard\Exceptions\UserNotFoundException;
Expand Down Expand Up @@ -181,4 +183,17 @@ public function it_prevent_cross_roles_resources()
}



/** @test */
public function custom_user_retrieve_method()
{
config(['keycloak.user_provider_custom_retrieve_method' => 'custom_retrieve']);

Auth::extend('keycloak', function ($app, $name, array $config) {
return new KeycloakGuard(new CustomUserProvider(new BcryptHasher(), User::class), $app->request);
});

$response = $this->withToken()->json('GET', '/foo/secret');
$this->assertTrue(Auth::user()->customRetrieve);
}
}
17 changes: 17 additions & 0 deletions tests/Extensions/CustomUserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace KeycloakGuard\Tests\Extensions;

use KeycloakGuard\Tests\Models\User;
use Illuminate\Auth\EloquentUserProvider;

class CustomUserProvider extends EloquentUserProvider
{
public function custom_retrieve(object $token, array $credentials)
{
$model = parent::retrieveByCredentials($credentials);
$model->customRetrieve = true;

return $model;
}
}

0 comments on commit 9943d75

Please sign in to comment.