Skip to content

Commit cd59c14

Browse files
committed
Support for laravel airlock
1 parent 1519af1 commit cd59c14

File tree

4 files changed

+76
-13
lines changed

4 files changed

+76
-13
lines changed

config/config.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,29 @@
66
return [
77
'auth' => [
88
/*
9-
|--------------------------------------------------------------------------
10-
| Table containing authenticatable resource
11-
|--------------------------------------------------------------------------
12-
|
13-
| This configuration contain the name of the table used for the authentication.
14-
|
15-
*/
9+
|--------------------------------------------------------------------------
10+
| Table containing authenticatable resource
11+
|--------------------------------------------------------------------------
12+
|
13+
| This configuration contain the name of the table used for the authentication.
14+
|
15+
*/
1616

1717
'table' => 'users',
18+
19+
/*
20+
|--------------------------------------------------------------------------
21+
|
22+
|--------------------------------------------------------------------------
23+
|
24+
| Next you may configure the package you're using for the personal tokens generation,
25+
| this will be used for the verification of the authenticatable model and provide the
26+
| authorizable functionality
27+
|
28+
| Supported: "passport", "airlock"
29+
*/
30+
31+
'provider' => 'airlock'
1832
],
1933

2034
/*

src/Contracts/Airlockable.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Contracts;
4+
5+
use Illuminate\Database\Query\Builder;
6+
7+
interface Airlockable
8+
{
9+
/**
10+
* Create a new personal access token for the user.
11+
*
12+
* @param string $name
13+
* @param array $scopes
14+
*/
15+
public function createToken($name, array $scopes = []);
16+
17+
/**
18+
* @return Builder
19+
*/
20+
public function tokens();
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Exceptions;
4+
5+
use Exception;
6+
7+
/**
8+
* @author Eduard Lupacescu <[email protected]>
9+
*/
10+
class AirlockUserException extends Exception
11+
{
12+
}

src/Services/AuthService.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Binaryk\LaravelRestify\Services;
44

5+
use Binaryk\LaravelRestify\Contracts\Airlockable;
56
use Binaryk\LaravelRestify\Contracts\Passportable;
67
use Binaryk\LaravelRestify\Events\UserLoggedIn;
78
use Binaryk\LaravelRestify\Events\UserLogout;
9+
use Binaryk\LaravelRestify\Exceptions\AirlockUserException;
810
use Binaryk\LaravelRestify\Exceptions\AuthenticatableUserException;
911
use Binaryk\LaravelRestify\Exceptions\CredentialsDoesntMatch;
1012
use Binaryk\LaravelRestify\Exceptions\Eloquent\EntityNotFoundException;
@@ -59,6 +61,7 @@ class AuthService extends RestifyService
5961
* @throws CredentialsDoesntMatch
6062
* @throws UnverifiedUser
6163
* @throws PassportUserException
64+
* @throws AirlockUserException
6265
*/
6366
public function login(array $credentials = [])
6467
{
@@ -69,7 +72,7 @@ public function login(array $credentials = [])
6972
}
7073

7174
/**
72-
* @var Authenticatable|Passportable
75+
* @var Authenticatable|Passportable|Airlockable
7376
*/
7477
$user = Auth::user();
7578

@@ -79,7 +82,7 @@ public function login(array $credentials = [])
7982

8083
$this->validateUserModel($user);
8184

82-
if ($user instanceof Passportable) {
85+
if (method_exists($user, 'createToken')) {
8386
$token = $user->createToken('Login')->accessToken;
8487
event(new UserLoggedIn($user));
8588
}
@@ -218,6 +221,7 @@ public function broker()
218221
*
219222
* @throws EntityNotFoundException
220223
* @throws PassportUserException
224+
* @throws AirlockUserException
221225
* @return Model
222226
*/
223227
public function userQuery()
@@ -239,12 +243,17 @@ public function userQuery()
239243
/**
240244
* @param $userInstance
241245
* @throws PassportUserException
246+
* @throws AirlockUserException
242247
*/
243248
public function validateUserModel($userInstance)
244249
{
245-
if (false === $userInstance instanceof Passportable) {
250+
if (config('restify.auth.provider') === 'passport' && false === $userInstance instanceof Passportable) {
246251
throw new PassportUserException(__("User is not implementing Binaryk\LaravelRestify\Contracts\Passportable contract. User can use 'Laravel\Passport\HasApiTokens' trait"));
247252
}
253+
254+
if (config('restify.auth.provider') === 'airlock' && false === $userInstance instanceof Airlockable) {
255+
throw new AirlockUserException(__("User is not implementing Binaryk\LaravelRestify\Contracts\Airlockable contract. User should use 'Laravel\Airlock\HasApiTokens' trait to provide"));
256+
}
248257
}
249258

250259
/**
@@ -302,9 +311,16 @@ public function logout()
302311
* @var User
303312
*/
304313
$user = Auth::user();
305-
if ($user instanceof Authenticatable && $user instanceof Passportable) {
306-
$user->tokens()->get()->each->revoke();
307-
event(new UserLogout($user));
314+
if ($user instanceof Authenticatable) {
315+
if ($user instanceof Passportable) {
316+
$user->tokens->each->revoke();
317+
event(new UserLogout($user));
318+
}
319+
320+
if ($user instanceof Airlockable) {
321+
$user->tokens->each->delete();
322+
event(new UserLogout($user));
323+
}
308324
} else {
309325
throw new AuthenticatableUserException(__('User is not authenticated.'));
310326
}

0 commit comments

Comments
 (0)