Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit 49eb233

Browse files
committed
Add Client Resolver
1 parent 9914bec commit 49eb233

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Auth;
4+
5+
use App\Contracts\ClientResolverInterface;
6+
use App\Entities\Auth\Client;
7+
8+
/**
9+
* Class EloquentClientResolver
10+
*
11+
* @package App\Auth
12+
*/
13+
class EloquentClientResolver implements ClientResolverInterface
14+
{
15+
16+
/**
17+
* @var
18+
*/
19+
protected $model;
20+
21+
/**
22+
* EloquentClientResolver constructor.
23+
*
24+
* @param Client $model
25+
*/
26+
public function __construct(Client $model)
27+
{
28+
$this->model = $model;
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function resolveById($id)
35+
{
36+
return $this->model->where('id', $id)->firstOrFail();
37+
}
38+
39+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Contracts;
4+
5+
6+
/**
7+
* Interface ClientResolverInterface
8+
*
9+
* @package App\Contracts
10+
*/
11+
interface ClientResolverInterface
12+
{
13+
14+
/**
15+
* Resolve a oAuth 2 client by ID
16+
*
17+
* @param $id
18+
* @return mixed
19+
*/
20+
public function resolveById($id);
21+
22+
}

app/Entities/Auth/Client.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Entities\Auth;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* Class Client
9+
*
10+
* @package App\Entities\Auth
11+
*/
12+
class Client extends Model
13+
{
14+
15+
/**
16+
* @var string
17+
*/
18+
public $table = "oauth_clients";
19+
20+
}

app/Providers/AppServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace App\Providers;
44

55
use App\Auth\EloquentUserResolver;
6+
use App\Auth\EloquentClientResolver;
67
use Illuminate\Support\ServiceProvider;
78
use App\Contracts\UserResolverInterface;
89
use App\Exceptions\OAuthExceptionHandler;
10+
use App\Contracts\ClientResolverInterface;
911
use App\Exceptions\UnauthorizedExceptionHandler;
1012
use League\OAuth2\Server\Exception\OAuthException;
1113
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
@@ -26,6 +28,7 @@ public function boot()
2628
{
2729
$this->registerOAuthExceptionHandler();
2830
$this->app->bind(UserResolverInterface::class, EloquentUserResolver::class);
31+
$this->app->bind(ClientResolverInterface::class, EloquentClientResolver::class);
2932
}
3033

3134
/**

app/Providers/AuthServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Dingo\Api\Auth\Provider\OAuth2;
66
use App\Contracts\UserResolverInterface;
7+
use App\Contracts\ClientResolverInterface;
78
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
89
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
910

@@ -53,6 +54,8 @@ function ($id) {
5354

5455
$provider->setClientResolver(
5556
function ($id) {
57+
$resolver = app(ClientResolverInterface::class);
58+
return $resolver->resolveById($id);
5659
}
5760
);
5861

tests/Auth/OAuthServerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Tests\TestCase;
88
use App\Entities\Users\User;
99
use App\Contracts\UserResolverInterface;
10+
use App\Contracts\ClientResolverInterface;
1011
use Illuminate\Foundation\Testing\DatabaseMigrations;
1112

1213
/**
@@ -211,4 +212,20 @@ public function itResolvesUserFromId()
211212
$resolved = $resolver->resolveById($user->id);
212213
$this->assertEquals($user->id, $resolved->id);
213214
}
215+
216+
/**
217+
* @test
218+
*/
219+
public function itResolvesClientFromId()
220+
{
221+
$resolver = app(ClientResolverInterface::class);
222+
DB::table('oauth_clients')->insert([
223+
'id' => '12345',
224+
'secret' => '12345',
225+
'name' => 'Testing Env',
226+
'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
227+
'updated_at' => Carbon::now()->format('Y-m-d H:i:s')
228+
]);
229+
$this->assertEquals('12345', $resolver->resolveById('12345')->id);
230+
}
214231
}

0 commit comments

Comments
 (0)