This repository was archived by the owner on Feb 18, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 6 files changed +104
-0
lines changed Expand file tree Collapse file tree 6 files changed +104
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 33namespace App \Providers ;
44
55use App \Auth \EloquentUserResolver ;
6+ use App \Auth \EloquentClientResolver ;
67use Illuminate \Support \ServiceProvider ;
78use App \Contracts \UserResolverInterface ;
89use App \Exceptions \OAuthExceptionHandler ;
10+ use App \Contracts \ClientResolverInterface ;
911use App \Exceptions \UnauthorizedExceptionHandler ;
1012use League \OAuth2 \Server \Exception \OAuthException ;
1113use 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 /**
Original file line number Diff line number Diff line change 44
55use Dingo \Api \Auth \Provider \OAuth2 ;
66use App \Contracts \UserResolverInterface ;
7+ use App \Contracts \ClientResolverInterface ;
78use Illuminate \Contracts \Auth \Access \Gate as GateContract ;
89use 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
Original file line number Diff line number Diff line change 77use App \Tests \TestCase ;
88use App \Entities \Users \User ;
99use App \Contracts \UserResolverInterface ;
10+ use App \Contracts \ClientResolverInterface ;
1011use 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}
You can’t perform that action at this time.
0 commit comments