Use client info from claims

This commit is contained in:
Cody Robibero 2021-11-05 10:40:45 -06:00
parent 3398f7f953
commit 17264a6020
3 changed files with 41 additions and 29 deletions

View file

@ -1,11 +1,12 @@
using System.Net.Mime; using System;
using System.Net.Mime;
using System.Threading.Tasks; using System.Threading.Tasks;
using Jellyfin.Api.Attributes; using Jellyfin.Api.Attributes;
using Jellyfin.Api.Constants; using Jellyfin.Api.Constants;
using Jellyfin.Api.Helpers;
using Jellyfin.Api.Models.ClientLogDtos; using Jellyfin.Api.Models.ClientLogDtos;
using MediaBrowser.Controller.ClientEvent; using MediaBrowser.Controller.ClientEvent;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.ClientLog; using MediaBrowser.Model.ClientLog;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -21,22 +22,18 @@ namespace Jellyfin.Api.Controllers
{ {
private const int MaxDocumentSize = 1_000_000; private const int MaxDocumentSize = 1_000_000;
private readonly IClientEventLogger _clientEventLogger; private readonly IClientEventLogger _clientEventLogger;
private readonly IAuthorizationContext _authorizationContext;
private readonly IServerConfigurationManager _serverConfigurationManager; private readonly IServerConfigurationManager _serverConfigurationManager;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ClientLogController"/> class. /// Initializes a new instance of the <see cref="ClientLogController"/> class.
/// </summary> /// </summary>
/// <param name="clientEventLogger">Instance of the <see cref="IClientEventLogger"/> interface.</param> /// <param name="clientEventLogger">Instance of the <see cref="IClientEventLogger"/> interface.</param>
/// <param name="authorizationContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param> /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
public ClientLogController( public ClientLogController(
IClientEventLogger clientEventLogger, IClientEventLogger clientEventLogger,
IAuthorizationContext authorizationContext,
IServerConfigurationManager serverConfigurationManager) IServerConfigurationManager serverConfigurationManager)
{ {
_clientEventLogger = clientEventLogger; _clientEventLogger = clientEventLogger;
_authorizationContext = authorizationContext;
_serverConfigurationManager = serverConfigurationManager; _serverConfigurationManager = serverConfigurationManager;
} }
@ -50,17 +47,15 @@ namespace Jellyfin.Api.Controllers
[HttpPost] [HttpPost]
[ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<ActionResult> LogEvent([FromBody] ClientLogEventDto clientLogEventDto) public ActionResult LogEvent([FromBody] ClientLogEventDto clientLogEventDto)
{ {
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload) if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
{ {
return Forbid(); return Forbid();
} }
var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request) var (clientName, clientVersion, userId, deviceId) = GetRequestInformation();
.ConfigureAwait(false); Log(clientLogEventDto, userId, clientName, clientVersion, deviceId);
Log(clientLogEventDto, authorizationInfo);
return NoContent(); return NoContent();
} }
@ -74,19 +69,17 @@ namespace Jellyfin.Api.Controllers
[HttpPost("Bulk")] [HttpPost("Bulk")]
[ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<ActionResult> LogEvents([FromBody] ClientLogEventDto[] clientLogEventDtos) public ActionResult LogEvents([FromBody] ClientLogEventDto[] clientLogEventDtos)
{ {
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload) if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
{ {
return Forbid(); return Forbid();
} }
var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request) var (clientName, clientVersion, userId, deviceId) = GetRequestInformation();
.ConfigureAwait(false);
foreach (var dto in clientLogEventDtos) foreach (var dto in clientLogEventDtos)
{ {
Log(dto, authorizationInfo); Log(dto, userId, clientName, clientVersion, deviceId);
} }
return NoContent(); return NoContent();
@ -118,24 +111,39 @@ namespace Jellyfin.Api.Controllers
return StatusCode(StatusCodes.Status413PayloadTooLarge, $"Payload must be less than {MaxDocumentSize:N0} bytes"); return StatusCode(StatusCodes.Status413PayloadTooLarge, $"Payload must be less than {MaxDocumentSize:N0} bytes");
} }
var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request) var (clientName, clientVersion, _, _) = GetRequestInformation();
.ConfigureAwait(false); var fileName = await _clientEventLogger.WriteDocumentAsync(clientName, clientVersion, Request.Body)
var fileName = await _clientEventLogger.WriteDocumentAsync(authorizationInfo, Request.Body)
.ConfigureAwait(false); .ConfigureAwait(false);
return Ok(new ClientLogDocumentResponseDto(fileName)); return Ok(new ClientLogDocumentResponseDto(fileName));
} }
private void Log(ClientLogEventDto dto, AuthorizationInfo authorizationInfo) private void Log(
ClientLogEventDto dto,
Guid userId,
string clientName,
string clientVersion,
string deviceId)
{ {
_clientEventLogger.Log(new ClientLogEvent( _clientEventLogger.Log(new ClientLogEvent(
dto.Timestamp, dto.Timestamp,
dto.Level, dto.Level,
authorizationInfo.UserId, userId,
authorizationInfo.Client, clientName,
authorizationInfo.Version, clientVersion,
authorizationInfo.DeviceId, deviceId,
dto.Message)); dto.Message));
} }
private (string ClientName, string ClientVersion, Guid UserId, string DeviceId) GetRequestInformation()
{
var clientName = ClaimHelpers.GetClient(HttpContext.User) ?? "unknown-client";
var clientVersion = ClaimHelpers.GetIsApiKey(HttpContext.User)
? "apikey"
: ClaimHelpers.GetVersion(HttpContext.User) ?? "unknown-version";
var userId = ClaimHelpers.GetUserId(HttpContext.User) ?? Guid.Empty;
var deviceId = ClaimHelpers.GetDeviceId(HttpContext.User) ?? "unknown-device-id";
return (clientName, clientVersion, userId, deviceId);
}
} }
} }

View file

@ -44,9 +44,9 @@ namespace MediaBrowser.Controller.ClientEvent
} }
/// <inheritdoc /> /// <inheritdoc />
public async Task<string> WriteDocumentAsync(AuthorizationInfo authorizationInfo, Stream fileContents) public async Task<string> WriteDocumentAsync(string clientName, string clientVersion, Stream fileContents)
{ {
var fileName = $"upload_{authorizationInfo.Client}_{(authorizationInfo.IsApiKey ? "apikey" : authorizationInfo.Version)}_{DateTime.UtcNow:yyyyMMddHHmmss}.log"; var fileName = $"upload_{clientName}_{clientVersion}_{DateTime.UtcNow:yyyyMMddHHmmss}.log";
var logFilePath = Path.Combine(_applicationPaths.LogDirectoryPath, fileName); var logFilePath = Path.Combine(_applicationPaths.LogDirectoryPath, fileName);
await using var fileStream = new FileStream(logFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None); await using var fileStream = new FileStream(logFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
await fileContents.CopyToAsync(fileStream).ConfigureAwait(false); await fileContents.CopyToAsync(fileStream).ConfigureAwait(false);

View file

@ -19,9 +19,13 @@ namespace MediaBrowser.Controller.ClientEvent
/// <summary> /// <summary>
/// Writes a file to the log directory. /// Writes a file to the log directory.
/// </summary> /// </summary>
/// <param name="authorizationInfo">The current authorization info.</param> /// <param name="clientName">The client name writing the document.</param>
/// <param name="clientVersion">The client version writing the document.</param>
/// <param name="fileContents">The file contents to write.</param> /// <param name="fileContents">The file contents to write.</param>
/// <returns>The created file name.</returns> /// <returns>The created file name.</returns>
Task<string> WriteDocumentAsync(AuthorizationInfo authorizationInfo, Stream fileContents); Task<string> WriteDocumentAsync(
string clientName,
string clientVersion,
Stream fileContents);
} }
} }