flarum / flarum/issue-archive

Add logged in user to HTTP response header

Open
#3 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

## 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)
```
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;
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.