Skip to content

Commit

Permalink
Merge pull request #29 from brunohbrito/2.0/plug-new-identity-system
Browse files Browse the repository at this point in the history
Removed dependencies from ASP.NET Identity
  • Loading branch information
brunobritodev committed Mar 6, 2020
2 parents 1366063 + b6fdda0 commit 488587f
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,37 @@ public class PersistedGrantsController : ApiController
[HttpGet, Route("")]
public async Task<ActionResult<ListOf<PersistedGrantViewModel>>> List([Range(1, 50)] int? limit = 10, [Range(1, int.MaxValue)] int? offset = 0)
{
// Search for grants
var searchPersisted = new PersistedGrantSearch()
{
Limit = limit,
Offset = offset
};
var irs = await _persistedGrantAppService.GetPersistedGrants(searchPersisted);
var usersIds = irs.Collection.Select(s => s.SubjectId).ToArray();
var persistedGrants = await _persistedGrantAppService.GetPersistedGrants(searchPersisted);

// Get additional data from users
var usersIds = persistedGrants.Collection.Select(s => s.SubjectId).ToArray();
var search = new UserSearch<string>()
{
Id = usersIds,
Limit = limit,
Offset = offset
};
var users = await _manager.Users.Apply(search).ToListAsync();
var collection = irs.Collection.ToList();
foreach (var persistedGrantViewModel in collection)

// Update addional data
foreach (var persistedGrantViewModel in persistedGrants.Collection)
{
var user = users.FirstOrDefault(u => u.Id == persistedGrantViewModel.SubjectId);
if (user == null) continue;

persistedGrantViewModel.UpdateUserInfo(user.UserName, user.Picture);
persistedGrantViewModel.UpdateUserInfo(user.UserName);
}

// truncate data for non administration users
if (!User.IsInRole("Administrator") && !User.HasClaim(c => c.Type == "is4-manager"))
{
foreach (var persistedGrantViewModel in collection)
foreach (var persistedGrantViewModel in persistedGrants.Collection)
{
if (persistedGrantViewModel.Email == _systemUser.Username)
continue;
Expand All @@ -78,7 +82,7 @@ public async Task<ActionResult<ListOf<PersistedGrantViewModel>>> List([Range(1,
}
}

return ResponseGet(new ListOf<PersistedGrantViewModel>(collection, collection.Count));
return ResponseGet(new ListOf<PersistedGrantViewModel>(persistedGrants.Collection, persistedGrants.Total));
}

[HttpDelete, Route("{id}")]
Expand Down
15 changes: 12 additions & 3 deletions src/Backend/Jp.Api.Management/Controllers/UserAdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace Jp.Api.Management.Controllers
Expand Down Expand Up @@ -48,11 +49,19 @@ public class UserAdminController : ApiController
[HttpGet, Route("")]
public async Task<ActionResult<ListOf<UserListViewModel>>> List([Range(1, 50)] int? limit = 10, [Range(1, int.MaxValue)] int? offset = 0, string search = null)
{
var irs = await _userManageAppService.SearchUsers(new UserFindByEmailNameUsername(search) { Limit = limit, Offset = offset });
var users = await _userManageAppService.SearchUsers(new UserFindByEmailNameUsername(search) { Limit = limit, Offset = offset });
var usersByClaims = await _userManageAppService.SearchUsersByClaims(new SearchUserByClaim() { Value = search });


var usersFinal = users.Collection.ToList();
usersFinal.AddRange(usersByClaims.Collection);

var collectionOfUsers = new ListOf<UserListViewModel>(usersFinal, usersFinal.Count);

// Truncate data for non admins
if (!User.IsInRole("Administrator") && !User.HasClaim(c => c.Type == "is4-manager"))
{
foreach (var ir in irs.Collection)
foreach (var ir in collectionOfUsers.Collection)
{
if (_user.Username == ir.UserName)
continue;
Expand All @@ -61,7 +70,7 @@ public async Task<ActionResult<ListOf<UserListViewModel>>> List([Range(1, 50)] i
}
}

return ResponseGet(irs);
return ResponseGet(users);
}

[HttpGet, Route("{username}")]
Expand Down
10 changes: 5 additions & 5 deletions src/Backend/Jp.Api.Management/Jp.Api.Management.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
<ItemGroup>
<PackageReference Include="Hellang.Middleware.ProblemDetails" Version="4.2.0" />
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
<PackageReference Include="JPProject.Admin.Application" Version="3.2.0" />
<PackageReference Include="JPProject.Admin.EntityFramework.Repository" Version="3.2.0" />
<PackageReference Include="JPProject.Sso.Application" Version="3.2.0" />
<PackageReference Include="JPProject.AspNet.Core" Version="3.2.0" />
<PackageReference Include="JPProject.Admin.Application" Version="3.2.1-prerelease1.03-05-061213" />
<PackageReference Include="JPProject.Admin.EntityFramework.Repository" Version="3.2.1-prerelease1.03-05-061213" />
<PackageReference Include="JPProject.Sso.Application" Version="3.2.1-prerelease1.03-05-061213" />
<PackageReference Include="JPProject.AspNet.Core" Version="3.2.1-prerelease1.03-05-061213" />
<PackageReference Include="JpProject.AspNetCore.PasswordHasher.Argon2" Version="3.0.2" />
<PackageReference Include="JPProject.Sso.EntityFramework.Repository" Version="3.2.0" />
<PackageReference Include="JPProject.Sso.EntityFramework.Repository" Version="3.2.1-prerelease1.03-05-061213" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.13.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.2" />
Expand Down
6 changes: 3 additions & 3 deletions src/Backend/Jp.Api.Management/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"ConnectionStrings": {
// Postgre Connection
"SSOConnection": "Server=localhost;Port=5432;Database=jpproject;User Id=postgres;Password=@Password1;"
//"SSOConnection": "Server=localhost;Port=5432;Database=jpproject;User Id=postgres;Password=@Password1;"
// Sql Server connection
//"SSOConnection": "Data Source=(LocalDb)\\MSSQLLocalDB;database=jpproject;trusted_connection=yes;"
"SSOConnection": "Data Source=(LocalDb)\\MSSQLLocalDB;database=jpproject;trusted_connection=yes;"
// MySql connection
//"SSOConnection": "server=localhost,port=3306;database=jpproject-new;user=bruno;password=10203040"
// SQLite Connection
Expand Down Expand Up @@ -32,7 +32,7 @@
"SendEmail": "false"
},
"ApplicationSettings": {
"DatabaseType": "MySql",
"DatabaseType": "SqlServer",
"Authority": "https://localhost:5000",
"Cors": "http://*.teste.work",
"UserManagementURL": "http://localhost:4200",
Expand Down
6 changes: 3 additions & 3 deletions src/Backend/Jp.Database/Jp.Database.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

<ItemGroup>
<PackageReference Include="IdentityServer4.EntityFramework" Version="3.1.2" />
<PackageReference Include="JPProject.Domain.Core" Version="3.2.0" />
<PackageReference Include="JPProject.Sso.AspNetIdentity" Version="3.2.0" />
<PackageReference Include="JPProject.Sso.EntityFramework.Repository" Version="3.2.0" />
<PackageReference Include="JPProject.Domain.Core" Version="3.2.1-prerelease1.03-05-061213" />
<PackageReference Include="JPProject.Sso.AspNetIdentity" Version="3.2.1-prerelease1.03-05-061213" />
<PackageReference Include="JPProject.Sso.EntityFramework.Repository" Version="3.2.1-prerelease1.03-05-061213" />
<PackageReference Include="Jwks.Manager.Store.EntityFrameworkCore" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.2" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using IdentityModel;
using Jp.UI.SSO.Util;
using JPProject.Domain.Core.Util;
using JPProject.Sso.AspNetIdentity.Models.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
Expand All @@ -23,21 +22,7 @@ protected override async Task<ClaimsIdentity> GenerateClaimsAsync(UserIdentity u
var claims = new List<Claim>();

claims.AddIfDontExist(new Claim(JwtClaimTypes.Name, user.UserName));

if (user.Birthdate.HasValue)
claims.AddIfDontExist(new Claim(JwtClaimTypes.BirthDate, user.Birthdate.Value.ToString("yyyy-MM-dd")));

if (user.Name.IsPresent())
claims.AddIfDontExist(new Claim(JwtClaimTypes.GivenName, user.Name));
else
claims.AddIfDontExist(new Claim(JwtClaimTypes.GivenName, user.UserName));

if (user.Picture.IsPresent())
claims.AddIfDontExist(new Claim(JwtClaimTypes.Picture, user.Picture));

if (user.SocialNumber.IsPresent())
claims.AddIfDontExist(new Claim("social_number", user.SocialNumber));

claims.AddIfDontExist(new Claim(JwtClaimTypes.GivenName, user.UserName));
var roles = await UserManager.GetRolesAsync(user);

if (identity.Claims.All(c => c.Type != JwtClaimTypes.Role))
Expand Down
19 changes: 1 addition & 18 deletions src/Frontend/Jp.UI.SSO/Configuration/SsoProfileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
using IdentityServer4.Models;
using IdentityServer4.Services;
using Jp.UI.SSO.Util;
using JPProject.Domain.Core.Util;
using JPProject.Sso.AspNetIdentity.Models.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using System;
using System.Globalization;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
Expand Down Expand Up @@ -41,22 +39,7 @@ public async Task GetProfileDataAsync(ProfileDataRequestContext context)
subjectClaims.Merge(claimsUser);
subjectClaims.AddIfDontExist(new Claim("username", user.UserName));

// Email is loaded default by IdentityServer4
//claims.Add(new Claim(JwtClaimTypes.Email, user.Email, ClaimValueTypes.Email));

if (user.Birthdate.HasValue)
subjectClaims.AddIfDontExist(new Claim(JwtClaimTypes.BirthDate, user.Birthdate.Value.ToString(CultureInfo.CurrentCulture), ClaimValueTypes.Date));

if (user.Name.IsPresent())
subjectClaims.AddIfDontExist(new Claim(JwtClaimTypes.Name, user.Name));
else
subjectClaims.AddIfDontExist(new Claim(JwtClaimTypes.Name, user.UserName));

if (user.Picture.IsPresent())
subjectClaims.AddIfDontExist(new Claim(JwtClaimTypes.Picture, user.Picture));

if (user.SocialNumber.IsPresent())
subjectClaims.AddIfDontExist(new Claim("social_number", user.SocialNumber));
subjectClaims.AddIfDontExist(new Claim(JwtClaimTypes.Name, user.UserName));

if (subjectClaims.All(a => a.Type != JwtClaimTypes.Role))
{
Expand Down
8 changes: 4 additions & 4 deletions src/Frontend/Jp.UI.SSO/Jp.UI.SSO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="JPProject.AspNet.Core" Version="3.2.0" />
<PackageReference Include="JPProject.AspNet.Core" Version="3.2.1-prerelease1.03-05-061213" />
<PackageReference Include="JpProject.AspNetCore.PasswordHasher.Argon2" Version="3.0.2" />
<PackageReference Include="JPProject.Sso.Application" Version="3.2.0" />
<PackageReference Include="JPProject.Sso.AspNetIdentity" Version="3.2.0" />
<PackageReference Include="JPProject.Sso.EntityFramework.Repository" Version="3.2.0" />
<PackageReference Include="JPProject.Sso.Application" Version="3.2.1-prerelease1.03-05-061213" />
<PackageReference Include="JPProject.Sso.AspNetIdentity" Version="3.2.1-prerelease1.03-05-061213" />
<PackageReference Include="JPProject.Sso.EntityFramework.Repository" Version="3.2.1-prerelease1.03-05-061213" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.13.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Facebook" Version="3.1.2" />
Expand Down
1 change: 0 additions & 1 deletion src/Frontend/Jp.UI.SSO/Util/DbMigrationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ private static async Task EnsureSeedGlobalConfigurationData(SsoContext context,

var user = new UserIdentity
{
Name = Users.GetUser(configuration),
UserName = Users.GetUser(configuration),
Email = Users.GetEmail(configuration),
EmailConfirmed = true,
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/Jp.UI.SSO/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//"SSOConnection": "Data Source=(LocalDb)\\MSSQLLocalDB;database=jpproject;trusted_connection=yes;",
"SSOConnection": "Server=.;Initial Catalog=JpProject;Persist Security Info=False;User ID=sa;Password=@Password1;MultipleActiveResultSets=False;Connection Timeout=30;"
// MySql Connection
//"SSOConnection": "server=localhost,port=3306;database=jpproject-new;user=bruno;password=10203040"
// "SSOConnection": "server=localhost,port=3306;database=jpproject-new;user=bruno;password=10203040"
// SQLite Connection
//"SSOConnection": "Data Source=jpproject.db"
},
Expand Down

0 comments on commit 488587f

Please sign in to comment.