From eaabd91f31db35e050b7215f5f36568bf6982a83 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 22 Sep 2017 23:43:58 -0400 Subject: authenticate GitHub queries if auth details are configured (#336) --- src/StardewModdingAPI.Web/Controllers/ModsController.cs | 17 +++++++++++++++-- .../Framework/ConfigModels/ModUpdateCheckConfig.cs | 6 ++++++ .../Framework/ModRepositories/GitHubRepository.cs | 6 +++++- src/StardewModdingAPI.Web/Startup.cs | 1 + src/StardewModdingAPI.Web/appsettings.json | 2 ++ 5 files changed, 29 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI.Web/Controllers/ModsController.cs b/src/StardewModdingAPI.Web/Controllers/ModsController.cs index 4b1abde4..4715d379 100644 --- a/src/StardewModdingAPI.Web/Controllers/ModsController.cs +++ b/src/StardewModdingAPI.Web/Controllers/ModsController.cs @@ -44,8 +44,21 @@ namespace StardewModdingAPI.Web.Controllers this.Repositories = new IModRepository[] { - new GitHubRepository(config.GitHubKey, config.GitHubBaseUrl, config.GitHubReleaseUrlFormat, config.GitHubUserAgent, config.GitHubAcceptHeader), - new NexusRepository(config.NexusKey, config.NexusUserAgent, config.NexusBaseUrl, config.NexusModUrlFormat) + new GitHubRepository( + vendorKey: config.GitHubKey, + baseUrl: config.GitHubBaseUrl, + releaseUrlFormat: config.GitHubReleaseUrlFormat, + userAgent: config.GitHubUserAgent, + acceptHeader: config.GitHubAcceptHeader, + username: config.GitHubUsername, + password: config.GitHubPassword + ), + new NexusRepository( + vendorKey: config.NexusKey, + userAgent: config.NexusUserAgent, + baseUrl: config.NexusBaseUrl, + modUrlFormat: config.NexusModUrlFormat + ) } .ToDictionary(p => p.VendorKey, StringComparer.CurrentCultureIgnoreCase); } diff --git a/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs b/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs index 4dbad506..5d55ba18 100644 --- a/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs +++ b/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs @@ -30,6 +30,12 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels /// The Accept header value expected by the GitHub API. public string GitHubAcceptHeader { get; set; } + /// The username with which to authenticate to the GitHub API (if any). + public string GitHubUsername { get; set; } + + /// The password with which to authenticate to the GitHub API (if any). + public string GitHubPassword { get; set; } + /**** ** Nexus Mods ****/ diff --git a/src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs b/src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs index c5772ad9..67e706ed 100644 --- a/src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs +++ b/src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs @@ -35,7 +35,9 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories /// The URL for a Nexus Mods API query excluding the , where {0} is the mod ID. /// The user agent for the GitHub API client. /// The Accept header value expected by the GitHub API. - public GitHubRepository(string vendorKey, string baseUrl, string releaseUrlFormat, string userAgent, string acceptHeader) + /// The username with which to authenticate to the GitHub API. + /// The password with which to authenticate to the GitHub API. + public GitHubRepository(string vendorKey, string baseUrl, string releaseUrlFormat, string userAgent, string acceptHeader, string username, string password) { this.VendorKey = vendorKey; this.ReleaseUrlFormat = releaseUrlFormat; @@ -43,6 +45,8 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories this.Client = new FluentClient(baseUrl) .SetUserAgent(string.Format(userAgent, this.GetType().Assembly.GetName().Version)) .AddDefault(req => req.WithHeader("Accept", acceptHeader)); + if (!string.IsNullOrWhiteSpace(username)) + this.Client = this.Client.SetBasicAuthentication(username, password); } /// Get metadata about a mod in the repository. diff --git a/src/StardewModdingAPI.Web/Startup.cs b/src/StardewModdingAPI.Web/Startup.cs index b668c63e..4806aefe 100644 --- a/src/StardewModdingAPI.Web/Startup.cs +++ b/src/StardewModdingAPI.Web/Startup.cs @@ -29,6 +29,7 @@ namespace StardewModdingAPI.Web { this.Configuration = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) + .AddEnvironmentVariables() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables() diff --git a/src/StardewModdingAPI.Web/appsettings.json b/src/StardewModdingAPI.Web/appsettings.json index f996157c..29fb195e 100644 --- a/src/StardewModdingAPI.Web/appsettings.json +++ b/src/StardewModdingAPI.Web/appsettings.json @@ -13,6 +13,8 @@ "GitHubBaseUrl": "https://api.github.com", "GitHubReleaseUrlFormat": "repos/{0}/releases/latest", "GitHubAcceptHeader": "application/vnd.github.v3+json", + "GitHubUsername": null, /* set via environment properties */ + "GitHubPassword": null, /* set via environment properties */ "NexusKey": "Nexus", "NexusUserAgent": "Nexus Client v0.63.15", -- cgit