-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Acting as a Keycloak user in tests (#103)
* feat: acting as a Keycloak user in tests * fix: test coverage and functionality logic changed a bit
- Loading branch information
1 parent
cc051f5
commit aa71dbe
Showing
5 changed files
with
104 additions
and
17 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
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,46 @@ | ||
<?php | ||
|
||
namespace KeycloakGuard; | ||
|
||
use Firebase\JWT\JWT; | ||
use Illuminate\Support\Facades\Config; | ||
|
||
trait ActingAsKeycloakUser | ||
{ | ||
public function actingAsKeycloakUser($user = null) | ||
{ | ||
if (!$user) { | ||
Config::set('keycloak.load_user_from_database', false); | ||
} | ||
|
||
$token = $this->generateKeycloakToken($user); | ||
|
||
$this->withHeader('Authorization', 'Bearer '.$token); | ||
|
||
return $this; | ||
} | ||
|
||
public function generateKeycloakToken($user = null) | ||
{ | ||
$privateKey = openssl_pkey_new([ | ||
'digest_alg' => 'sha256', | ||
'private_key_bits' => 1024, | ||
'private_key_type' => OPENSSL_KEYTYPE_RSA | ||
]); | ||
|
||
$publicKey = openssl_pkey_get_details($privateKey)['key']; | ||
|
||
$publicKey = Token::plainPublicKey($publicKey); | ||
|
||
Config::set('keycloak.realm_public_key', $publicKey); | ||
|
||
$payload = [ | ||
'preferred_username' => $user->username ?? config('keycloak.preferred_username'), | ||
'resource_access' => [config('keycloak.allowed_resources') => []] | ||
]; | ||
|
||
$token = JWT::encode($payload, $privateKey, 'RS256'); | ||
|
||
return $token; | ||
} | ||
} |
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
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
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