Skip to content

Commit

Permalink
AddUser endpoint simplified (#76)
Browse files Browse the repository at this point in the history
* Fix github workflows

* AddUser endpoint simplified
  • Loading branch information
AleF83 authored Mar 10, 2021
1 parent 9e962e3 commit ef8ebb8
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Reflection;
using System.Security.Claims;
using IdentityServer4.Test;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
Expand All @@ -10,32 +11,37 @@ namespace OpenIdConnectServer.Controllers
public class UserController: Controller
{
private readonly TestUserStore _usersStore;
private readonly ICollection<TestUser> _users;
private readonly ILogger Logger;

public UserController(TestUserStore userStore, ILogger<UserController> logger)
{
_usersStore = userStore;
Logger = logger;

var usersField = _usersStore.GetType().GetField("_users", BindingFlags.NonPublic | BindingFlags.Instance);
_users = usersField.GetValue(_usersStore) as List<TestUser>;
}

[HttpGet("{subjectId}")]
public IActionResult GetUser([FromRoute]string subjectId)
{
var user = _usersStore.FindBySubjectId(subjectId);
Logger.LogDebug("GetUser: {subjectId}: {user}", subjectId, user);
Logger.LogDebug("User found: {subjectId}", subjectId);
return Json(user);
}

[HttpPost]
public IActionResult AddUser([FromBody]TestUser user)
{
_users.Add(user);
Logger.LogDebug("AddUser {user}", user);
var claims = new List<Claim>(user.Claims);
claims.Add(new Claim(ClaimTypes.Name, user.Username));
var newUser =_usersStore.AutoProvisionUser("Alex", user.SubjectId, new List<Claim>(user.Claims));
newUser.SubjectId = user.SubjectId;
newUser.Username = user.Username;
newUser.Password = user.Password;
newUser.ProviderName = string.Empty;
newUser.ProviderSubjectId = string.Empty;

Logger.LogDebug("New user added: {user}", user.SubjectId);

return Json(user.SubjectId);
}
}
}
}

0 comments on commit ef8ebb8

Please sign in to comment.