Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds xml documentation generation. Better error handling when xml documentation is missing. #3889

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 41 additions & 44 deletions src/Ombi/Extensions/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.JsonPatch.Operations;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Ombi.Config;
using Ombi.Helpers;
using Ombi.Models.Identity;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Ombi
{
public static class StartupExtensions
public static class StartupExtensions
{
public static void AddSwagger(this IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo()
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Ombi Api V1",
Expand All @@ -37,35 +31,39 @@ public static void AddSwagger(this IServiceCollection services)
}
});




c.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme
{
Description = "API Key provided by Ombi. Example: \"ApiKey: {token}\"",
Name = "ApiKey",
In = ParameterLocation.Header,
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey
});

c.CustomSchemaIds(x => x.FullName);
var basePath = Path.GetDirectoryName(AppContext.BaseDirectory);
var xmlPath = Path.Combine(basePath, "Swagger.xml");

try
{
c.IncludeXmlComments(xmlPath);
string basePath = Path.GetDirectoryName(AppContext.BaseDirectory);
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
string xmlPath = Path.Combine(basePath ?? string.Empty, $"{assemblyName}.xml");
if (File.Exists(xmlPath))
{
c.IncludeXmlComments(xmlPath);
}
else
{
Console.WriteLine($"Swagger failed to find documentation file at '{xmlPath}'.");
}
}
catch (Exception e)
{
Console.WriteLine(e);
}


c.DescribeAllParametersInCamelCase();
});
}



public static void AddAppSettingsValues(this IServiceCollection services, IConfigurationRoot configuration)
{
services.Configure<ApplicationSettings>(configuration.GetSection("ApplicationSettings"));
Expand All @@ -78,47 +76,46 @@ public static void AddAppSettingsValues(this IServiceCollection services, IConfi

public static void AddJwtAuthentication(this IServiceCollection services, IConfigurationRoot configuration)
{
var tokenOptions = configuration.GetSection("TokenAuthentication");

var tokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(StartupSingleton.Instance.SecurityKey)),

RequireExpirationTime = true,
ValidateLifetime = true,
ValidAudience = "Ombi",
ValidIssuer = "Ombi",
ClockSkew = TimeSpan.Zero,
};

services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(x =>
{
x.Audience = "Ombi";
x.TokenValidationParameters = tokenValidationParameters;
x.Events = new JwtBearerEvents
services
.AddAuthentication(options =>
{
OnMessageReceived = context =>
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.Audience = "Ombi";
x.TokenValidationParameters = tokenValidationParameters;
x.Events = new JwtBearerEvents
{
var accessToken = context.Request.Query["access_token"];

// If the request is for our hub...
var path = context.HttpContext.Request.Path;
if (!string.IsNullOrEmpty(accessToken) &&
(path.StartsWithSegments("/hubs")))
OnMessageReceived = context =>
{
// Read the token out of the query string
context.Token = accessToken;
}
return Task.CompletedTask;
}
};
});
var accessToken = context.Request.Query["access_token"];

// If the request is for our hub...
var path = context.HttpContext.Request.Path;
if (!string.IsNullOrEmpty(accessToken) &&
(path.StartsWithSegments("/hubs")))
{
// Read the token out of the query string
context.Token = accessToken;
}

return Task.CompletedTask;
}
};
});
}
}
}
1 change: 1 addition & 0 deletions src/Ombi/Ombi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<SpaRoot>ClientApp\</SpaRoot>
<DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
<TieredCompilationQuickJit>true</TieredCompilationQuickJit> <!--https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0#tiered-compilation-->
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup>
Expand Down