55use Closure ;
66use Lcobucci \JWT \Parser ;
77use Cake \Chronos \Chronos ;
8- use Illuminate \Support \Str ;
98use Illuminate \Http \Request ;
109use Pterodactyl \Models \ApiKey ;
1110use Illuminate \Auth \AuthManager ;
@@ -64,24 +63,19 @@ public function __construct(ApiKeyRepositoryInterface $repository, AuthManager $
6463 public function handle (Request $ request , Closure $ next , int $ keyType )
6564 {
6665 if (is_null ($ request ->bearerToken ())) {
67- if (! Str::startsWith ($ request ->route ()->getName (), ['api.client ' ]) && ! $ request ->user ()) {
68- throw new HttpException (401 , null , null , ['WWW-Authenticate ' => 'Bearer ' ]);
69- }
66+ throw new HttpException (401 , null , null , ['WWW-Authenticate ' => 'Bearer ' ]);
7067 }
7168
72- if (is_null ($ request ->bearerToken ())) {
73- $ model = (new ApiKey )->forceFill ([
74- 'user_id ' => $ request ->user ()->id ,
75- 'key_type ' => ApiKey::TYPE_ACCOUNT ,
76- ]);
77- }
69+ $ raw = $ request ->bearerToken ();
7870
79- if (! isset ($ model )) {
80- $ raw = $ request ->bearerToken ();
71+ // This is an internal JWT, treat it differently to get the correct user before passing it along.
72+ if (strlen ($ raw ) > ApiKey::IDENTIFIER_LENGTH + ApiKey::KEY_LENGTH ) {
73+ $ model = $ this ->authenticateJWT ($ raw );
74+ } else {
8175 $ model = $ this ->authenticateApiKey ($ raw , $ keyType );
82- $ this ->auth ->guard ()->loginUsingId ($ model ->user_id );
8376 }
8477
78+ $ this ->auth ->guard ()->loginUsingId ($ model ->user_id );
8579 $ request ->attributes ->set ('api_key ' , $ model );
8680
8781 return $ next ($ request );
@@ -103,6 +97,16 @@ protected function authenticateJWT(string $token): ApiKey
10397 throw new HttpException (401 , null , null , ['WWW-Authenticate ' => 'Bearer ' ]);
10498 }
10599
100+ // Run through the token validation and throw an exception if the token is not valid.
101+ if (
102+ $ token ->getClaim ('nbf ' ) > Chronos::now ()->getTimestamp ()
103+ || $ token ->getClaim ('iss ' ) !== 'Pterodactyl Panel '
104+ || $ token ->getClaim ('aud ' ) !== config ('app.url ' )
105+ || $ token ->getClaim ('exp ' ) <= Chronos::now ()->getTimestamp ()
106+ ) {
107+ throw new AccessDeniedHttpException ;
108+ }
109+
106110 return (new ApiKey )->forceFill ([
107111 'user_id ' => object_get ($ token ->getClaim ('user ' ), 'id ' , 0 ),
108112 'key_type ' => ApiKey::TYPE_ACCOUNT ,
0 commit comments