mirror of
https://github.com/bitwarden/directory-connector
synced 2025-12-10 13:23:18 +00:00
set org when logging in
This commit is contained in:
@@ -111,6 +111,7 @@ namespace Bit.Console
|
|||||||
string email = null;
|
string email = null;
|
||||||
string masterPassword = null;
|
string masterPassword = null;
|
||||||
string token = null;
|
string token = null;
|
||||||
|
string orgId = null;
|
||||||
|
|
||||||
if(_usingArgs)
|
if(_usingArgs)
|
||||||
{
|
{
|
||||||
@@ -120,10 +121,14 @@ namespace Bit.Console
|
|||||||
email = parameters["e"];
|
email = parameters["e"];
|
||||||
masterPassword = parameters["p"];
|
masterPassword = parameters["p"];
|
||||||
}
|
}
|
||||||
if(parameters.Count == 3 && parameters.ContainsKey("t"))
|
if(parameters.Count >= 3 && parameters.ContainsKey("t"))
|
||||||
{
|
{
|
||||||
token = parameters["t"];
|
token = parameters["t"];
|
||||||
}
|
}
|
||||||
|
if(parameters.Count >= 3 && parameters.ContainsKey("o"))
|
||||||
|
{
|
||||||
|
orgId = parameters["o"];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -160,7 +165,44 @@ namespace Bit.Console
|
|||||||
Con.WriteLine("Two-step login is enabled on this account. Please enter your verification code.");
|
Con.WriteLine("Two-step login is enabled on this account. Please enter your verification code.");
|
||||||
Con.Write("Verification code: ");
|
Con.Write("Verification code: ");
|
||||||
token = Con.ReadLine().Trim();
|
token = Con.ReadLine().Trim();
|
||||||
result = await Core.Services.AuthService.Instance.LogInTwoFactorAsync(token, email, result.MasterPasswordHash);
|
result = await Core.Services.AuthService.Instance.LogInTwoFactorWithHashAsync(token, email,
|
||||||
|
result.MasterPasswordHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(result.Success && result.Organizations.Count > 1)
|
||||||
|
{
|
||||||
|
Organization org = null;
|
||||||
|
if(string.IsNullOrWhiteSpace(orgId))
|
||||||
|
{
|
||||||
|
org = result.Organizations.FirstOrDefault(o => o.Id == orgId);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Con.WriteLine();
|
||||||
|
Con.WriteLine();
|
||||||
|
for(int i = 0; i < result.Organizations.Count; i++)
|
||||||
|
{
|
||||||
|
Con.WriteLine("{0}. {1}", i + 1, result.Organizations[i].Name);
|
||||||
|
}
|
||||||
|
Con.Write("Select your organization: ");
|
||||||
|
var orgIndexInput = Con.ReadLine().Trim();
|
||||||
|
int orgIndex;
|
||||||
|
if(int.TryParse(orgIndexInput, out orgIndex))
|
||||||
|
{
|
||||||
|
org = result.Organizations[orgIndex];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(org == null)
|
||||||
|
{
|
||||||
|
result.Success = false;
|
||||||
|
result.ErrorMessage = "Organization not found.";
|
||||||
|
Core.Services.AuthService.Instance.LogOut();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Core.Services.SettingsService.Instance.Organization = org;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Con.WriteLine();
|
Con.WriteLine();
|
||||||
|
|||||||
@@ -55,6 +55,7 @@
|
|||||||
<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\ImportRequest.cs" />
|
||||||
|
<Compile Include="Models\Organization.cs" />
|
||||||
<Compile Include="Models\ProfileOrganizationResponse.cs" />
|
<Compile Include="Models\ProfileOrganizationResponse.cs" />
|
||||||
<Compile Include="Models\ServerConfiguration.cs" />
|
<Compile Include="Models\ServerConfiguration.cs" />
|
||||||
<Compile Include="Models\LoginResult.cs" />
|
<Compile Include="Models\LoginResult.cs" />
|
||||||
|
|||||||
@@ -12,5 +12,6 @@ namespace Bit.Core.Models
|
|||||||
public string ErrorMessage { get; set; }
|
public string ErrorMessage { get; set; }
|
||||||
public bool TwoFactorRequired { get; set; }
|
public bool TwoFactorRequired { get; set; }
|
||||||
public string MasterPasswordHash { get; set; }
|
public string MasterPasswordHash { get; set; }
|
||||||
|
public List<Organization> Organizations { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
20
src/Core/Models/Organization.cs
Normal file
20
src/Core/Models/Organization.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Bit.Core.Models
|
||||||
|
{
|
||||||
|
public class Organization
|
||||||
|
{
|
||||||
|
public Organization(ProfileOrganizationResponseModel org)
|
||||||
|
{
|
||||||
|
Name = org.Name;
|
||||||
|
Id = org.Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Id { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using Bit.Core.Models;
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.Models;
|
||||||
using Bit.Core.Utilities;
|
using Bit.Core.Utilities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -29,6 +30,7 @@ namespace Bit.Core.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
public bool Authenticated => !string.IsNullOrWhiteSpace(TokenService.Instance.AccessToken);
|
public bool Authenticated => !string.IsNullOrWhiteSpace(TokenService.Instance.AccessToken);
|
||||||
|
public bool OrganizationSet => SettingsService.Instance.Organization != null;
|
||||||
|
|
||||||
public void LogOut()
|
public void LogOut()
|
||||||
{
|
{
|
||||||
@@ -68,8 +70,7 @@ namespace Bit.Core.Services
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
await ProcessLogInSuccessAsync(response.Result);
|
return await ProcessLogInSuccessAsync(response.Result);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<LoginResult> LogInTwoFactorAsync(string token, string email, string masterPassword)
|
public async Task<LoginResult> LogInTwoFactorAsync(string token, string email, string masterPassword)
|
||||||
@@ -97,24 +98,54 @@ namespace Bit.Core.Services
|
|||||||
|
|
||||||
var response = await ApiService.Instance.PostTokenAsync(request);
|
var response = await ApiService.Instance.PostTokenAsync(request);
|
||||||
|
|
||||||
var result = new LoginResult();
|
|
||||||
if(!response.Succeeded)
|
if(!response.Succeeded)
|
||||||
{
|
{
|
||||||
|
var result = new LoginResult();
|
||||||
result.Success = false;
|
result.Success = false;
|
||||||
result.ErrorMessage = response.Errors.FirstOrDefault()?.Message;
|
result.ErrorMessage = response.Errors.FirstOrDefault()?.Message;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.Success = true;
|
return await ProcessLogInSuccessAsync(response.Result);
|
||||||
await ProcessLogInSuccessAsync(response.Result);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task ProcessLogInSuccessAsync(TokenResponse response)
|
private async Task<LoginResult> ProcessLogInSuccessAsync(TokenResponse response)
|
||||||
{
|
{
|
||||||
TokenService.Instance.AccessToken = response.AccessToken;
|
TokenService.Instance.AccessToken = response.AccessToken;
|
||||||
TokenService.Instance.RefreshToken = response.RefreshToken;
|
TokenService.Instance.RefreshToken = response.RefreshToken;
|
||||||
return Task.FromResult(0);
|
|
||||||
|
var result = new LoginResult();
|
||||||
|
|
||||||
|
var profile = await ApiService.Instance.GetProfileAsync();
|
||||||
|
if(profile.Succeeded)
|
||||||
|
{
|
||||||
|
var adminOrgs = profile.Result.Organizations.Where(o =>
|
||||||
|
o.Status == OrganizationUserStatusType.Confirmed &&
|
||||||
|
o.Type != OrganizationUserType.User);
|
||||||
|
if(!adminOrgs.Any())
|
||||||
|
{
|
||||||
|
LogOut();
|
||||||
|
result.Success = false;
|
||||||
|
result.ErrorMessage = "You are not an admin of any organizations.";
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Organizations = adminOrgs.Select(o => new Organization(o)).ToList();
|
||||||
|
if(result.Organizations.Count == 1)
|
||||||
|
{
|
||||||
|
SettingsService.Instance.Organization = new Organization(adminOrgs.First());
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Success = true;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LogOut();
|
||||||
|
result.Success = false;
|
||||||
|
result.ErrorMessage = "Could not load profile.";
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,6 +127,19 @@ namespace Bit.Core.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Organization Organization
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return Settings.Organization;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
Settings.Organization = value;
|
||||||
|
SaveSettings();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public ServerConfiguration Server
|
public ServerConfiguration Server
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -147,6 +160,7 @@ namespace Bit.Core.Services
|
|||||||
public EncryptedData AccessToken { get; set; }
|
public EncryptedData AccessToken { get; set; }
|
||||||
public EncryptedData RefreshToken { get; set; }
|
public EncryptedData RefreshToken { get; set; }
|
||||||
public ServerConfiguration Server { get; set; }
|
public ServerConfiguration Server { get; set; }
|
||||||
|
public Organization Organization { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user