Skip to content

Commit

Permalink
Merge pull request #29 from dimmik/swagger
Browse files Browse the repository at this point in the history
Swagger
  • Loading branch information
dimmik authored Oct 19, 2019
2 parents cc59e20 + 7c1d6e7 commit 5b2047e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
5 changes: 2 additions & 3 deletions TourCalcWebApp/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
using TourCalcWebApp.Auth;
using TourCalcWebApp.Exceptions;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace TourCalcWebApp.Controllers
{
[Route("api/[controller]")]
Expand Down Expand Up @@ -53,6 +51,7 @@ public string GenerateRandomKey(int lengthInBytes)
/// </summary>
/// <param name="scope">Should be 'code' or 'admin'</param>
/// <param name="key">code or admin key</param>
/// <param name="signerKey">ECDSA crypto service</param>
/// <returns>JWT Token</returns>
[HttpGet("token/{scope}/{key}")]
public string GetToken(string scope, string key, [FromServices] IECDsaCryptoKey signerKey)
Expand Down Expand Up @@ -106,7 +105,7 @@ private AuthData Authorize(string scope, string accessCode)
auth.IsMaster = true;
} else
{
throw HttpException.NotAuthenticated($"Wrong Master Key. Should be '{Guid.NewGuid()}'");
throw HttpException.NotAuthenticated($"Wrong Master Key");
}
}
/*else if (scope == "user")
Expand Down
20 changes: 10 additions & 10 deletions TourCalcWebApp/Exceptions/HttpException.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace TourCalcWebApp.Exceptions
{
Expand All @@ -23,34 +20,37 @@ public static HttpException NotAuthenticated(string msg)
return new HttpException(401, msg);
}




public HttpException(int httpStatusCode)
{
this.StatusCode = httpStatusCode;
StatusCode = httpStatusCode;
}

public HttpException(HttpStatusCode httpStatusCode)
{
this.StatusCode = (int)httpStatusCode;
StatusCode = (int)httpStatusCode;
}

private HttpException(int httpStatusCode, string message) : base(message)
public HttpException(int httpStatusCode, string message) : base(message)
{
this.StatusCode = httpStatusCode;
StatusCode = httpStatusCode;
}

public HttpException(HttpStatusCode httpStatusCode, string message) : base(message)
{
this.StatusCode = (int)httpStatusCode;
StatusCode = (int)httpStatusCode;
}

public HttpException(int httpStatusCode, string message, Exception inner) : base(message, inner)
{
this.StatusCode = httpStatusCode;
StatusCode = httpStatusCode;
}

public HttpException(HttpStatusCode httpStatusCode, string message, Exception inner) : base(message, inner)
{
this.StatusCode = (int)httpStatusCode;
StatusCode = (int)httpStatusCode;
}

public int StatusCode { get; }
Expand Down
17 changes: 17 additions & 0 deletions TourCalcWebApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Swagger;
using TourCalcWebApp.Exceptions;
using Swashbuckle.AspNetCore.SwaggerGen;
using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;
using System.Linq;
using Swashbuckle.AspNetCore.Filters;

namespace TourCalcWebApp
{
Expand All @@ -41,6 +46,18 @@ public void ConfigureServices(IServiceCollection services)
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Tourcalc API", Version = "v1" });
// https://stackoverflow.com/questions/43447688/setting-up-swagger-asp-net-core-using-the-authorization-headers-bearer
c.AddSecurityDefinition("Bearer",
new ApiKeyScheme
{
In = "header",
Description = "Please enter into field the word 'Bearer' following by space and JWT",
Name = "Authorization",
Type = "apiKey"
});
c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> {
{ "Bearer", Enumerable.Empty<string>() },
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
Expand Down
1 change: 1 addition & 0 deletions TourCalcWebApp/TourCalcWebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<PackageReference Include="Microsoft.OpenApi" Version="1.1.4" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="4.5.5" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 5b2047e

Please sign in to comment.