fengling-console/docs/task-10-add-oauth-client-management.md

177 lines
4.9 KiB
Markdown

# Task 10: Add OAuth Client Management
## Task Description
**Files:**
- Create: `src/Fengling.AuthService/Models/OAuthApplication.cs`
- Create: `src/Fengling.AuthService/Controllers/OAuthClientsController.cs`
- Modify: `src/Fengling.AuthService/Data/ApplicationDbContext.cs`
## Implementation Steps
### Step 1: Create OAuthApplication model
Create: `src/Fengling.AuthService/Models/OAuthApplication.cs`
```csharp
namespace Fengling.AuthService.Models;
public class OAuthApplication
{
public long Id { get; set; }
public string ClientId { get; set; } = string.Empty;
public string? ClientSecret { get; set; }
public string DisplayName { get; set; } = string.Empty;
public string[] RedirectUris { get; set; } = Array.Empty<string>();
public string[] PostLogoutRedirectUris { get; set; } = Array.Empty<string>();
public string[] Scopes { get; set; } = Array.Empty<string>();
public string[] GrantTypes { get; set; } = Array.Empty<string>();
public string ClientType { get; set; } = "public";
public string ConsentType { get; set; } = "implicit";
public string Status { get; set; } = "active";
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime? UpdatedAt { get; set; }
}
```
### Step 2: Update ApplicationDbContext
Edit: `src/Fengling.AuthService/Data/ApplicationDbContext.cs`
Add to context:
```csharp
public DbSet<OAuthApplication> OAuthApplications { get; set; }
```
Add to OnModelCreating:
```csharp
builder.Entity<OAuthApplication>(entity =>
{
entity.HasKey(e => e.Id);
entity.HasIndex(e => e.ClientId).IsUnique();
entity.Property(e => e.ClientId).HasMaxLength(100);
entity.Property(e => e.ClientSecret).HasMaxLength(200);
entity.Property(e => e.DisplayName).HasMaxLength(100);
entity.Property(e => e.ClientType).HasMaxLength(20);
entity.Property(e => e.ConsentType).HasMaxLength(20);
entity.Property(e => e.Status).HasMaxLength(20);
});
```
### Step 3: Add migration
Run:
```bash
dotnet ef migrations add AddOAuthApplications
dotnet ef database update
```
### Step 4: Create OAuthClientsController
Create: `src/Fengling.AuthService/Controllers/OAuthClientsController.cs`
```csharp
using Fengling.AuthService.Data;
using Fengling.AuthService.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace Fengling.AuthService.Controllers;
[ApiController]
[Route("api/[controller]")]
public class OAuthClientsController : ControllerBase
{
private readonly ApplicationDbContext _context;
private readonly ILogger<OAuthClientsController> _logger;
public OAuthClientsController(
ApplicationDbContext context,
ILogger<OAuthClientsController> logger)
{
_context = context;
_logger = logger;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<OAuthApplication>>> GetClients()
{
return await _context.OAuthApplications.ToListAsync();
}
[HttpGet("{id}")]
public async Task<ActionResult<OAuthApplication>> GetClient(long id)
{
var client = await _context.OAuthApplications.FindAsync(id);
if (client == null)
{
return NotFound();
}
return client;
}
[HttpPost]
public async Task<ActionResult<OAuthApplication>> CreateClient(OAuthApplication application)
{
_context.OAuthApplications.Add(application);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetClient), new { id = application.Id }, application);
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateClient(long id, OAuthApplication application)
{
if (id != application.Id)
{
return BadRequest();
}
_context.Entry(application).State = EntityState.Modified;
await _context.SaveChangesAsync();
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteClient(long id)
{
var client = await _context.OAuthApplications.FindAsync(id);
if (client == null)
{
return NotFound();
}
_context.OAuthApplications.Remove(client);
await _context.SaveChangesAsync();
return NoContent();
}
}
```
### Step 5: Commit
```bash
git add src/Fengling.AuthService/Models/ src/Fengling.AuthService/Controllers/ src/Fengling.AuthService/Data/
git commit -m "feat(auth): add OAuth client management API"
```
## Context
This task adds OAuth client management functionality for managing OAuth applications. This will be used by Fengling.Console to register and manage clients.
**Tech Stack**: EF Core, ASP.NET Core Controllers
## Verification
- [ ] OAuthApplication model created
- [ ] DbSet added to ApplicationDbContext
- [ ] Migration generated and applied
- [ ] CRUD API endpoints created
- [ ] Build succeeds
- [ ] Committed to git
## Notes
- Clients can be registered through this API
- Fengling.Console will be pre-registered in Task 11
- Status field enables client enable/disable