Skip to content
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 logged in user to HTTP response header #3

Open
fatihusta opened this issue Feb 25, 2022 · 1 comment
Open

Add logged in user to HTTP response header #3

fatihusta opened this issue Feb 25, 2022 · 1 comment

Comments

@fatihusta
Copy link

Feature Request

Requierment
I want to save username to access.log who logged in from the IP address for security purpose.

Solution
I saw a solution on the link.
https://stackoverflow.com/questions/39475430/how-to-add-laravel-username-to-the-access-log-of-nginx

I staticly tested. And It worked but just at root page(/). I'm not a PHP developer. So please support this feature in flarum.

Sample nginx log

{
  "time_local": "2022-02-25T15:37:45+03:00",
  "client_ip": "x.x.x.x",
  "remote_addr": "x.x.x.x",
  "remote_user": "Flarum",
  "request": "GET / HTTP/1.1",
  "status": "200",
  "body_bytes_sent": "17571",
  "request_time": "0.221",
  "http_referrer": "",
  "http_user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36",
  "request_id": "6a2cbee8fb90adb02b4218cb4a485ef2"
}

My static test
src/Http/Middleware/UsernameToHeader.php (copied from FlarumPromotionHeader.php)

<?php

/*
 * This file is part of Flarum.
 *
 * For detailed copyright and license information, please view the
 * LICENSE file that was distributed with this source code.
 */

namespace Flarum\Http\Middleware;

use Flarum\Foundation\Config;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface;

class UsernameToHeader implements Middleware
{
    protected $enabled = true;

    public function __construct(Config $config)
    {
        $this->enabled = Arr::get($config, 'headers.usernameToHeader') ?? true;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response = $handler->handle($request);

        if ($this->enabled) {
            $response = $response->withAddedHeader('X-Username', 'Flarum');
        }

        return $response;
    }
}

src/Forum/ForumServiceProvider.php

...
HttpMiddleware\FlarumPromotionHeader::class,
HttpMiddleware\UsernameToHeader::class,
...

config.php

....
  'headers' =>
  array (
    'poweredByHeader' => true,
    'usernameToHeader' => true,
    'referrerPolicy' => 'same-origin',
  ),

/etc/nginx/conf.d/logging.conf

log_format json_combined escape=json
    '{'
      '"time_local":"$time_iso8601",'
      '"client_ip":"$http_x_forwarded_for",'
      '"remote_addr":"$remote_addr",'
      '"remote_user":"$sent_http_x_username",'
      '"request":"$request",'
      '"status":"$status",'
      '"body_bytes_sent":"$body_bytes_sent",'
      '"request_time":"$request_time",'
      '"http_referrer":"$http_referer",'
      '"http_user_agent":"$http_user_agent",'
      '"request_id":"$request_id"'
    '}';

access_log /var/log/nginx/access.log json_combined;
@tankerkiller125
Copy link

tankerkiller125 commented Feb 25, 2022

So this can 100% be an extension, you would just need to have your directories setup something like this:

- src/
-- Middleware/UsernameToHeader::class
-- extend.php
composer.json

From there you need to change the namespace of your Middleware to be your own and setup the extend.php and use the Middleware extender to add your middleware in the extend.php file.

For an example of adding and using middleware in an extension you can take a look at https://github.com/flarum-tank/middleware/blob/master/extend.php

In your case you need to use both: (new Extend\Middleware('forum'))->add(<classhere>) and (new Extend\Middleware('api'))->add(<classhere>)

You can read more documentation on this via our docs: https://docs.flarum.org/extend/middleware

@askvortsov1 askvortsov1 transferred this issue from flarum/framework Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants