-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* README.md * bug fix while migrating database * LDAP feature
- Loading branch information
1 parent
8a4dd7f
commit 0c954c5
Showing
15 changed files
with
385 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Backend/Jp.Api.Management/ViewModel/LdapSettingsTestQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using JPProject.Sso.Domain.ViewModels.Settings; | ||
|
||
namespace Jp.Api.Management.ViewModel | ||
{ | ||
public class LdapSettingsTestQuery | ||
{ | ||
public string Username { get; set; } | ||
public string Password { get; set; } | ||
public string DomainName { get; set; } | ||
public string DistinguishedName { get; set; } | ||
public string Attributes { get; set; } | ||
public string AuthType { get; set; } | ||
public string SearchScope { get; set; } | ||
public LdapSettings Get() | ||
{ | ||
return new LdapSettings(DomainName, DistinguishedName, Attributes, AuthType, SearchScope); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="JPProject.Sso.Application" Version="3.2.7" /> | ||
<PackageReference Include="Microsoft.Windows.Compatibility" Version="3.1.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using JPProject.Domain.Core.Util; | ||
using JPProject.Sso.Application.ViewModels.UserViewModels; | ||
using JPProject.Sso.Domain.ViewModels.Settings; | ||
using System; | ||
using System.DirectoryServices.Protocols; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Security.Claims; | ||
|
||
namespace Jp.Ldap | ||
{ | ||
|
||
public class LdapAuthentication | ||
{ | ||
private readonly LdapSettings _settings; | ||
|
||
public LdapAuthentication(LdapSettings settings) | ||
{ | ||
_settings = settings; | ||
} | ||
|
||
public UserViewModel Login(string username, string password) | ||
{ | ||
var credential = new NetworkCredential(username, password); | ||
//var dcName = "brunobrito"; | ||
//var dcSufix = "net"; | ||
|
||
using var connection = new LdapConnection(_settings.DomainName); | ||
if (_settings.AuthType.IsPresent() && Enum.TryParse(typeof(AuthType), _settings.AuthType, out _)) | ||
connection.AuthType = Enum.Parse<AuthType>(_settings.AuthType); | ||
|
||
connection.Bind(credential); | ||
//var _userStore = $"dc={dcName},dc={dcSufix}"; | ||
var searchFilter = $"(SAMAccountName={username})"; | ||
|
||
var attributes = _settings.Attributes?.Trim() == "" ? null : _settings.Attributes?.Split(",").Select(s => s.Trim()); | ||
var searchRequest = new SearchRequest | ||
( | ||
_settings.DistinguishedName, | ||
searchFilter, | ||
Enum.Parse<SearchScope>(_settings.SearchScope), | ||
attributes?.ToArray() | ||
//new string[] { "displayName", "cn", "mail" } | ||
); | ||
// null pega todos os campos | ||
var response = (SearchResponse)connection.SendRequest(searchRequest); | ||
var uservm = new UserViewModel() { UserName = username, Name = username}; | ||
foreach (SearchResultEntry entry in response.Entries) | ||
{ | ||
foreach (DirectoryAttribute dirAttr in entry.Attributes.Values) | ||
{ | ||
uservm.CustomClaims.Add(new Claim(dirAttr.Name, dirAttr[0].ToString())); | ||
Console.WriteLine($"Attribute={dirAttr.Name},Value={dirAttr[0]}"); | ||
} | ||
} | ||
|
||
return uservm; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using JPProject.Domain.Core.ViewModels; | ||
using System.Collections.Generic; | ||
|
||
namespace Jp.Ldap | ||
{ | ||
public class LdapConnectionResult | ||
{ | ||
public bool Success { get; } | ||
public List<ClaimViewModel> Claims { get; } | ||
public string Error { get; } | ||
public string Step { get; } | ||
|
||
public LdapConnectionResult(bool success, string error, string step) | ||
{ | ||
Success = success; | ||
Error = error; | ||
Step = step; | ||
} | ||
|
||
public LdapConnectionResult(bool success, List<ClaimViewModel> claims) | ||
{ | ||
Success = success; | ||
Claims = claims; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using JPProject.Domain.Core.Util; | ||
using JPProject.Domain.Core.ViewModels; | ||
using JPProject.Sso.Domain.ViewModels.Settings; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.DirectoryServices.Protocols; | ||
using System.Linq; | ||
using System.Net; | ||
|
||
namespace Jp.Ldap | ||
{ | ||
public class LdapTestConnection | ||
{ | ||
private readonly LdapSettings _settings; | ||
|
||
public LdapTestConnection(LdapSettings settings) | ||
{ | ||
_settings = settings; | ||
} | ||
|
||
public LdapConnectionResult Test(string username, string password) | ||
{ | ||
var credential = new NetworkCredential(username, password); | ||
//var dcName = "brunobrito"; | ||
//var dcSufix = "net"; | ||
LdapConnection connection; | ||
|
||
try | ||
{ | ||
connection = new LdapConnection(_settings.DomainName); | ||
if (_settings.AuthType.IsPresent() && Enum.TryParse(typeof(AuthType), _settings.AuthType, out _)) | ||
connection.AuthType = Enum.Parse<AuthType>(_settings.AuthType); | ||
|
||
connection.Bind(credential); | ||
} | ||
catch (Exception e) | ||
{ | ||
return new LdapConnectionResult(false, e.Message, "Login"); | ||
} | ||
|
||
|
||
//var _userStore = $"dc={dcName},dc={dcSufix}"; | ||
var searchFilter = $"(SAMAccountName={username})"; | ||
|
||
var attributes = _settings.Attributes?.Trim() == "" ? null : _settings.Attributes?.Split(",").Select(s => s.Trim()); | ||
var searchRequest = new SearchRequest | ||
( | ||
_settings.DistinguishedName, | ||
searchFilter, | ||
Enum.Parse<SearchScope>(_settings.SearchScope), | ||
attributes?.ToArray() | ||
//new string[] { "displayName", "cn", "mail" } | ||
); | ||
|
||
var claims = new List<ClaimViewModel>(); | ||
try | ||
{ | ||
var response = (SearchResponse)connection.SendRequest(searchRequest); | ||
foreach (SearchResultEntry entry in response.Entries) | ||
{ | ||
foreach (DirectoryAttribute dirAttr in entry.Attributes.Values) | ||
{ | ||
claims.Add(new ClaimViewModel(dirAttr.Name, dirAttr[0].ToString())); | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
return new LdapConnectionResult(false, e.Message, "Search Error"); | ||
} | ||
|
||
return new LdapConnectionResult(true, claims.OrderBy(b => b.Type).ToList()); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.