diff --git a/src/Ombi.Core/Authentication/OmbiUserManager.cs b/src/Ombi.Core/Authentication/OmbiUserManager.cs index 185677215..4e3d9ef43 100644 --- a/src/Ombi.Core/Authentication/OmbiUserManager.cs +++ b/src/Ombi.Core/Authentication/OmbiUserManager.cs @@ -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 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; + } + + /// /// Sign the user into plex and make sure we can get the authentication token. /// We do not check if the user is in the owners "friends" since they must have a local user account to get this far diff --git a/src/Ombi/Controllers/TokenController.cs b/src/Ombi/Controllers/TokenController.cs index 87136fd00..0c05c81fe 100644 --- a/src/Ombi/Controllers/TokenController.cs +++ b/src/Ombi/Controllers/TokenController.cs @@ -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 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 CreateToken(bool rememberMe, OmbiUser user) { var roles = await _userManager.GetRolesAsync(user); diff --git a/src/Ombi/Models/External/PlexTokenAuthentication.cs b/src/Ombi/Models/External/PlexTokenAuthentication.cs new file mode 100644 index 000000000..38184ecdb --- /dev/null +++ b/src/Ombi/Models/External/PlexTokenAuthentication.cs @@ -0,0 +1,7 @@ +namespace Ombi.Models.External +{ + public class PlexTokenAuthentication + { + public string PlexToken { get; set; } + } +} \ No newline at end of file