# 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(); public string[] PostLogoutRedirectUris { get; set; } = Array.Empty(); public string[] Scopes { get; set; } = Array.Empty(); public string[] GrantTypes { get; set; } = Array.Empty(); 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 OAuthApplications { get; set; } ``` Add to OnModelCreating: ```csharp builder.Entity(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 _logger; public OAuthClientsController( ApplicationDbContext context, ILogger logger) { _context = context; _logger = logger; } [HttpGet] public async Task>> GetClients() { return await _context.OAuthApplications.ToListAsync(); } [HttpGet("{id}")] public async Task> GetClient(long id) { var client = await _context.OAuthApplications.FindAsync(id); if (client == null) { return NotFound(); } return client; } [HttpPost] public async Task> CreateClient(OAuthApplication application) { _context.OAuthApplications.Add(application); await _context.SaveChangesAsync(); return CreatedAtAction(nameof(GetClient), new { id = application.Id }, application); } [HttpPut("{id}")] public async Task 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 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