|
5 | 5 | use Closure; |
6 | 6 | use Lcobucci\JWT\Parser; |
7 | 7 | use Cake\Chronos\Chronos; |
| 8 | +use Illuminate\Support\Str; |
8 | 9 | use Illuminate\Http\Request; |
9 | 10 | use Pterodactyl\Models\ApiKey; |
10 | 11 | use Illuminate\Auth\AuthManager; |
@@ -63,19 +64,24 @@ public function __construct(ApiKeyRepositoryInterface $repository, AuthManager $ |
63 | 64 | public function handle(Request $request, Closure $next, int $keyType) |
64 | 65 | { |
65 | 66 | if (is_null($request->bearerToken())) { |
66 | | - throw new HttpException(401, null, null, ['WWW-Authenticate' => 'Bearer']); |
| 67 | + if (! Str::startsWith($request->route()->getName(), ['api.client']) && ! $request->user()) { |
| 68 | + throw new HttpException(401, null, null, ['WWW-Authenticate' => 'Bearer']); |
| 69 | + } |
67 | 70 | } |
68 | 71 |
|
69 | | - $raw = $request->bearerToken(); |
| 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 | + } |
70 | 78 |
|
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 { |
| 79 | + if (! isset($model)) { |
| 80 | + $raw = $request->bearerToken(); |
75 | 81 | $model = $this->authenticateApiKey($raw, $keyType); |
| 82 | + $this->auth->guard()->loginUsingId($model->user_id); |
76 | 83 | } |
77 | 84 |
|
78 | | - $this->auth->guard()->loginUsingId($model->user_id); |
79 | 85 | $request->attributes->set('api_key', $model); |
80 | 86 |
|
81 | 87 | return $next($request); |
|
0 commit comments