Esempio n. 1
0
        public async Task <IActionResult> OAuth(int pinId)
        {
            var accessToken = await _plexOAuthManager.GetAccessTokenFromPin(pinId);

            if (accessToken.IsNullOrEmpty())
            {
                // Looks like we are not authenticated.
                return(new JsonResult(new
                {
                    errorMessage = "Could not authenticate with Plex"
                }));
            }

            // Let's look for the users account
            var account = await _plexOAuthManager.GetAccount(accessToken);

            // Get the ombi user
            var user = await _userManager.FindByNameAsync(account.user.username);

            if (user == null)
            {
                // Could this be an email login?
                user = await _userManager.FindByEmailAsync(account.user.email);

                if (user == null)
                {
                    return(new UnauthorizedResult());
                }
            }

            user.MediaServerToken = account.user.authentication_token;
            await _userManager.UpdateAsync(user);

            return(await CreateToken(true, user));
        }
Esempio n. 2
0
        public async Task <IActionResult> OAuthWizardCallBack([FromRoute] int pinId)
        {
            var accessToken = await _manager.GetAccessTokenFromPin(pinId);

            if (accessToken.IsNullOrEmpty())
            {
                return(Json(new
                {
                    success = false,
                    error = "Authentication did not work. Please try again"
                }));
            }
            var settings = await _plexSettings.GetSettingsAsync();

            var server = await _plexApi.GetServer(accessToken);

            var servers = server.Server.FirstOrDefault();

            if (servers == null)
            {
                _log.LogWarning("Looks like we can't find any Plex Servers");
            }
            _log.LogDebug("Adding first server");

            settings.Enable  = true;
            settings.Servers = new List <PlexServers> {
                new PlexServers
                {
                    PlexAuthToken     = accessToken,
                    Id                = new Random().Next(),
                    Ip                = servers?.LocalAddresses?.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries)?.FirstOrDefault() ?? string.Empty,
                    MachineIdentifier = servers?.MachineIdentifier ?? string.Empty,
                    Port              = int.Parse(servers?.Port ?? "0"),
                    Ssl               = (servers?.Scheme ?? "http") != "http",
                    Name              = "Server 1",
                }
            };

            await _plexSettings.SaveSettingsAsync(settings);

            return(Json(new { accessToken }));
        }