private async Task <Response> SavePlex() { var plexSettings = this.Bind <PlexSettings>(); var valid = this.Validate(plexSettings); if (!valid.IsValid) { return(Response.AsJson(valid.SendJsonError())); } //Lookup identifier var server = PlexApi.GetServer(plexSettings.PlexAuthToken); plexSettings.MachineIdentifier = server.Server.FirstOrDefault(x => x.AccessToken == plexSettings.PlexAuthToken)?.MachineIdentifier; var result = await PlexService.SaveSettingsAsync(plexSettings); return(Response.AsJson(result ? new JsonResponseModel { Result = true, Message = "Successfully Updated the Settings for Plex!" } : new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." })); }
private Response PlexAuth() { var user = this.Bind <PlexAuth>(); if (string.IsNullOrEmpty(user.username) || string.IsNullOrEmpty(user.password)) { return(Response.AsJson(new JsonResponseModel { Result = false, Message = "Please provide a valid username and password" })); } var model = PlexApi.SignIn(user.username, user.password); if (model?.user == null) { return(Response.AsJson(new JsonResponseModel { Result = false, Message = "Incorrect username or password!" })); } // Set the auth token in the session so we can use it in the next form Session[SessionKeys.UserWizardPlexAuth] = model.user.authentication_token; var servers = PlexApi.GetServer(model.user.authentication_token); var firstServer = servers.Server.FirstOrDefault(); return(Response.AsJson(new { Result = true, firstServer?.Port, Ip = firstServer?.LocalAddresses, firstServer?.Scheme })); }
public async Task <PlexAuthentication> SignIn([FromBody] UserRequest request) { try { // Do we already have settings? _log.LogDebug("OK, signing into Plex"); var settings = await PlexSettings.GetSettingsAsync(); if (!settings.Servers?.Any() ?? false) { return(null); } _log.LogDebug("This is our first time, good to go!"); var result = await PlexApi.SignIn(request); _log.LogDebug("Attempting to sign in to Plex.Tv"); if (!string.IsNullOrEmpty(result.user?.authentication_token)) { _log.LogDebug("Sign in successful"); _log.LogDebug("Getting servers"); var server = await PlexApi.GetServer(result.user.authentication_token); 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 = result.user.authentication_token, 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); } _log.LogDebug("Finished"); return(result); } catch (Exception e) { _log.LogCritical(e, "Error when trying to sign into Plex.tv"); throw; } }
private async Task <Response> Plex() { var form = this.Bind <PlexSettings>(); var valid = this.Validate(form); if (!valid.IsValid) { return(Response.AsJson(valid.SendJsonError())); } form.PlexAuthToken = Session[SessionKeys.UserWizardPlexAuth].ToString(); // Set the auth token from the previous form // Get the machine ID from the settings (This could have changed) try { var servers = PlexApi.GetServer(form.PlexAuthToken); var firstServer = servers.Server.FirstOrDefault(x => x.AccessToken == form.PlexAuthToken); Session[SessionKeys.UserWizardMachineId] = firstServer?.MachineIdentifier; form.MachineIdentifier = firstServer?.MachineIdentifier; } catch (Exception e) { // Probably bad settings, just continue Log.Error(e); } var result = await PlexSettings.SaveSettingsAsync(form); if (result) { return(Response.AsJson(new JsonResponseModel { Result = true })); } return (Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not save the settings to the database, please try again." })); }
/// <summary> /// Migrates to version1910. /// </summary> public void MigrateToVersion1910() { try { // Get the new machine Identifier var settings = new SettingsServiceV2 <PlexSettings>(new SettingsJsonRepository(Db, new MemoryCacheProvider())); var plex = settings.GetSettings(); if (!string.IsNullOrEmpty(plex.PlexAuthToken)) { var api = new PlexApi(new ApiRequest()); var server = api.GetServer(plex.PlexAuthToken); // Get the server info plex.MachineIdentifier = server.Server.FirstOrDefault(x => x.AccessToken == plex.PlexAuthToken)?.MachineIdentifier; settings.SaveSettings(plex); // Save the new settings } } catch (Exception e) { Log.Error(e); } }
public async Task <PlexServersViewModel> GetServers([FromBody] UserRequest u) { try { var signIn = await PlexApi.SignIn(u); var servers = await PlexApi.GetServer(signIn?.user?.authentication_token); return(new PlexServersViewModel { Servers = servers, Success = true }); } catch (Exception e) { return(new PlexServersViewModel { Success = false, Message = e.Message }); } }