Skip to content

Commit 121f9b9

Browse files
authored
Merge branch 'master' into feat/events
2 parents fda35e3 + 9107870 commit 121f9b9

File tree

10 files changed

+71
-24
lines changed

10 files changed

+71
-24
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<img src="https://img.shields.io/packagist/dt/vizir/laravel-keycloak-web-guard.svg" />
44
</p>
55

6-
# Keycloak Web Guard for Laravel
6+
# [NEEDS A MAINTAINER] Keycloak Web Guard for Laravel
77

88
This packages allow you authenticate users with [Keycloak Server](https://www.keycloak.org).
99

@@ -18,7 +18,7 @@ It works on front. For APIs we recommend [laravel-keycloak-guard](https://github
1818

1919
This package was tested with:
2020

21-
* Laravel: 5.8 / 7 / 8 / 9
21+
* Laravel: 5.8 / 7 / 8 / 9 / 10
2222
* Keycloak: 18.0.0
2323

2424
Any other version is not guaranteed to work.
@@ -64,6 +64,18 @@ After publishing `config/keycloak-web.php` file, you can change the routes:
6464
]
6565
```
6666

67+
The scope `openid` is always included, but if you need extra scopes you can add them as strings to the array:
68+
69+
```php
70+
'scopes' => [],
71+
```
72+
73+
Example:
74+
75+
```php
76+
'scopes' => ['example_scope_1', 'example_scope_2'],
77+
```
78+
6779
Change any value to change the URL.
6880

6981
Other configurations can be changed to have a new default value, but we recommend to use `.env` file:

config/keycloak-web.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,11 @@
7373
* @link http://docs.guzzlephp.org/en/stable/request-options.html
7474
*/
7575
'guzzle_options' => [],
76+
77+
/**
78+
* Keycloak optional scopes
79+
*
80+
* array of strings
81+
*/
82+
'scopes' => [],
7683
];

src/Auth/Guard/KeycloakWebGuard.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ class KeycloakWebGuard implements Guard
2222
protected $user;
2323

2424
/**
25-
* Constructor.
26-
*
27-
* @param Request $request
25+
* @var UserProvider
26+
*/
27+
protected $provider;
28+
29+
/**
30+
* @var Request
2831
*/
32+
protected $request;
33+
2934
public function __construct(UserProvider $provider, Request $request)
3035
{
3136
$this->provider = $provider;
@@ -41,7 +46,10 @@ public function check()
4146
{
4247
return (bool) $this->user();
4348
}
44-
49+
50+
/**
51+
* @return bool
52+
*/
4553
public function hasUser()
4654
{
4755
return (bool) $this->user();
@@ -74,7 +82,7 @@ public function user()
7482
/**
7583
* Set the current user.
7684
*
77-
* @param \Illuminate\Contracts\Auth\Authenticatable $user
85+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
7886
* @return void
7987
*/
8088
public function setUser(?Authenticatable $user)
@@ -131,7 +139,7 @@ public function validate(array $credentials = [])
131139
* Try to authenticate the user
132140
*
133141
* @throws KeycloakCallbackException
134-
* @return boolean
142+
* @return bool
135143
*/
136144
public function authenticate()
137145
{
@@ -166,7 +174,7 @@ public function authenticate()
166174
*
167175
* @param string $resource Default is empty: point to client_id
168176
*
169-
* @return array
177+
* @return bool|array
170178
*/
171179
public function roles($resource = '')
172180
{
@@ -200,7 +208,7 @@ public function roles($resource = '')
200208
* @param array|string $roles
201209
* @param string $resource Default is empty: point to client_id
202210
*
203-
* @return boolean
211+
* @return bool
204212
*/
205213
public function hasRole($roles, $resource = '')
206214
{

src/Auth/KeycloakAccessToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function getIdToken()
8585
/**
8686
* Check access token has expired
8787
*
88-
* @return boolean
88+
* @return bool
8989
*/
9090
public function hasExpired()
9191
{
@@ -145,7 +145,7 @@ public function validateIdToken($claims)
145145
/**
146146
* Validate sub from ID token
147147
*
148-
* @return boolean
148+
* @return bool
149149
*/
150150
public function validateSub($userSub)
151151
{

src/Auth/KeycloakWebUserProvider.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,17 @@ public function validateCredentials(Authenticatable $user, array $credentials)
8484
{
8585
throw new \BadMethodCallException('Unexpected method [validateCredentials] call');
8686
}
87+
88+
/**
89+
* Rehash the user's password if required and supported.
90+
*
91+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
92+
* @param array $credentials
93+
* @param bool $force
94+
* @return void
95+
*/
96+
public function rehashPasswordIfRequired(UserContract $user, array $credentials, bool $force = false)
97+
{
98+
throw new \BadMethodCallException('Unexpected method [rehashPasswordIfRequired] call');
99+
}
87100
}

src/Controllers/AuthController.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Vizir\KeycloakWebGuard\Controllers;
44

55
use Illuminate\Auth\Events\Logout;
6+
use Illuminate\Http\RedirectResponse;
67
use Illuminate\Http\Request;
78
use Illuminate\Routing\Controller;
89
use Illuminate\Support\Facades\Auth;
@@ -14,7 +15,7 @@ class AuthController extends Controller
1415
/**
1516
* Redirect to login
1617
*
17-
* @return view
18+
* @return RedirectResponse
1819
*/
1920
public function login()
2021
{
@@ -27,20 +28,22 @@ public function login()
2728
/**
2829
* Redirect to logout
2930
*
30-
* @return view
31+
* @return RedirectResponse
3132
*/
3233
public function logout()
3334
{
3435
$url = KeycloakWeb::getLogoutUrl();
3536
KeycloakWeb::forgetToken();
37+
3638
event(new Logout(Auth::getDefaultDriver(), Auth()->user()));
39+
3740
return redirect($url);
3841
}
3942

4043
/**
4144
* Redirect to register
4245
*
43-
* @return view
46+
* @return RedirectResponse
4447
*/
4548
public function register()
4649
{
@@ -53,7 +56,7 @@ public function register()
5356
*
5457
* @throws KeycloakCallbackException
5558
*
56-
* @return view
59+
* @return RedirectResponse
5760
*/
5861
public function callback(Request $request)
5962
{

src/KeycloakWebGuardServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Vizir\KeycloakWebGuard\Middleware\KeycloakAuthenticated;
1515
use Vizir\KeycloakWebGuard\Middleware\KeycloakCan;
1616
use Vizir\KeycloakWebGuard\Middleware\KeycloakCanOne;
17-
use Vizir\KeycloakWebGuard\Models\KeycloakUser;
1817
use Vizir\KeycloakWebGuard\Services\KeycloakService;
1918

2019
class KeycloakWebGuardServiceProvider extends ServiceProvider

src/Middleware/KeycloakCan.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Closure;
66
use Illuminate\Auth\Access\AuthorizationException;
77
use Illuminate\Support\Facades\Auth;
8-
use Vizir\KeycloakWebGuard\Exceptions\KeycloakCanException;
98

109
class KeycloakCan extends KeycloakAuthenticated
1110
{

src/Middleware/KeycloakCanOne.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Closure;
66
use Illuminate\Auth\Access\AuthorizationException;
77
use Illuminate\Support\Facades\Auth;
8-
use Vizir\KeycloakWebGuard\Exceptions\KeycloakCanException;
98

109
class KeycloakCanOne extends KeycloakAuthenticated
1110
{

src/Services/KeycloakService.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@
1111
use Illuminate\Support\Facades\Config;
1212
use Illuminate\Support\Facades\Route;
1313
use Vizir\KeycloakWebGuard\Auth\KeycloakAccessToken;
14-
use Vizir\KeycloakWebGuard\Auth\Guard\KeycloakWebGuard;
1514

1615
class KeycloakService
1716
{
1817
/**
1918
* The Session key for token
2019
*/
21-
const KEYCLOAK_SESSION = '_keycloak_token';
20+
public const KEYCLOAK_SESSION = '_keycloak_token';
2221

2322
/**
2423
* The Session key for state
2524
*/
26-
const KEYCLOAK_SESSION_STATE = '_keycloak_state';
25+
public const KEYCLOAK_SESSION_STATE = '_keycloak_state';
2726

2827
/**
2928
* Keycloak URL
@@ -95,6 +94,11 @@ class KeycloakService
9594
*/
9695
protected $httpClient;
9796

97+
/**
98+
* @var array of strings
99+
*/
100+
protected $scopes = ['openid'];
101+
98102
/**
99103
* The Constructor
100104
* You can extend this service setting protected variables before call
@@ -133,6 +137,8 @@ public function __construct(ClientInterface $client)
133137
$this->redirectLogout = Config::get('keycloak-web.redirect_logout');
134138
}
135139

140+
$this->scopes = array_merge($this->scopes, Config::get('keycloak-web.scopes'));
141+
136142
$this->state = $this->generateRandomState();
137143
$this->httpClient = $client;
138144
}
@@ -148,7 +154,7 @@ public function getLoginUrl()
148154
{
149155
$url = $this->getOpenIdValue('authorization_endpoint');
150156
$params = [
151-
'scope' => 'openid',
157+
'scope' => implode(' ', $this->scopes),
152158
'response_type' => 'code',
153159
'client_id' => $this->getClientId(),
154160
'redirect_uri' => $this->callbackUrl,
@@ -275,7 +281,7 @@ public function refreshAccessToken($credentials)
275281
* Invalidate Refresh
276282
*
277283
* @param string $refreshToken
278-
* @return array
284+
* @return bool
279285
*/
280286
public function invalidateRefreshToken($refreshToken)
281287
{
@@ -302,6 +308,7 @@ public function invalidateRefreshToken($refreshToken)
302308
/**
303309
* Get access token from Code
304310
* @param array $credentials
311+
* @throws Exception
305312
* @return array
306313
*/
307314
public function getUserProfile($credentials)

0 commit comments

Comments
 (0)