44
55use Closure ;
66use Illuminate \Http \Request ;
7+ use Illuminate \Auth \AuthManager ;
8+ use Symfony \Component \HttpKernel \Exception \HttpException ;
9+ use Pterodactyl \Exceptions \Repository \RecordNotFoundException ;
10+ use Illuminate \Contracts \Config \Repository as ConfigRepository ;
711use Pterodactyl \Contracts \Repository \ApiKeyRepositoryInterface ;
12+ use Symfony \Component \HttpKernel \Exception \AccessDeniedHttpException ;
813
914class AuthenticateKey
1015{
16+ /**
17+ * @var \Illuminate\Auth\AuthManager
18+ */
19+ private $ auth ;
20+
21+ /**
22+ * @var \Illuminate\Contracts\Config\Repository
23+ */
24+ private $ config ;
25+
1126 /**
1227 * @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
1328 */
@@ -17,9 +32,16 @@ class AuthenticateKey
1732 * AuthenticateKey constructor.
1833 *
1934 * @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
35+ * @param \Illuminate\Auth\AuthManager $auth
36+ * @param \Illuminate\Contracts\Config\Repository $config
2037 */
21- public function __construct (ApiKeyRepositoryInterface $ repository )
22- {
38+ public function __construct (
39+ ApiKeyRepositoryInterface $ repository ,
40+ AuthManager $ auth ,
41+ ConfigRepository $ config
42+ ) {
43+ $ this ->auth = $ auth ;
44+ $ this ->config = $ config ;
2345 $ this ->repository = $ repository ;
2446 }
2547
@@ -30,11 +52,23 @@ public function __construct(ApiKeyRepositoryInterface $repository)
3052 *
3153 * @param \Illuminate\Http\Request $request
3254 * @param \Closure $next
55+ * @return mixed
3356 */
3457 public function handle (Request $ request , Closure $ next )
3558 {
36- $ this ->repository ->findFirstWhere ([
37- '' ,
38- ]);
59+ if (is_null ($ request ->bearerToken ())) {
60+ throw new HttpException (401 , null , null , ['WWW-Authenticate ' => 'Bearer ' ]);
61+ }
62+
63+ try {
64+ $ model = $ this ->repository ->findFirstWhere ([['token ' , '= ' , $ request ->bearerToken ()]]);
65+ } catch (RecordNotFoundException $ exception ) {
66+ throw new AccessDeniedHttpException ;
67+ }
68+
69+ $ this ->auth ->guard ()->loginUsingId ($ model ->user_id );
70+ $ request ->attributes ->set ('api_key ' , $ model );
71+
72+ return $ next ($ request );
3973 }
4074}
0 commit comments