Start protecting the Api
This commit is contained in:
parent
070f41ac7e
commit
57adfc82a5
5 changed files with 11 additions and 4 deletions
|
|
@ -58,7 +58,7 @@ internal class Program
|
||||||
{
|
{
|
||||||
policy
|
policy
|
||||||
.RequireAuthenticatedUser()
|
.RequireAuthenticatedUser()
|
||||||
.RequireClaim(JwtClaimTypes.Scope, new string[] { "scope2" });
|
.RequireClaim(JwtClaimTypes.Scope, new string[] { "com" });
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.AddCors(options =>
|
.AddCors(options =>
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Yavsc.Models;
|
using Yavsc.Models;
|
||||||
|
|
@ -6,6 +7,7 @@ using Yavsc.Server.Helpers;
|
||||||
|
|
||||||
namespace Yavsc.Controllers
|
namespace Yavsc.Controllers
|
||||||
{
|
{
|
||||||
|
[Authorize("BlogScope")]
|
||||||
[Produces("application/json")]
|
[Produces("application/json")]
|
||||||
[Route("api/blog")]
|
[Route("api/blog")]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,11 +41,11 @@ internal class Program
|
||||||
services
|
services
|
||||||
.AddAuthorization(options =>
|
.AddAuthorization(options =>
|
||||||
{
|
{
|
||||||
options.AddPolicy("ApiScope", policy =>
|
options.AddPolicy("BlogScope", policy =>
|
||||||
{
|
{
|
||||||
policy
|
policy
|
||||||
.RequireAuthenticatedUser()
|
.RequireAuthenticatedUser()
|
||||||
.RequireClaim(JwtClaimTypes.Scope, new string[] { "blogs" });
|
.RequireClaim(JwtClaimTypes.Scope, new string[] { "blog" });
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.AddCors(options =>
|
.AddCors(options =>
|
||||||
|
|
|
||||||
5
src/Yavsc.Org/Contants.cs
Normal file
5
src/Yavsc.Org/Contants.cs
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
|
||||||
|
public static class Constants
|
||||||
|
{
|
||||||
|
public static readonly string[] BuildInApiScopes = { "blog", "admin", "contract", "com" };
|
||||||
|
}
|
||||||
|
|
@ -342,7 +342,7 @@ public static class HostingExtensions
|
||||||
{
|
{
|
||||||
return (context, _) =>
|
return (context, _) =>
|
||||||
{
|
{
|
||||||
foreach (String scope in new string[] { "blog", "admin", "contract", "com" })
|
foreach (String scope in Constants.BuildInApiScopes)
|
||||||
{
|
{
|
||||||
var existentScope = context.Set<ApiScope>().FirstOrDefault(b => b.Name == scope);
|
var existentScope = context.Set<ApiScope>().FirstOrDefault(b => b.Name == scope);
|
||||||
if (existentScope == null)
|
if (existentScope == null)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue