Added the ability to get the ombi user via a Plex Token #2591

pull/2628/head
Jamie 6 years ago
parent e7a776640b
commit 07f3f4162a

@ -29,6 +29,7 @@ using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Ombi.Api.Emby;
@ -101,6 +102,22 @@ namespace Ombi.Core.Authentication
return true;
}
public async Task<OmbiUser> GetOmbiUserFromPlexToken(string plexToken)
{
var plexAccount = await _plexApi.GetAccount(plexToken);
// Check for a ombi user
if (plexAccount?.user != null)
{
var potentialOmbiUser = await Users.FirstOrDefaultAsync(x =>
x.ProviderUserId.Equals(plexAccount.user.id, StringComparison.InvariantCultureIgnoreCase));
return potentialOmbiUser;
}
return null;
}
/// <summary>
/// Sign the user into plex and make sure we can get the authentication token.
/// <remarks>We do not check if the user is in the owners "friends" since they must have a local user account to get this far</remarks>

@ -10,14 +10,13 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Ombi.Api;
using Ombi.Core.Authentication;
using Ombi.Helpers;
using Ombi.Models;
using Ombi.Models.External;
using Ombi.Models.Identity;
using Ombi.Store.Entities;
using Ombi.Store.Repository;
using StackExchange.Profiling.Helpers;
namespace Ombi.Controllers
{
@ -80,7 +79,7 @@ namespace Ombi.Controllers
{
// Plex OAuth
// Redirect them to Plex
var websiteAddress = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}";
//https://app.plex.tv/auth#?forwardUrl=http://google.com/&clientID=Ombi-Test&context%5Bdevice%5D%5Bproduct%5D=Ombi%20SSO&pinID=798798&code=4lgfd
var url = await _plexOAuthManager.GetOAuthUrl(model.PlexTvPin.code, websiteAddress);
@ -97,6 +96,22 @@ namespace Ombi.Controllers
return new UnauthorizedResult();
}
[HttpPost("plextoken")]
public async Task<IActionResult> GetTokenWithPlexToken([FromBody] PlexTokenAuthentication model)
{
if (!model.PlexToken.HasValue())
{
return BadRequest("Token was not provided");
}
var user = await _userManager.GetOmbiUserFromPlexToken(model.PlexToken);
if (user == null)
{
return Unauthorized();
}
return await CreateToken(true, user);
}
private async Task<IActionResult> CreateToken(bool rememberMe, OmbiUser user)
{
var roles = await _userManager.GetRolesAsync(user);

@ -0,0 +1,7 @@
namespace Ombi.Models.External
{
public class PlexTokenAuthentication
{
public string PlexToken { get; set; }
}
}
Loading…
Cancel
Save