Skip to content

Commit 7a1b8a8

Browse files
author
Lupacescu Eduard
authored
Laravel Airlock Support
Laravel Airlock Support
2 parents cd52c8c + 727b531 commit 7a1b8a8

File tree

6 files changed

+106
-15
lines changed

6 files changed

+106
-15
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
/*

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
convertNoticesToExceptions="true"
99
convertWarningsToExceptions="true"
1010
processIsolation="false"
11-
stopOnFailure="false">
11+
stopOnFailure="true">
1212
<testsuites>
1313
<testsuite name="Test Suite">
1414
<directory>tests</directory>

src/Contracts/Airlockable.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 $abilities
14+
* @return \Laravel\Airlock\NewAccessToken
15+
*/
16+
public function createToken(string $name, array $abilities = ['*']);
17+
18+
/**
19+
* @return Builder
20+
*/
21+
public function tokens();
22+
}
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: 24 additions & 7 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
}
@@ -88,13 +91,14 @@ public function login(array $credentials = [])
8891
}
8992

9093
/**
91-
* @param array $payload
94+
* @param array $payload
9295
* @return \Illuminate\Database\Eloquent\Builder|Model|mixed
9396
* @throws AuthenticatableUserException
9497
* @throws EntityNotFoundException
9598
* @throws PassportUserException
9699
* @throws ValidationException
97100
* @throws BindingResolutionException
101+
* @throws AirlockUserException
98102
*/
99103
public function register(array $payload)
100104
{
@@ -218,6 +222,7 @@ public function broker()
218222
*
219223
* @throws EntityNotFoundException
220224
* @throws PassportUserException
225+
* @throws AirlockUserException
221226
* @return Model
222227
*/
223228
public function userQuery()
@@ -239,12 +244,17 @@ public function userQuery()
239244
/**
240245
* @param $userInstance
241246
* @throws PassportUserException
247+
* @throws AirlockUserException
242248
*/
243249
public function validateUserModel($userInstance)
244250
{
245-
if (false === $userInstance instanceof Passportable) {
251+
if (config('restify.auth.provider') === 'passport' && false === $userInstance instanceof Passportable) {
246252
throw new PassportUserException(__("User is not implementing Binaryk\LaravelRestify\Contracts\Passportable contract. User can use 'Laravel\Passport\HasApiTokens' trait"));
247253
}
254+
255+
if (config('restify.auth.provider') === 'airlock' && false === $userInstance instanceof Airlockable) {
256+
throw new AirlockUserException(__("User is not implementing Binaryk\LaravelRestify\Contracts\Airlockable contract. User should use 'Laravel\Airlock\HasApiTokens' trait to provide"));
257+
}
248258
}
249259

250260
/**
@@ -302,9 +312,16 @@ public function logout()
302312
* @var User
303313
*/
304314
$user = Auth::user();
305-
if ($user instanceof Authenticatable && $user instanceof Passportable) {
306-
$user->tokens()->get()->each->revoke();
307-
event(new UserLogout($user));
315+
if ($user instanceof Authenticatable) {
316+
if ($user instanceof Passportable) {
317+
$user->tokens()->get()->each->revoke();
318+
event(new UserLogout($user));
319+
}
320+
321+
if ($user instanceof Airlockable) {
322+
$user->tokens->each->delete();
323+
event(new UserLogout($user));
324+
}
308325
} else {
309326
throw new AuthenticatableUserException(__('User is not authenticated.'));
310327
}

tests/AuthServiceLoginTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Binaryk\LaravelRestify\Contracts\Passportable;
66
use Binaryk\LaravelRestify\Events\UserLoggedIn;
77
use Binaryk\LaravelRestify\Events\UserLogout;
8+
use Binaryk\LaravelRestify\Exceptions\AirlockUserException;
89
use Binaryk\LaravelRestify\Exceptions\AuthenticatableUserException;
910
use Binaryk\LaravelRestify\Exceptions\CredentialsDoesntMatch;
1011
use Binaryk\LaravelRestify\Exceptions\PassportUserException;
@@ -75,6 +76,7 @@ public function test_user_did_not_verified_email()
7576

7677
public function test_login_user_did_not_user_passport_trait_or_not_implement_pasportable()
7778
{
79+
$this->app['config']->set('restify.auth.provider', 'passport');
7880
$this->expectException(PassportUserException::class);
7981
$userMustVerify = (new class extends User implements MustVerifyEmail {
8082
use \Illuminate\Auth\MustVerifyEmail;
@@ -94,6 +96,28 @@ public function test_login_user_did_not_user_passport_trait_or_not_implement_pas
9496
$this->authService->login();
9597
}
9698

99+
public function test_login_user_did_not_user_passport_trait_or_not_implement_airlockable()
100+
{
101+
$this->app['config']->set('restify.auth.provider', 'airlock');
102+
$this->expectException(AirlockUserException::class);
103+
$userMustVerify = (new class extends User implements MustVerifyEmail {
104+
use \Illuminate\Auth\MustVerifyEmail;
105+
});
106+
107+
$userMustVerify->fill([
108+
'email' => '[email protected]',
109+
'email_verified_at' => Carbon::now(),
110+
]);
111+
112+
Auth::shouldReceive('attempt')
113+
->andReturnTrue();
114+
115+
Auth::shouldReceive('user')
116+
->andReturn($userMustVerify);
117+
118+
$this->authService->login();
119+
}
120+
97121
public function test_login_with_success()
98122
{
99123
Event::fake([
@@ -127,6 +151,8 @@ public function test_login_with_success()
127151
public function test_logout_success()
128152
{
129153
Event::fake();
154+
$this->app['config']->set('restify.auth.provider', 'passport');
155+
130156
$user = (new class extends \Binaryk\LaravelRestify\Tests\Fixtures\User {
131157
public function tokens()
132158
{

0 commit comments

Comments
 (0)