APi Scopes
This commit is contained in:
parent
4f8ccc919d
commit
be7df3d054
25 changed files with 823 additions and 116 deletions
|
|
@ -5,33 +5,36 @@ using Microsoft.AspNetCore.Authorization;
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Auth;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
[Authorize("AdministratorOnly")]
|
||||
public class ClientController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext context;
|
||||
private readonly ApplicationDbContext dbContext;
|
||||
private readonly ClientStore clientStore;
|
||||
private readonly SiteSettings siteSettings;
|
||||
|
||||
public ClientController(ApplicationDbContext context,
|
||||
ClientStore clientStore,
|
||||
|
||||
IdentityServer8.Stores.ValidatingClientStore<ClientStore> validatingClientStore
|
||||
public ClientController(
|
||||
ApplicationDbContext dbContext,
|
||||
ClientStore clientStore, IOptions<SiteSettings> siteSettingsOptions
|
||||
|
||||
|
||||
)
|
||||
{
|
||||
this.context = context;
|
||||
this.dbContext = dbContext;
|
||||
this.clientStore = clientStore;
|
||||
this.siteSettings = siteSettingsOptions.Value;
|
||||
}
|
||||
|
||||
// GET: Client
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
return View(await context.Clients.Include(c => c.AllowedGrantTypes)
|
||||
return View(await dbContext.Clients.Include(c => c.AllowedGrantTypes)
|
||||
.Include(c => c.RedirectUris).ToListAsync());
|
||||
}
|
||||
|
||||
|
|
@ -39,7 +42,7 @@ namespace Yavsc.Controllers
|
|||
public async Task<IActionResult> Details(int id)
|
||||
{
|
||||
|
||||
Client client = await context.Clients.Include(
|
||||
Client client = await dbContext.Clients.Include(
|
||||
c => c.ClientSecrets
|
||||
).Include(c => c.AllowedGrantTypes)
|
||||
.Include(c => c.RedirectUris)
|
||||
|
|
@ -59,8 +62,6 @@ namespace Yavsc.Controllers
|
|||
// GET: Client/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
Secret s;
|
||||
|
||||
SetAppTypesInputValues();
|
||||
return View();
|
||||
}
|
||||
|
|
@ -68,6 +69,7 @@ namespace Yavsc.Controllers
|
|||
// POST: Client/Create
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
|
||||
public async Task<IActionResult> Create(Client client)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
|
|
@ -79,16 +81,39 @@ namespace Yavsc.Controllers
|
|||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
context.Clients.Add(client);
|
||||
if (client.ClientSecrets != null)
|
||||
{
|
||||
foreach (var secret in client.ClientSecrets)
|
||||
{
|
||||
context.ClientSecrets.Add(secret);
|
||||
}
|
||||
}
|
||||
await context.SaveChangesAsync();
|
||||
dbContext.Clients.Add(client);
|
||||
await dbContext.SaveChangesAsync(User.GetUserId());
|
||||
|
||||
dbContext.ClientRedirectUris.Add(new ClientRedirectUri
|
||||
{
|
||||
ClientId = client.Id,
|
||||
RedirectUri = siteSettings.Audience
|
||||
|
||||
});
|
||||
dbContext.ClientCorsOrigins.Add(new ClientCorsOrigin
|
||||
{
|
||||
ClientId = client.Id,
|
||||
Origin = siteSettings.Audience
|
||||
|
||||
});
|
||||
foreach (String credType in new String[] { "code", "client_credentials", "password" })
|
||||
{
|
||||
dbContext.ClientGrantTypes.Add(new ClientGrantType
|
||||
{
|
||||
ClientId = client.Id,
|
||||
GrantType = credType
|
||||
});
|
||||
}
|
||||
foreach (String scope in new String[] { "openid", "profile" })
|
||||
{
|
||||
dbContext.ClientScopes.Add(new ClientScope
|
||||
{
|
||||
ClientId = client.Id,
|
||||
Scope = scope
|
||||
});
|
||||
}
|
||||
|
||||
await dbContext.SaveChangesAsync(User.GetUserId());
|
||||
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
|
@ -112,7 +137,7 @@ namespace Yavsc.Controllers
|
|||
// GET: Client/Edit/5
|
||||
public async Task<IActionResult> Edit(int id)
|
||||
{
|
||||
Client client = await context.Clients.SingleOrDefaultAsync(m => m.Id == id);
|
||||
Client client = await dbContext.Clients.SingleOrDefaultAsync(m => m.Id == id);
|
||||
if (client == null)
|
||||
{
|
||||
return NotFound();
|
||||
|
|
@ -133,11 +158,11 @@ namespace Yavsc.Controllers
|
|||
{
|
||||
foreach (var secret in client.ClientSecrets)
|
||||
{
|
||||
context.Update(secret);
|
||||
dbContext.Update(secret);
|
||||
}
|
||||
}
|
||||
context.Update(client);
|
||||
await context.SaveChangesAsync();
|
||||
dbContext.Update(client);
|
||||
await dbContext.SaveChangesAsync();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
return View(client);
|
||||
|
|
@ -148,7 +173,7 @@ namespace Yavsc.Controllers
|
|||
public async Task<IActionResult> Delete(int id)
|
||||
{
|
||||
|
||||
Client client = await context.Clients.SingleOrDefaultAsync(m => m.Id == id);
|
||||
Client client = await dbContext.Clients.SingleOrDefaultAsync(m => m.Id == id);
|
||||
if (client == null)
|
||||
{
|
||||
return NotFound();
|
||||
|
|
@ -162,11 +187,11 @@ namespace Yavsc.Controllers
|
|||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(int id)
|
||||
{
|
||||
Client client = await context.Clients
|
||||
Client client = await dbContext.Clients
|
||||
.Include(client => client.ClientSecrets)
|
||||
.SingleAsync(m => m.Id == id);
|
||||
context.Clients.Remove(client);
|
||||
await context.SaveChangesAsync();
|
||||
dbContext.Clients.Remove(client);
|
||||
await dbContext.SaveChangesAsync();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue