-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add header-based authentication support #5271
Open
banddude
wants to merge
2
commits into
pterodactyl:1.0-develop
Choose a base branch
from
banddude:feature/header-authentication
base: 1.0-develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Pterodactyl\Http\Middleware; | ||
|
||
use Closure; | ||
use Ramsey\Uuid\Uuid; | ||
use Illuminate\Http\Request; | ||
use Pterodactyl\Models\User; | ||
use Illuminate\Support\Facades\Auth; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class HeaderAuthentication | ||
{ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
if (!config('auth.header.enabled', false)) { | ||
return $next($request); | ||
} | ||
|
||
$usernameHeader = config('auth.header.username_header', 'X-Auth-Username'); | ||
$emailHeader = config('auth.header.email_header', 'X-Auth-Email'); | ||
|
||
$username = $request->header($usernameHeader); | ||
$email = $request->header($emailHeader); | ||
|
||
if (!$username || !$email) { | ||
return $next($request); | ||
} | ||
|
||
$user = User::where('email', $email)->first(); | ||
|
||
if (!$user && config('auth.header.auto_create', false)) { | ||
$user = new User(); | ||
$user->uuid = Uuid::uuid4()->toString(); | ||
$user->username = $username; | ||
$user->email = $email; | ||
$user->name_first = $username; | ||
$user->name_last = $username; | ||
$user->password = bcrypt(Uuid::uuid4()->toString()); | ||
$user->language = config('app.locale', 'en'); | ||
$user->root_admin = false; | ||
$user->use_totp = false; | ||
$user->totp_secret = null; | ||
$user->external_id = ''; | ||
$user->gravatar = true; | ||
$user->totp_authenticated_at = null; | ||
$user->save(); | ||
} | ||
|
||
if ($user) { | ||
Auth::login($user); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
tests/Unit/Http/Middleware/HeaderAuthenticationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace Pterodactyl\Tests\Unit\Http\Middleware; | ||
|
||
use Ramsey\Uuid\Uuid; | ||
use Illuminate\Http\Request; | ||
use Pterodactyl\Models\User; | ||
use Illuminate\Support\Facades\Auth; | ||
use Pterodactyl\Tests\TestCase; | ||
use Pterodactyl\Http\Middleware\HeaderAuthentication; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
||
class HeaderAuthenticationTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
config()->set('auth.header.enabled', true); | ||
} | ||
|
||
public function test_middleware_does_nothing_when_disabled() | ||
{ | ||
config()->set('auth.header.enabled', false); | ||
|
||
$middleware = new HeaderAuthentication(); | ||
$request = new Request(); | ||
|
||
$response = $middleware->handle($request, function ($req) { | ||
return response('OK'); | ||
}); | ||
|
||
$this->assertEquals('OK', $response->getContent()); | ||
$this->assertFalse(Auth::check()); | ||
} | ||
|
||
public function test_middleware_authenticates_existing_user() | ||
{ | ||
$user = User::factory()->create([ | ||
'username' => 'testuser', | ||
'email' => '[email protected]', | ||
'name_first' => 'Test', | ||
'name_last' => 'User', | ||
'external_id' => '', | ||
]); | ||
|
||
$middleware = new HeaderAuthentication(); | ||
$request = new Request(); | ||
$request->headers->set('X-Auth-Username', 'testuser'); | ||
$request->headers->set('X-Auth-Email', '[email protected]'); | ||
|
||
$response = $middleware->handle($request, function ($req) { | ||
return response('OK'); | ||
}); | ||
|
||
$this->assertEquals('OK', $response->getContent()); | ||
$this->assertTrue(Auth::check()); | ||
$this->assertEquals($user->id, Auth::id()); | ||
} | ||
|
||
public function test_middleware_creates_new_user_when_enabled() | ||
{ | ||
config()->set('auth.header.auto_create', true); | ||
|
||
$middleware = new HeaderAuthentication(); | ||
$request = new Request(); | ||
$request->headers->set('X-Auth-Username', 'testuser'); | ||
$request->headers->set('X-Auth-Email', '[email protected]'); | ||
|
||
$response = $middleware->handle($request, function ($req) { | ||
return response('OK'); | ||
}); | ||
|
||
$this->assertEquals('OK', $response->getContent()); | ||
$this->assertTrue(Auth::check()); | ||
$user = Auth::user(); | ||
$this->assertEquals('testuser', $user->username); | ||
$this->assertEquals('[email protected]', $user->email); | ||
$this->assertEquals('', $user->external_id); | ||
$this->assertNotNull($user->uuid); | ||
} | ||
|
||
public function test_middleware_does_not_create_user_when_auto_create_disabled() | ||
{ | ||
config()->set('auth.header.auto_create', false); | ||
|
||
$middleware = new HeaderAuthentication(); | ||
$request = new Request(); | ||
$request->headers->set('X-Auth-Username', 'testuser'); | ||
$request->headers->set('X-Auth-Email', '[email protected]'); | ||
|
||
$response = $middleware->handle($request, function ($req) { | ||
return response('OK'); | ||
}); | ||
|
||
$this->assertEquals('OK', $response->getContent()); | ||
$this->assertFalse(Auth::check()); | ||
} | ||
|
||
public function test_middleware_uses_custom_header_names() | ||
{ | ||
config()->set('auth.header.username_header', 'HTTP_X_USERNAME'); | ||
config()->set('auth.header.email_header', 'HTTP_X_EMAIL'); | ||
|
||
$user = User::factory()->create([ | ||
'username' => 'testuser', | ||
'email' => '[email protected]', | ||
'name_first' => 'Test', | ||
'name_last' => 'User', | ||
'external_id' => '', | ||
]); | ||
|
||
$middleware = new HeaderAuthentication(); | ||
$request = new Request(); | ||
$request->headers->set('HTTP_X_USERNAME', 'testuser'); | ||
$request->headers->set('HTTP_X_EMAIL', '[email protected]'); | ||
|
||
$response = $middleware->handle($request, function ($req) { | ||
return response('OK'); | ||
}); | ||
|
||
$this->assertEquals('OK', $response->getContent()); | ||
$this->assertTrue(Auth::check()); | ||
$this->assertEquals($user->id, Auth::id()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious to the reasoning on setting
hash
tofalse
here? I don't think that is part of this feature, and this seems to be the default value according to older Laravel docs:https://laravel.com/docs/5.8/api-authentication#hashing-tokens
Maybe good to keep removed?