11<?php
22
3- namespace Tests \Unit \Http \Middleware \Api \ Application ;
3+ namespace Tests \Unit \Http \Middleware \API ;
44
55use Mockery as m ;
66use Cake \Chronos \Chronos ;
77use Pterodactyl \Models \ApiKey ;
88use Illuminate \Auth \AuthManager ;
99use Illuminate \Contracts \Encryption \Encrypter ;
1010use Tests \Unit \Http \Middleware \MiddlewareTestCase ;
11+ use Pterodactyl \Http \Middleware \Api \AuthenticateKey ;
1112use Symfony \Component \HttpKernel \Exception \HttpException ;
1213use Pterodactyl \Exceptions \Repository \RecordNotFoundException ;
1314use Pterodactyl \Contracts \Repository \ApiKeyRepositoryInterface ;
14- use Pterodactyl \Http \Middleware \Api \Application \AuthenticateKey ;
1515
1616class AuthenticateKeyTest extends MiddlewareTestCase
1717{
@@ -51,7 +51,7 @@ public function testMissingBearerTokenThrowsException()
5151 $ this ->request ->shouldReceive ('bearerToken ' )->withNoArgs ()->once ()->andReturnNull ();
5252
5353 try {
54- $ this ->getMiddleware ()->handle ($ this ->request , $ this ->getClosureAssertions ());
54+ $ this ->getMiddleware ()->handle ($ this ->request , $ this ->getClosureAssertions (), ApiKey:: TYPE_APPLICATION );
5555 } catch (HttpException $ exception ) {
5656 $ this ->assertEquals (401 , $ exception ->getStatusCode ());
5757 $ this ->assertEquals (['WWW-Authenticate ' => 'Bearer ' ], $ exception ->getHeaders ());
@@ -68,7 +68,7 @@ public function testInvalidIdentifier()
6868 $ this ->request ->shouldReceive ('bearerToken ' )->withNoArgs ()->twice ()->andReturn ('abcd1234 ' );
6969 $ this ->repository ->shouldReceive ('findFirstWhere ' )->andThrow (new RecordNotFoundException );
7070
71- $ this ->getMiddleware ()->handle ($ this ->request , $ this ->getClosureAssertions ());
71+ $ this ->getMiddleware ()->handle ($ this ->request , $ this ->getClosureAssertions (), ApiKey:: TYPE_APPLICATION );
7272 }
7373
7474 /**
@@ -90,7 +90,30 @@ public function testValidToken()
9090 'last_used_at ' => Chronos::now (),
9191 ])->once ()->andReturnNull ();
9292
93- $ this ->getMiddleware ()->handle ($ this ->request , $ this ->getClosureAssertions ());
93+ $ this ->getMiddleware ()->handle ($ this ->request , $ this ->getClosureAssertions (), ApiKey::TYPE_APPLICATION );
94+ $ this ->assertEquals ($ model , $ this ->request ->attributes ->get ('api_key ' ));
95+ }
96+
97+ /**
98+ * Test that a valid token can continue past the middleware when set as a user token.
99+ */
100+ public function testValidTokenWithUserKey ()
101+ {
102+ $ model = factory (ApiKey::class)->make ();
103+
104+ $ this ->request ->shouldReceive ('bearerToken ' )->withNoArgs ()->twice ()->andReturn ($ model ->identifier . 'decrypted ' );
105+ $ this ->repository ->shouldReceive ('findFirstWhere ' )->with ([
106+ ['identifier ' , '= ' , $ model ->identifier ],
107+ ['key_type ' , '= ' , ApiKey::TYPE_ACCOUNT ],
108+ ])->once ()->andReturn ($ model );
109+ $ this ->encrypter ->shouldReceive ('decrypt ' )->with ($ model ->token )->once ()->andReturn ('decrypted ' );
110+ $ this ->auth ->shouldReceive ('guard->loginUsingId ' )->with ($ model ->user_id )->once ()->andReturnNull ();
111+
112+ $ this ->repository ->shouldReceive ('withoutFreshModel->update ' )->with ($ model ->id , [
113+ 'last_used_at ' => Chronos::now (),
114+ ])->once ()->andReturnNull ();
115+
116+ $ this ->getMiddleware ()->handle ($ this ->request , $ this ->getClosureAssertions (), ApiKey::TYPE_ACCOUNT );
94117 $ this ->assertEquals ($ model , $ this ->request ->attributes ->get ('api_key ' ));
95118 }
96119
@@ -111,13 +134,13 @@ public function testInvalidTokenForIdentifier()
111134 ])->once ()->andReturn ($ model );
112135 $ this ->encrypter ->shouldReceive ('decrypt ' )->with ($ model ->token )->once ()->andReturn ('decrypted ' );
113136
114- $ this ->getMiddleware ()->handle ($ this ->request , $ this ->getClosureAssertions ());
137+ $ this ->getMiddleware ()->handle ($ this ->request , $ this ->getClosureAssertions (), ApiKey:: TYPE_APPLICATION );
115138 }
116139
117140 /**
118141 * Return an instance of the middleware with mocked dependencies for testing.
119142 *
120- * @return \Pterodactyl\Http\Middleware\Api\Application\ AuthenticateKey
143+ * @return \Pterodactyl\Http\Middleware\Api\AuthenticateKey
121144 */
122145 private function getMiddleware (): AuthenticateKey
123146 {
0 commit comments