Skip to content

Commit

Permalink
Don't disable db loading if a user is given in a payload
Browse files Browse the repository at this point in the history
  • Loading branch information
elnurvl committed May 20, 2024
1 parent 686985f commit ebd395d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/ActingAsKeycloakUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ trait ActingAsKeycloakUser

public function actingAsKeycloakUser($user = null, $payload = []): self
{
if (!$user) {
$principal = Config::get('keycloak.token_principal_attribute');
if (!$user && !isset($payload[$principal]) && !isset($this->payload[$principal])) {
Config::set('keycloak.load_user_from_database', false);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/AuthenticateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,14 +459,15 @@ public function test_acting_as_keycloak_user_trait_with_custom_payload(string $s
$arg = $payload;
}

$this->actingAsKeycloakUser($this->user, $arg)->json('GET', '/foo/secret');
$this->actingAsKeycloakUser(payload: $arg)->json('GET', '/foo/secret');

$this->assertEquals('test_username', Auth::user()->username);
$token = Token::decode(request()->bearerToken(), config('keycloak.realm_public_key'), config('keycloak.leeway'), config('keycloak.token_encryption_algorithm'));
$this->assertEquals(12345, $token->iat);
$this->assertEquals(9999999999999, $token->exp);
$this->assertEquals('test_sub', $token->sub);
$this->assertEquals('test_aud', $token->aud);
$this->assertTrue(config('keycloak.load_user_from_database'));
}

public function test_acting_as_keycloak_user_trait_without_user()
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class TestCase extends Orchestra
public array $defaultPayload;
public string $token;

protected User $user;

protected function setUp(): void
{
// Prepare credentials
Expand Down

0 comments on commit ebd395d

Please sign in to comment.