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

Show end-user external IP address to Emby when logging in as an Emby user #4949

Merged
merged 1 commit into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Ombi.Api.Emby/EmbyApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<PublicInfo> GetPublicInformation(string baseUrl)
return obj;
}

public async Task<EmbyUser> LogIn(string username, string password, string apiKey, string baseUri)
public async Task<EmbyUser> LogIn(string username, string password, string apiKey, string baseUri, string clientIpAddress)
{
var request = new Request("emby/users/authenticatebyname", baseUri, HttpMethod.Post);
var body = new
Expand All @@ -71,6 +71,11 @@ public async Task<EmbyUser> LogIn(string username, string password, string apiKe
$"MediaBrowser Client=\"Ombi\", Device=\"Ombi\", DeviceId=\"v3\", Version=\"v3\"");
AddHeaders(request, apiKey);

if (!string.IsNullOrEmpty(clientIpAddress))
{
request.AddHeader("X-Forwarded-For", clientIpAddress);
}

var obj = await Api.Request<EmbyUser>(request);
return obj;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Ombi.Api.Emby/IBaseEmbyApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface IBaseEmbyApi
{
Task<EmbySystemInfo> GetSystemInformation(string apiKey, string baseUrl);
Task<List<EmbyUser>> GetUsers(string baseUri, string apiKey);
Task<EmbyUser> LogIn(string username, string password, string apiKey, string baseUri);
Task<EmbyUser> LogIn(string username, string password, string apiKey, string baseUri, string clientIpAddress);

Task<EmbyItemContainer<EmbyMovie>> GetAllMovies(string apiKey, string parentIdFilder, int startIndex, int count, string userId,
string baseUri);
Expand Down
8 changes: 5 additions & 3 deletions src/Ombi.Core/Authentication/OmbiUserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public OmbiUserManager(IUserStore<OmbiUser> store, IOptions<IdentityOptions> opt
private readonly ISettingsService<EmbySettings> _embySettings;
private readonly ISettingsService<JellyfinSettings> _jellyfinSettings;
private readonly ISettingsService<AuthenticationSettings> _authSettings;
private string _clientIpAddress;
public string ClientIpAddress { get => _clientIpAddress; set => _clientIpAddress = value; }

public override async Task<bool> CheckPasswordAsync(OmbiUser user, string password)
{
Expand All @@ -88,7 +90,7 @@ public override async Task<bool> CheckPasswordAsync(OmbiUser user, string passwo
}
if (user.UserType == UserType.EmbyUser || user.UserType == UserType.EmbyConnectUser)
{
return await CheckEmbyPasswordAsync(user, password);
return await CheckEmbyPasswordAsync(user, password, ClientIpAddress);
}
if (user.UserType == UserType.JellyfinUser)
{
Expand Down Expand Up @@ -168,7 +170,7 @@ private async Task<bool> CheckPlexPasswordAsync(OmbiUser user, string password)
/// <param name="user"></param>
/// <param name="password"></param>
/// <returns></returns>
private async Task<bool> CheckEmbyPasswordAsync(OmbiUser user, string password)
private async Task<bool> CheckEmbyPasswordAsync(OmbiUser user, string password, string clientIpAddress="")
{
var embySettings = await _embySettings.GetSettingsAsync();
var client = _embyApi.CreateClient(embySettings);
Expand Down Expand Up @@ -196,7 +198,7 @@ private async Task<bool> CheckEmbyPasswordAsync(OmbiUser user, string password)
{
try
{
var result = await client.LogIn(user.UserName, password, server.ApiKey, server.FullUri);
var result = await client.LogIn(user.UserName, password, server.ApiKey, server.FullUri, clientIpAddress);
if (result != null)
{
return true;
Expand Down
28 changes: 28 additions & 0 deletions src/Ombi/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

using System.Linq;
using Microsoft.AspNetCore.Mvc;

public class BaseController : Controller
{
protected string GetRequestIP()
{
string ip = null;

if (Request.HttpContext?.Request?.Headers != null && Request.HttpContext.Request.Headers.ContainsKey("X-Forwarded-For"))
{
var forwardedip = Request.HttpContext.Request.Headers["X-Forwarded-For"].ToString();
ip = forwardedip.TrimEnd(',').Split(",").Select(s => s.Trim()).FirstOrDefault();
}

if (string.IsNullOrWhiteSpace(ip) && Request.HttpContext?.Connection?.RemoteIpAddress != null)
ip = Request.HttpContext.Connection.RemoteIpAddress.ToString();

if (string.IsNullOrWhiteSpace(ip) && Request.HttpContext?.Request?.Headers != null && Request.HttpContext.Request.Headers.ContainsKey("REMOTE_ADDR"))
{
var remoteip = Request.HttpContext.Request.Headers["REMOTE_ADDR"].ToString();
ip = remoteip.TrimEnd(',').Split(",").Select(s => s.Trim()).FirstOrDefault();
}

return ip;
}
}
3 changes: 2 additions & 1 deletion src/Ombi/Controllers/V1/IdentityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Ombi.Controllers.V1
[ApiV1]
[Produces("application/json")]
[ApiController]
public class IdentityController : Controller
public class IdentityController : BaseController
{
public IdentityController(OmbiUserManager user,
RoleManager<IdentityRole> rm,
Expand Down Expand Up @@ -555,6 +555,7 @@ public async Task<OmbiIdentityResult> UpdateLocalUser([FromBody] UpdateLocalUser
}

// Make sure the pass is ok
UserManager.ClientIpAddress = GetRequestIP();
var passwordCheck = await UserManager.CheckPasswordAsync(user, ui.CurrentPassword);
if (!passwordCheck)
{
Expand Down
25 changes: 2 additions & 23 deletions src/Ombi/Controllers/V1/TokenController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Token
[ApiV1]
[Produces("application/json")]
[ApiController]
public class TokenController : ControllerBase
public class TokenController : BaseController
{
public TokenController(OmbiUserManager um, ITokenRepository token,
IPlexOAuthManager oAuthManager, ILogger<TokenController> logger, ISettingsService<AuthenticationSettings> auth,
Expand Down Expand Up @@ -82,7 +82,7 @@ public async Task<IActionResult> GetToken([FromBody] UserAuthModel model)
user.EmailLogin = true;
}


_userManager.ClientIpAddress = GetRequestIP();
// Verify Password
if (await _userManager.CheckPasswordAsync(user, model.Password))
{
Expand Down Expand Up @@ -269,27 +269,6 @@ public class TokenRefresh
public string Userename { get; set; }
}

private string GetRequestIP()
{
string ip = null;

if (Request.HttpContext?.Request?.Headers != null && Request.HttpContext.Request.Headers.ContainsKey("X-Forwarded-For"))
{
var forwardedip = Request.HttpContext.Request.Headers["X-Forwarded-For"].ToString();
ip = forwardedip.TrimEnd(',').Split(",").Select(s => s.Trim()).FirstOrDefault();
}

if (string.IsNullOrWhiteSpace(ip) && Request.HttpContext?.Connection?.RemoteIpAddress != null)
ip = Request.HttpContext.Connection.RemoteIpAddress.ToString();

if (string.IsNullOrWhiteSpace(ip) && Request.HttpContext?.Request?.Headers != null && Request.HttpContext.Request.Headers.ContainsKey("REMOTE_ADDR"))
{
var remoteip = Request.HttpContext.Request.Headers["REMOTE_ADDR"].ToString();
ip = remoteip.TrimEnd(',').Split(",").Select(s => s.Trim()).FirstOrDefault();
}

return ip;
}

[HttpPost("header_auth")]
[ProducesResponseType(401)]
Expand Down