33namespace Pterodactyl \Http \Middleware \Api ;
44
55use Closure ;
6- use Lcobucci \JWT \Parser ;
76use Cake \Chronos \Chronos ;
87use Illuminate \Http \Request ;
8+ use Pterodactyl \Models \User ;
99use Pterodactyl \Models \ApiKey ;
1010use Illuminate \Auth \AuthManager ;
1111use Illuminate \Contracts \Encryption \Encrypter ;
@@ -62,15 +62,19 @@ public function __construct(ApiKeyRepositoryInterface $repository, AuthManager $
6262 */
6363 public function handle (Request $ request , Closure $ next , int $ keyType )
6464 {
65- if (is_null ($ request ->bearerToken ())) {
65+ if (is_null ($ request ->bearerToken ()) && is_null ( $ request -> user ()) ) {
6666 throw new HttpException (401 , null , null , ['WWW-Authenticate ' => 'Bearer ' ]);
6767 }
6868
6969 $ raw = $ request ->bearerToken ();
7070
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 );
71+ // This is a request coming through using cookies, we have an authenticated user not using
72+ // an API key. Make some fake API key models and continue on through the process.
73+ if (empty ($ raw ) && $ request ->user () instanceof User) {
74+ $ model = new ApiKey ([
75+ 'user_id ' => $ request ->user ()->id ,
76+ 'key_type ' => ApiKey::TYPE_ACCOUNT ,
77+ ]);
7478 } else {
7579 $ model = $ this ->authenticateApiKey ($ raw , $ keyType );
7680 }
@@ -81,42 +85,6 @@ public function handle(Request $request, Closure $next, int $keyType)
8185 return $ next ($ request );
8286 }
8387
84- /**
85- * Authenticate an API request using a JWT rather than an API key.
86- *
87- * @param string $token
88- * @return \Pterodactyl\Models\ApiKey
89- */
90- protected function authenticateJWT (string $ token ): ApiKey
91- {
92- $ token = (new Parser )->parse ($ token );
93-
94- // If the key cannot be verified throw an exception to indicate that a bad
95- // authorization header was provided.
96- if (! $ token ->verify ($ this ->getJWTSigner (), $ this ->getJWTSigningKey ())) {
97- throw new HttpException (401 , null , null , ['WWW-Authenticate ' => 'Bearer ' ]);
98- }
99-
100- // Run through the token validation and throw an exception if the token is not valid.
101- //
102- // The issued_at time is used for verification in order to allow rapid changing of session
103- // length on the Panel without having to wait on existing tokens to first expire.
104- $ now = Chronos::now ('utc ' );
105- if (
106- Chronos::createFromTimestampUTC ($ token ->getClaim ('nbf ' ))->gt ($ now )
107- || $ token ->getClaim ('iss ' ) !== 'Pterodactyl Panel '
108- || $ token ->getClaim ('aud ' ) !== config ('app.url ' )
109- || Chronos::createFromTimestampUTC ($ token ->getClaim ('iat ' ))->addMinutes (config ('jwt.lifetime ' ))->lte ($ now )
110- ) {
111- throw new AccessDeniedHttpException ('The authentication parameters provided are not valid for accessing this resource. ' );
112- }
113-
114- return (new ApiKey )->forceFill ([
115- 'user_id ' => object_get ($ token ->getClaim ('user ' ), 'id ' , 0 ),
116- 'key_type ' => ApiKey::TYPE_ACCOUNT ,
117- ]);
118- }
119-
12088 /**
12189 * Authenticate an API key.
12290 *
0 commit comments