Skip to content

Commit

Permalink
#391: Added authentication support for remote files via custom HTTP h…
Browse files Browse the repository at this point in the history
…eaders
  • Loading branch information
danielpalme committed Oct 6, 2020
1 parent 4823752 commit ab58a27
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ CHANGELOG

* New: Dropped support for Nuget package 'dotnet-reportgenerator-cli'. Use 'dotnet-reportgenerator-globaltool' instead.
* New: #384: Added warning for unknown or duplicate command line arguments
* New: #391: Added authentication support for remote files via custom HTTP headers

4.6.7.0

Expand Down
2 changes: 1 addition & 1 deletion src/ReportGenerator.Core/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public bool GenerateReport(IReportConfiguration reportConfiguration)
DateTime executionTime = DateTime.Now;

new Reporting.ReportGenerator(
new CachingFileReader(new LocalFileReader(reportConfiguration.SourceDirectories), settings.CachingDurationOfRemoteFilesInMinutes),
new CachingFileReader(new LocalFileReader(reportConfiguration.SourceDirectories), settings.CachingDurationOfRemoteFilesInMinutes, settings.CustomHeadersForRemoteFiles),
parserResult,
reportBuilderFactory.GetReportBuilders(reportContext))
.CreateReport(reportConfiguration.HistoryDirectory != null, overallHistoricCoverages, executionTime, reportConfiguration.Tag);
Expand Down
20 changes: 19 additions & 1 deletion src/ReportGenerator.Core/Parser/FileReading/CachingFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,28 @@ internal class CachingFileReader : IFileReader
/// </summary>
/// <param name="localFileReader"><see cref="IFileReader"/> for loading files from local disk.</param>
/// <param name="cachingDurationOfRemoteFilesInMinutes">The caching duration of code files that are downloaded from remote servers in minutes.</param>
public CachingFileReader(IFileReader localFileReader, int cachingDurationOfRemoteFilesInMinutes)
/// <param name="customHeadersForRemoteFiles">Custom headers (e.g. authentication headers) for remote requests.</param>
public CachingFileReader(IFileReader localFileReader, int cachingDurationOfRemoteFilesInMinutes, string customHeadersForRemoteFiles)
{
this.localFileReader = localFileReader;
this.cachingDurationOfRemoteFilesInMinutes = cachingDurationOfRemoteFilesInMinutes;

if (!string.IsNullOrWhiteSpace(customHeadersForRemoteFiles))
{
var parts = customHeadersForRemoteFiles.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

foreach (var part in parts)
{
int index = part.IndexOf('=');

if (index > 0 && part.Length > index + 1)
{
string key = part.Substring(0, index);
string value = part.Substring(index + 1);
HttpClient.DefaultRequestHeaders.Add(key, value);
}
}
}
}

/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions src/ReportGenerator.Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,12 @@ public int CachingDuringOfRemoteFilesInMinutes
/// Gets or sets a value indicating whether a subdirectory should be created in the target directory for each report type.
/// </summary>
public bool CreateSubdirectoryForAllReportTypes { get; set; } = false;

/// <summary>
/// Gets or sets custom headers (e.g. authentication headers) for remote requests.
/// Format: key1=value1;key2=value2
/// Example: Authorization=Bearer <JWT>
/// </summary>
public string CustomHeadersForRemoteFiles { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/ReportGenerator.Core/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"cachingDurationOfRemoteFilesInMinutes": 10080,
"disableRiskHotspots": false,
"excludeTestProjects": false,
"createSubdirectoryForAllReportTypes": false
"createSubdirectoryForAllReportTypes": false,
"customHeadersForRemoteFiles": null
}
}

0 comments on commit ab58a27

Please sign in to comment.