Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mfa required auth flow #686

Merged
merged 11 commits into from
Jul 1, 2024
93 changes: 92 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,95 @@
[*.cs]

# IDE0011: Add braces
csharp_prefer_braces = false:silent
csharp_prefer_braces = false:silent
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent
csharp_indent_labels = one_less_than_current

[*.{cs,vb}]
#### Naming styles ####

# Naming rules

dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i

dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

# Symbol specifications

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =

# Naming styles

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_object_initializer = true:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
indent_size = 4
end_of_line = crlf
dotnet_style_collection_initializer = true:suggestion
indent_style = tab
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion
dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion
dotnet_style_namespace_match_folder = true:suggestion
dotnet_style_readonly_field = true:suggestion
dotnet_style_predefined_type_for_locals_parameters_members = true:silent
dotnet_style_predefined_type_for_member_access = true:silent
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
dotnet_style_allow_multiple_blank_lines_experimental = true:silent
dotnet_style_allow_statement_immediately_after_block_experimental = true:silent
dotnet_code_quality_unused_parameters = all:suggestion
10 changes: 6 additions & 4 deletions DesktopEdge/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
<Image x:Name="InfoBlurb" Visibility="Collapsed" Source="/Assets/Images/Warning.png" Grid.Column="0" Width="30" Grid.Row="1" Height="30" Margin="0,0,0,0"></Image>
<Label x:Name="Blurb" Content="An Update is available!" MouseUp="BlurbAction" Grid.Column="1" Padding="0,0,0,0" Grid.Row="1" Margin="0,0,0,0" FontFamily="pack://application:,,,/Assets/Fonts/#Open Sans" Foreground="#000000" FontSize="12" Cursor="Hand" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<Image Source="/Assets/Images/closeBlack.png" Grid.Column="2" Width="10" Height="10" Grid.Row="1" Margin="0,0,0,0" Cursor="Hand" MouseUp="DoHideBlurb"></Image>
</Grid>
</Grid>
</Grid>

<Grid x:Name="NoServiceView" Visibility="Collapsed" Margin="10,10,10,10">
Expand Down Expand Up @@ -314,12 +314,14 @@
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="10"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Label x:Name="LoadingTitle" Foreground="White" Content="Loading" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" FontFamily="pack://application:,,,/Assets/Fonts/#Open Sans"/>
<TextBlock x:Name="LoadingDetails" TextWrapping="WrapWithOverflow" Foreground="LightGray" Padding="10" Text="${x:DefaultLoadText}" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" FontSize="14" FontFamily="pack://application:,,,/Assets/Fonts/#Open Sans"/>
<ProgressBar x:Name="LoadProgress" Width="100" Grid.Row="3" IsEnabled="True" />
<!-- Label x:Name="TimerLabel" Foreground="White" Content="05:00" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="16"/ -->
<TextBlock x:Name="LoadingDetails" TextWrapping="WrapWithOverflow" Foreground="LightGray" Padding="10" Text="${x:DefaultLoadText}" Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" FontSize="14" FontFamily="pack://application:,,,/Assets/Fonts/#Open Sans"/>
<ProgressBar x:Name="LoadProgress" Width="100" Grid.Row="4" IsEnabled="True" />
</Grid>
</Grid>

Expand Down Expand Up @@ -373,4 +375,4 @@
<Window.InputBindings>
<KeyBinding x:Name="ShowDebug" Command="{Binding SomeCommand}" Key="F12" />
</Window.InputBindings>
</Window>
</Window>
65 changes: 48 additions & 17 deletions DesktopEdge/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ limitations under the License.
using Windows.Data.Xml.Dom;
using Ziti.Desktop.Edge.Models;
using System.Reflection;
using System.Windows.Threading;

namespace ZitiDesktopEdge {

Expand Down Expand Up @@ -144,8 +145,9 @@ private void ServiceClient_OnMfaEvent(object sender, MfaEvent mfa) {
if (identities[i].Identifier == mfa.Identifier) {
identities[i].WasNotified = false;
identities[i].WasFullNotified = false;
identities[i].IsMFAEnabled = true;
identities[i].IsAuthenticated = false;
//identities[i].IsMFAEnabled = true;
identities[i].IsMFANeeded = true;
identities[i].ShowMFA = false;
identities[i].IsTimingOut = false;
break;
}
Expand All @@ -157,15 +159,16 @@ private void ServiceClient_OnMfaEvent(object sender, MfaEvent mfa) {
if (identities[i].Identifier == mfa.Identifier) {
identities[i].WasNotified = false;
identities[i].WasFullNotified = false;
identities[i].IsMFAEnabled = mfa.Successful;
identities[i].IsAuthenticated = mfa.Successful;
//identities[i].IsMFAEnabled = mfa.Successful;
identities[i].ShowMFA = mfa.Successful;
identities[i].IsTimingOut = false;
identities[i].LastUpdatedTime = DateTime.Now;
for (int j = 0; j < identities[i].Services.Count; j++) {
identities[i].Services[j].TimeUpdated = DateTime.Now;
identities[i].Services[j].TimeoutRemaining = identities[i].Services[j].Timeout;
}
found = identities[i];
found.IsMFAEnabled = true;
break;
}
}
Expand All @@ -182,7 +185,7 @@ private void ServiceClient_OnMfaEvent(object sender, MfaEvent mfa) {
identities[i].WasNotified = false;
identities[i].WasFullNotified = false;
identities[i].IsMFAEnabled = false;
identities[i].IsAuthenticated = false;
identities[i].ShowMFA = false;
identities[i].LastUpdatedTime = DateTime.Now;
identities[i].IsTimingOut = false;
for (int j = 0; j < identities[i].Services.Count; j++) {
Expand All @@ -205,7 +208,7 @@ private void ServiceClient_OnMfaEvent(object sender, MfaEvent mfa) {
identities[i].WasNotified = false;
identities[i].WasFullNotified = false;
identities[i].IsTimingOut = false;
identities[i].IsAuthenticated = mfa.Successful;
identities[i].ShowMFA = mfa.Successful;
identities[i].LastUpdatedTime = DateTime.Now;
for (int j = 0; j < identities[i].Services.Count; j++) {
identities[i].Services[j].TimeUpdated = DateTime.Now;
Expand Down Expand Up @@ -279,7 +282,7 @@ private void Animatin_Completed(object sender, EventArgs e) {
/// <param name="identity">The Ziti Identity to Authenticate</param>
async public void ShowMFARecoveryCodes(ZitiIdentity identity) {
if (identity.IsMFAEnabled) {
if (identity.IsAuthenticated && identity.RecoveryCodes != null) {
if (identity.ShowMFA && identity.RecoveryCodes != null) {
MFASetup.Opacity = 0;
MFASetup.Visibility = Visibility.Visible;
MFASetup.Margin = new Thickness(0, 0, 0, 0);
Expand Down Expand Up @@ -722,7 +725,7 @@ private void ServiceClient_OnNotificationEvent(object sender, NotificationEvent
found.MinTimeout = notification.MfaMinimumTimeout;

if (notification.MfaMinimumTimeout == 0) {
// found.MFAInfo.IsAuthenticated = false;
// found.MFAInfo.ShowMFA = false;
// display mfa token icon
displayMFARequired = true;
} else {
Expand All @@ -738,7 +741,7 @@ private void ServiceClient_OnNotificationEvent(object sender, NotificationEvent
}
}

// we may need to display mfa icon, based on the timer in UI, remove found.MFAInfo.IsAuthenticated setting in this function.
// we may need to display mfa icon, based on the timer in UI, remove found.MFAInfo.ShowMFA setting in this function.
// the below function can show mfa icon even after user authenticates successfully, in race conditions
if (displayMFARequired || displayMFATimout) {
this.Dispatcher.Invoke(() => {
Expand Down Expand Up @@ -782,7 +785,7 @@ private void ServiceClient_OnControllerEvent(object sender, ControllerEvent e) {

string nextVersionStr = null;
private void MonitorClient_OnReconnectFailure(object sender, object e) {
logger.Debug("OnReconnectFailure triggered");
//logger.Debug("OnReconnectFailure triggered");
if (nextVersionStr == null) {
// check for the current version
nextVersionStr = "checking for update";
Expand Down Expand Up @@ -1082,7 +1085,7 @@ private void ServiceClient_OnIdentityEvent(object sender, IdentityEvent e) {
if (zid.ContollerVersion != null && zid.ContollerVersion.Length > 0) found.ContollerVersion = zid.ContollerVersion;
found.IsEnabled = zid.IsEnabled;
found.IsMFAEnabled = e.Id.MfaEnabled;
found.IsAuthenticated = !e.Id.MfaNeeded;
//xx questionable found.ShowMFA = !e.Id.MfaNeeded;
found.IsConnected = true;
for (int i = 0; i < identities.Count; i++) {
if (identities[i].Identifier == found.Identifier) {
Expand Down Expand Up @@ -1128,7 +1131,7 @@ private void ServiceClient_OnMetricsEvent(object sender, List<Identity> ids) {
long totalUp = 0;
long totalDown = 0;
foreach (var id in ids) {
//logger.Debug($"==== MetricsEvent : id {id.Name} down: {id.Metrics.Down} up:{id.Metrics.Up}");
//logger.Debug($"==== MetricsEvent : id {id.Name} down: {id.Metrics.Down} up:{id.Metrics.Up}");
if (id?.Metrics != null) {
totalDown += id.Metrics.Down;
totalUp += id.Metrics.Up;
Expand All @@ -1154,7 +1157,7 @@ public void SetSpeed(decimal bytes, Label speed, Label speedLabel) {
private void ServiceClient_OnServiceEvent(object sender, ServiceEvent e) {
if (e == null) return;

logger.Debug($"==== ServiceEvent : action:{e.Action} identifier:{e.Identifier} name:{e.Service.Name} ");
logger.Debug($"==== ServiceEvent : action:{e.Action} identifier:{e.Identifier} name:{e.Service.Name} ");
var found = identities.Find(id => id.Identifier == e.Identifier);
if (found == null) {
logger.Debug($"{e.Action} service event for {e.Service.Name} but the provided identity identifier {e.Identifier} is not found!");
Expand Down Expand Up @@ -1184,7 +1187,7 @@ private void addService(ZitiIdentity found, Service added) {
if (zs.HasFailingPostureCheck()) {
found.HasServiceFailingPostureCheck = true;
if (zs.PostureChecks.Any(p => !p.IsPassing && p.QueryType == "MFA")) {
found.IsAuthenticated = false;
found.ShowMFA = false;
}
}
} else {
Expand Down Expand Up @@ -1334,7 +1337,7 @@ private bool IsTimingOut() {
private bool IsTimedOut() {
if (identities != null) {
for (int i = 0; i < identities.Count; i++) {
if (identities[i].IsMFAEnabled && !identities[i].IsAuthenticated) return true;
if (identities[i].IsMFANeeded && !identities[i].ShowMFA) return true;
}
}
return false;
Expand Down Expand Up @@ -1386,8 +1389,11 @@ private void LoadIdentities(Boolean repaint) {
IdentityItem idItem = new IdentityItem();

idItem.ToggleStatus.IsEnabled = id.IsEnabled;
if (id.IsEnabled) idItem.ToggleStatus.Content = "ENABLED";
else idItem.ToggleStatus.Content = "DISABLED";
if (id.IsEnabled) {
idItem.ToggleStatus.Content = "ENABLED";
} else {
idItem.ToggleStatus.Content = "DISABLED";
}

idItem.Authenticate += IdItem_Authenticate;
idItem.OnStatusChanged += Id_OnStatusChanged;
Expand Down Expand Up @@ -1618,14 +1624,39 @@ async private void Disconnect(object sender, RoutedEventArgs e) {

internal void ShowLoad(string title, string msg) {
this.Dispatcher.Invoke(() => {
//TimerLabel.Content = "this is content";
//TimerLabel.Visibility = Visibility.Visible;
LoadingDetails.Text = msg;
LoadingTitle.Content = title;
LoadProgress.IsIndeterminate = true;
LoadingScreen.Visibility = Visibility.Visible;
StartTimer();
UpdateLayout();
});
}


private TimeSpan _remainingTime;

private void StartTimer() {
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
dispatcherTimer.Tick += (sender, args) => UpdateTimer(sender as DispatcherTimer);
dispatcherTimer.Start();
_remainingTime = TimeSpan.FromMinutes(5);
}

private void UpdateTimer(DispatcherTimer timer) {
if (_remainingTime.TotalSeconds > 0) {
_remainingTime = _remainingTime.Add(TimeSpan.FromSeconds(-1));
//TimerLabel.Content = _remainingTime.ToString(@"mm\:ss");
} else {
timer.Stop();
//TimerLabel.Content = "00:00";
// Optionally, you can hide the loading screen or perform any other action after the timer ends
}
}

internal void HideLoad() {
this.Dispatcher.Invoke(() => {
LoadingScreen.Visibility = Visibility.Collapsed;
Expand Down
2 changes: 1 addition & 1 deletion DesktopEdge/Models/MFA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ namespace ZitiDesktopEdge.Models {
public class MFA {
public string Url { get; set; }
public string[] RecoveryCodes { get; set; }
public bool IsAuthenticated { get; set; }
public bool IsAuthenticateda { get; set; }
}
}
29 changes: 23 additions & 6 deletions DesktopEdge/Models/ZitiIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,21 @@ public class ZitiIdentity {
public bool IsEnabled { get; set; }
public string EnrollmentStatus { get; set; }
public string Status { get; set; }
public bool IsMFAEnabled { get; set; }
private bool isMfaEnabled = false;
public bool IsMFAEnabled {
get {
return isMfaEnabled;
}
set {
isMfaEnabled = value;
}
}

public void MFADebug(string where) {
logger.Info($"{where}\n\tIdentifiter : {Identifier}\n\tIsMFAEnabled : {IsMFAEnabled}\n\tIsMFANeeded : {IsMFANeeded}\n\tShowMFA\t : {ShowMFA}");
}

public bool IsMFANeeded { get; set; }
public int MinTimeout { get; set; }
public int MaxTimeout { get; set; }
public DateTime LastUpdatedTime { get; set; }
Expand All @@ -41,7 +55,7 @@ public class ZitiIdentity {
public bool WasFullNotified { get; set; }
public string Fingerprint { get; set; }
public string Identifier { get; set; }
public bool IsAuthenticated { get; set; }
public bool ShowMFA { get; set; }
public bool IsTimedOut { get; set; }
public string[] RecoveryCodes { get; set; }
public bool IsTimingOut { get; set; }
Expand Down Expand Up @@ -86,7 +100,7 @@ public ZitiIdentity(string Name, string ControllerUrl, bool IsEnabled, List<Ziti

public static ZitiIdentity FromClient(DataStructures.Identity id) {
ZitiIdentity zid = new ZitiIdentity() {
ControllerUrl = (id.Config == null) ? "": id.Config.ztAPI,
ControllerUrl = (id.Config == null) ? "" : id.Config.ztAPI,
ContollerVersion = id.ControllerVersion,
EnrollmentStatus = "status",
Fingerprint = id.FingerPrint,
Expand All @@ -96,7 +110,8 @@ public static ZitiIdentity FromClient(DataStructures.Identity id) {
Status = id.Status,
RecoveryCodes = new string[0],
IsMFAEnabled = id.MfaEnabled,
IsAuthenticated = !id.MfaNeeded,
IsMFANeeded = id.MfaNeeded,
ShowMFA = id.MfaNeeded && !id.MfaEnabled,
IsTimedOut = false,
IsTimingOut = false,
MinTimeout = id.MinTimeout,
Expand All @@ -105,8 +120,10 @@ public static ZitiIdentity FromClient(DataStructures.Identity id) {
TimeoutMessage = "",
IsConnected = true
};



#if DEBUG
zid.MFADebug("002");
#endif
if (id.Services != null) {
foreach (var svc in id.Services) {
if (svc != null) {
Expand Down
Loading
Loading