mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-30 19:40:05 -07:00
Made an attempt at PlexOAuth !wip
This commit is contained in:
parent
a837868be5
commit
7bbcb9a626
3 changed files with 97 additions and 39 deletions
40
src/Ombi/Controllers/PlexOAuthController.cs
Normal file
40
src/Ombi/Controllers/PlexOAuthController.cs
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
using System.Net;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Http.Internal;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Ombi.Controllers
|
||||||
|
{
|
||||||
|
[ApiExplorerSettings(IgnoreApi = true)]
|
||||||
|
[ApiV1]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public class PlexOAuthController : Controller
|
||||||
|
{
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult OAuthCallBack()
|
||||||
|
{
|
||||||
|
var bodyStr = "";
|
||||||
|
var req = Request;
|
||||||
|
|
||||||
|
// Allows using several time the stream in ASP.Net Core
|
||||||
|
req.EnableRewind();
|
||||||
|
|
||||||
|
// Arguments: Stream, Encoding, detect encoding, buffer size
|
||||||
|
// AND, the most important: keep stream opened
|
||||||
|
using (StreamReader reader
|
||||||
|
= new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
|
||||||
|
{
|
||||||
|
bodyStr = reader.ReadToEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rewind, so the core is not lost when it looks the body for the request
|
||||||
|
req.Body.Position = 0;
|
||||||
|
|
||||||
|
// Do your work with bodyStr
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IdentityModel.Tokens.Jwt;
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
@ -9,6 +10,7 @@ using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using Ombi.Api;
|
||||||
using Ombi.Core.Authentication;
|
using Ombi.Core.Authentication;
|
||||||
using Ombi.Helpers;
|
using Ombi.Helpers;
|
||||||
using Ombi.Models;
|
using Ombi.Models;
|
||||||
|
@ -62,6 +64,8 @@ namespace Ombi.Controllers
|
||||||
user.EmailLogin = true;
|
user.EmailLogin = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!model.UsePlexOAuth)
|
||||||
|
{
|
||||||
// Verify Password
|
// Verify Password
|
||||||
if (await _userManager.CheckPasswordAsync(user, model.Password))
|
if (await _userManager.CheckPasswordAsync(user, model.Password))
|
||||||
{
|
{
|
||||||
|
@ -92,7 +96,7 @@ namespace Ombi.Controllers
|
||||||
claims: claims,
|
claims: claims,
|
||||||
expires: model.RememberMe ? DateTime.UtcNow.AddDays(7) : DateTime.UtcNow.AddHours(5),
|
expires: model.RememberMe ? DateTime.UtcNow.AddDays(7) : DateTime.UtcNow.AddHours(5),
|
||||||
signingCredentials: creds,
|
signingCredentials: creds,
|
||||||
audience: "Ombi", issuer:"Ombi"
|
audience: "Ombi", issuer: "Ombi"
|
||||||
);
|
);
|
||||||
var accessToken = new JwtSecurityTokenHandler().WriteToken(token);
|
var accessToken = new JwtSecurityTokenHandler().WriteToken(token);
|
||||||
if (model.RememberMe)
|
if (model.RememberMe)
|
||||||
|
@ -107,6 +111,19 @@ namespace Ombi.Controllers
|
||||||
expiration = token.ValidTo
|
expiration = token.ValidTo
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Plex OAuth
|
||||||
|
// Redirect them to Plex
|
||||||
|
|
||||||
|
var request = new Request("auth", "https://app.plex.tv", HttpMethod.Get);
|
||||||
|
request.AddQueryString("clientID", "OMBIv3");
|
||||||
|
request.AddQueryString("forwardUrl", "http://localhost:5000");
|
||||||
|
request.AddQueryString("context-device-product", "http://localhost:5000");
|
||||||
|
return new RedirectResult("https://app.plex.tv/auth#?forwardUrl=http://localhost:5000/api/v1/plexoauth&clientID=OMBIv3&context%5Bdevice%5D%5Bproduct%5D=Ombi%20SSO");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return new UnauthorizedResult();
|
return new UnauthorizedResult();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,5 +6,6 @@
|
||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
public bool RememberMe { get; set; }
|
public bool RememberMe { get; set; }
|
||||||
public bool UsePlexAdminAccount { get; set; }
|
public bool UsePlexAdminAccount { get; set; }
|
||||||
|
public bool UsePlexOAuth { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue