mirror of
https://github.com/bitwarden/directory-connector
synced 2025-12-10 13:23:18 +00:00
import api
This commit is contained in:
@@ -52,6 +52,7 @@
|
|||||||
<Compile Include="Models\ApiError.cs" />
|
<Compile Include="Models\ApiError.cs" />
|
||||||
<Compile Include="Models\ApiResult.cs" />
|
<Compile Include="Models\ApiResult.cs" />
|
||||||
<Compile Include="Models\Entry.cs" />
|
<Compile Include="Models\Entry.cs" />
|
||||||
|
<Compile Include="Models\ImportRequest.cs" />
|
||||||
<Compile Include="Models\ServerConfiguration.cs" />
|
<Compile Include="Models\ServerConfiguration.cs" />
|
||||||
<Compile Include="Models\LoginResult.cs" />
|
<Compile Include="Models\LoginResult.cs" />
|
||||||
<Compile Include="Models\ErrorResponse.cs" />
|
<Compile Include="Models\ErrorResponse.cs" />
|
||||||
|
|||||||
42
src/Core/Models/ImportRequest.cs
Normal file
42
src/Core/Models/ImportRequest.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Bit.Core.Models
|
||||||
|
{
|
||||||
|
public class ImportRequest
|
||||||
|
{
|
||||||
|
public ImportRequest(List<GroupEntry> groups, List<UserEntry> users)
|
||||||
|
{
|
||||||
|
Groups = groups.Select(g => new Group(g)).ToArray();
|
||||||
|
Users = users.Select(u => new User(u)).ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Group[] Groups { get; set; }
|
||||||
|
public User[] Users { get; set; }
|
||||||
|
|
||||||
|
public class Group
|
||||||
|
{
|
||||||
|
public Group(GroupEntry entry)
|
||||||
|
{
|
||||||
|
Name = entry.Name;
|
||||||
|
ExternalId = entry.DistinguishedName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string ExternalId { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class User
|
||||||
|
{
|
||||||
|
public User(UserEntry entry)
|
||||||
|
{
|
||||||
|
Email = entry.Email;
|
||||||
|
ExternalGroupIds = entry.Groups;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Email { get; set; }
|
||||||
|
public IEnumerable<string> ExternalGroupIds { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Bit.Core.Services
|
namespace Bit.Core.Services
|
||||||
@@ -85,6 +86,40 @@ namespace Bit.Core.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual async Task<ApiResult> PostImportAsync(ImportRequest requestObj)
|
||||||
|
{
|
||||||
|
var tokenStateResponse = await HandleTokenStateAsync();
|
||||||
|
if(!tokenStateResponse.Succeeded)
|
||||||
|
{
|
||||||
|
return tokenStateResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
var stringContent = JsonConvert.SerializeObject(requestObj);
|
||||||
|
var requestMessage = new HttpRequestMessage
|
||||||
|
{
|
||||||
|
Method = HttpMethod.Post,
|
||||||
|
RequestUri = new Uri(ApiClient.BaseAddress, "import"), // TODO: org id
|
||||||
|
Content = new StringContent(stringContent, Encoding.UTF8, "application/json"),
|
||||||
|
};
|
||||||
|
|
||||||
|
requestMessage.Headers.Add("Authorization", $"Bearer3 {TokenService.Instance.AccessToken}");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var response = await ApiClient.SendAsync(requestMessage).ConfigureAwait(false);
|
||||||
|
if(!response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
return await HandleErrorAsync(response).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ApiResult.Success(response.StatusCode);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return HandledWebException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected ApiResult HandledWebException()
|
protected ApiResult HandledWebException()
|
||||||
{
|
{
|
||||||
return ApiResult.Failed(HttpStatusCode.BadGateway,
|
return ApiResult.Failed(HttpStatusCode.BadGateway,
|
||||||
|
|||||||
@@ -1,36 +1,56 @@
|
|||||||
using Bit.Core.Models;
|
using Bit.Core.Models;
|
||||||
|
using Bit.Core.Services;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.DirectoryServices;
|
using System.DirectoryServices;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Bit.Core.Utilities
|
namespace Bit.Core.Utilities
|
||||||
{
|
{
|
||||||
public static class Sync
|
public static class Sync
|
||||||
{
|
{
|
||||||
public static Task<List<GroupEntry>> SyncGroupsAsync()
|
public static async Task SyncAllAsync()
|
||||||
{
|
{
|
||||||
if(!Services.SettingsService.Instance.Server.SyncGroups)
|
List<GroupEntry> groups = null;
|
||||||
|
if(SettingsService.Instance.Server.SyncGroups)
|
||||||
|
{
|
||||||
|
groups = await GetGroupsAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<UserEntry> users = null;
|
||||||
|
if(SettingsService.Instance.Server.SyncUsers)
|
||||||
|
{
|
||||||
|
users = await GetUsersAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
FlattenGroupsToUsers(groups, null, groups, users);
|
||||||
|
|
||||||
|
var request = new ImportRequest(groups, users);
|
||||||
|
await ApiService.Instance.PostImportAsync(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Task<List<GroupEntry>> GetGroupsAsync()
|
||||||
|
{
|
||||||
|
if(!SettingsService.Instance.Server.SyncGroups)
|
||||||
{
|
{
|
||||||
throw new ApplicationException("Not configured to sync groups.");
|
throw new ApplicationException("Not configured to sync groups.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Services.SettingsService.Instance.Server == null)
|
if(SettingsService.Instance.Server == null)
|
||||||
{
|
{
|
||||||
throw new ApplicationException("No configuration for directory server.");
|
throw new ApplicationException("No configuration for directory server.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!Services.AuthService.Instance.Authenticated)
|
if(!AuthService.Instance.Authenticated)
|
||||||
{
|
{
|
||||||
throw new ApplicationException("Not authenticated.");
|
throw new ApplicationException("Not authenticated.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var entry = Services.SettingsService.Instance.Server.GetDirectoryEntry();
|
var entry = SettingsService.Instance.Server.GetDirectoryEntry();
|
||||||
var filter = string.IsNullOrWhiteSpace(Services.SettingsService.Instance.Server.GroupFilter) ? null :
|
var filter = string.IsNullOrWhiteSpace(SettingsService.Instance.Server.GroupFilter) ? null :
|
||||||
Services.SettingsService.Instance.Server.GroupFilter;
|
SettingsService.Instance.Server.GroupFilter;
|
||||||
var searcher = new DirectorySearcher(entry, filter);
|
var searcher = new DirectorySearcher(entry, filter);
|
||||||
var result = searcher.FindAll();
|
var result = searcher.FindAll();
|
||||||
|
|
||||||
@@ -48,10 +68,10 @@ namespace Bit.Core.Utilities
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Name
|
// Name
|
||||||
if(item.Properties.Contains(Services.SettingsService.Instance.Server.GroupNameAttribute) &&
|
if(item.Properties.Contains(SettingsService.Instance.Server.GroupNameAttribute) &&
|
||||||
item.Properties[Services.SettingsService.Instance.Server.GroupNameAttribute].Count > 0)
|
item.Properties[SettingsService.Instance.Server.GroupNameAttribute].Count > 0)
|
||||||
{
|
{
|
||||||
group.Name = item.Properties[Services.SettingsService.Instance.Server.GroupNameAttribute][0].ToString();
|
group.Name = item.Properties[SettingsService.Instance.Server.GroupNameAttribute][0].ToString();
|
||||||
}
|
}
|
||||||
else if(item.Properties.Contains("cn") && item.Properties["cn"].Count > 0)
|
else if(item.Properties.Contains("cn") && item.Properties["cn"].Count > 0)
|
||||||
{
|
{
|
||||||
@@ -63,14 +83,14 @@ namespace Bit.Core.Utilities
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Dates
|
// Dates
|
||||||
group.CreationDate = ParseDate(item.Properties, Services.SettingsService.Instance.Server.CreationDateAttribute);
|
group.CreationDate = ParseDate(item.Properties, SettingsService.Instance.Server.CreationDateAttribute);
|
||||||
group.RevisionDate = ParseDate(item.Properties, Services.SettingsService.Instance.Server.RevisionDateAttribute);
|
group.RevisionDate = ParseDate(item.Properties, SettingsService.Instance.Server.RevisionDateAttribute);
|
||||||
|
|
||||||
// Members
|
// Members
|
||||||
if(item.Properties.Contains(Services.SettingsService.Instance.Server.MemberAttribute) &&
|
if(item.Properties.Contains(SettingsService.Instance.Server.MemberAttribute) &&
|
||||||
item.Properties[Services.SettingsService.Instance.Server.MemberAttribute].Count > 0)
|
item.Properties[SettingsService.Instance.Server.MemberAttribute].Count > 0)
|
||||||
{
|
{
|
||||||
foreach(var member in item.Properties[Services.SettingsService.Instance.Server.MemberAttribute])
|
foreach(var member in item.Properties[SettingsService.Instance.Server.MemberAttribute])
|
||||||
{
|
{
|
||||||
var memberDn = member.ToString();
|
var memberDn = member.ToString();
|
||||||
if(!group.Members.Contains(memberDn))
|
if(!group.Members.Contains(memberDn))
|
||||||
@@ -86,26 +106,26 @@ namespace Bit.Core.Utilities
|
|||||||
return Task.FromResult(groups);
|
return Task.FromResult(groups);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<List<UserEntry>> SyncUsersAsync()
|
private static Task<List<UserEntry>> GetUsersAsync()
|
||||||
{
|
{
|
||||||
if(!Services.SettingsService.Instance.Server.SyncUsers)
|
if(!SettingsService.Instance.Server.SyncUsers)
|
||||||
{
|
{
|
||||||
throw new ApplicationException("Not configured to sync users.");
|
throw new ApplicationException("Not configured to sync users.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Services.SettingsService.Instance.Server == null)
|
if(SettingsService.Instance.Server == null)
|
||||||
{
|
{
|
||||||
throw new ApplicationException("No configuration for directory server.");
|
throw new ApplicationException("No configuration for directory server.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!Services.AuthService.Instance.Authenticated)
|
if(!AuthService.Instance.Authenticated)
|
||||||
{
|
{
|
||||||
throw new ApplicationException("Not authenticated.");
|
throw new ApplicationException("Not authenticated.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var entry = Services.SettingsService.Instance.Server.GetDirectoryEntry();
|
var entry = SettingsService.Instance.Server.GetDirectoryEntry();
|
||||||
var filter = string.IsNullOrWhiteSpace(Services.SettingsService.Instance.Server.UserFilter) ? null :
|
var filter = string.IsNullOrWhiteSpace(SettingsService.Instance.Server.UserFilter) ? null :
|
||||||
Services.SettingsService.Instance.Server.UserFilter;
|
SettingsService.Instance.Server.UserFilter;
|
||||||
var searcher = new DirectorySearcher(entry, filter);
|
var searcher = new DirectorySearcher(entry, filter);
|
||||||
var result = searcher.FindAll();
|
var result = searcher.FindAll();
|
||||||
|
|
||||||
@@ -123,19 +143,19 @@ namespace Bit.Core.Utilities
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Email
|
// Email
|
||||||
if(Services.SettingsService.Instance.Server.EmailPrefixSuffix &&
|
if(SettingsService.Instance.Server.EmailPrefixSuffix &&
|
||||||
item.Properties.Contains(Services.SettingsService.Instance.Server.UserEmailPrefixAttribute) &&
|
item.Properties.Contains(SettingsService.Instance.Server.UserEmailPrefixAttribute) &&
|
||||||
item.Properties[Services.SettingsService.Instance.Server.UserEmailPrefixAttribute].Count > 0 &&
|
item.Properties[SettingsService.Instance.Server.UserEmailPrefixAttribute].Count > 0 &&
|
||||||
!string.IsNullOrWhiteSpace(Services.SettingsService.Instance.Server.UserEmailSuffix))
|
!string.IsNullOrWhiteSpace(SettingsService.Instance.Server.UserEmailSuffix))
|
||||||
{
|
{
|
||||||
user.Email = string.Concat(
|
user.Email = string.Concat(
|
||||||
item.Properties[Services.SettingsService.Instance.Server.UserEmailPrefixAttribute][0].ToString(),
|
item.Properties[SettingsService.Instance.Server.UserEmailPrefixAttribute][0].ToString(),
|
||||||
Services.SettingsService.Instance.Server.UserEmailSuffix).ToLowerInvariant();
|
SettingsService.Instance.Server.UserEmailSuffix).ToLowerInvariant();
|
||||||
}
|
}
|
||||||
else if(item.Properties.Contains(Services.SettingsService.Instance.Server.UserEmailAttribute) &&
|
else if(item.Properties.Contains(SettingsService.Instance.Server.UserEmailAttribute) &&
|
||||||
item.Properties[Services.SettingsService.Instance.Server.UserEmailAttribute].Count > 0)
|
item.Properties[SettingsService.Instance.Server.UserEmailAttribute].Count > 0)
|
||||||
{
|
{
|
||||||
user.Email = item.Properties[Services.SettingsService.Instance.Server.UserEmailAttribute][0]
|
user.Email = item.Properties[SettingsService.Instance.Server.UserEmailAttribute][0]
|
||||||
.ToString()
|
.ToString()
|
||||||
.ToLowerInvariant();
|
.ToLowerInvariant();
|
||||||
}
|
}
|
||||||
@@ -145,8 +165,8 @@ namespace Bit.Core.Utilities
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Dates
|
// Dates
|
||||||
user.CreationDate = ParseDate(item.Properties, Services.SettingsService.Instance.Server.CreationDateAttribute);
|
user.CreationDate = ParseDate(item.Properties, SettingsService.Instance.Server.CreationDateAttribute);
|
||||||
user.RevisionDate = ParseDate(item.Properties, Services.SettingsService.Instance.Server.RevisionDateAttribute);
|
user.RevisionDate = ParseDate(item.Properties, SettingsService.Instance.Server.RevisionDateAttribute);
|
||||||
|
|
||||||
users.Add(user);
|
users.Add(user);
|
||||||
}
|
}
|
||||||
@@ -154,24 +174,7 @@ namespace Bit.Core.Utilities
|
|||||||
return Task.FromResult(users);
|
return Task.FromResult(users);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task SyncAllAsync()
|
private static void FlattenGroupsToUsers(List<GroupEntry> currentGroups, List<UserEntry> currentGroupsUsers,
|
||||||
{
|
|
||||||
List<GroupEntry> groups = null;
|
|
||||||
if(Services.SettingsService.Instance.Server.SyncGroups)
|
|
||||||
{
|
|
||||||
groups = await SyncGroupsAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
List<UserEntry> users = null;
|
|
||||||
if(Services.SettingsService.Instance.Server.SyncUsers)
|
|
||||||
{
|
|
||||||
users = await SyncUsersAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
FlattenGroupsToUsers(groups, null, groups, users);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void FlattenGroupsToUsers(List<GroupEntry> currentGroups, List<UserEntry> currentGroupsUsers,
|
|
||||||
List<GroupEntry> allGroups, List<UserEntry> allUsers)
|
List<GroupEntry> allGroups, List<UserEntry> allUsers)
|
||||||
{
|
{
|
||||||
foreach(var group in currentGroups)
|
foreach(var group in currentGroups)
|
||||||
|
|||||||
Reference in New Issue
Block a user