Skip to content

Commit

Permalink
Replaced Hashtable with AuthCredentials, zyanfx#44.
Browse files Browse the repository at this point in the history
Hashtable is implicitly convertable to AuthCredentials for the backwards compatibility.
  • Loading branch information
yallie committed May 18, 2018
1 parent a9d48ad commit efd81f0
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 133 deletions.
7 changes: 4 additions & 3 deletions source/Zyan.Communication/NewLogonNeededEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Collections;
using Zyan.Communication.Security;

namespace Zyan.Communication
{
Expand All @@ -24,7 +25,7 @@ public NewLogonNeededEventArgs()
/// Creates a new instance of the NewLogonNeededEventArgs class.
/// </summary>
/// <param name="credentials">Credentials for logon</param>
public NewLogonNeededEventArgs(Hashtable credentials)
public NewLogonNeededEventArgs(AuthCredentials credentials)
{
Cancel = false;
Credentials = credentials;
Expand All @@ -35,7 +36,7 @@ public NewLogonNeededEventArgs(Hashtable credentials)
/// </summary>
/// <param name="credentials">Security credentials</param>
/// <param name="cancel">Cancel flag</param>
public NewLogonNeededEventArgs(Hashtable credentials, bool cancel)
public NewLogonNeededEventArgs(AuthCredentials credentials, bool cancel)
{
Cancel = cancel;
Credentials = credentials;
Expand All @@ -44,7 +45,7 @@ public NewLogonNeededEventArgs(Hashtable credentials, bool cancel)
/// <summary>
/// Gets or sets the security credentials for the new logon.
/// </summary>
public Hashtable Credentials { get; set; }
public AuthCredentials Credentials { get; set; }

/// <summary>
/// Gets or sets the cancel flag. If set to true, the new logon will be canceled.
Expand Down
122 changes: 122 additions & 0 deletions source/Zyan.Communication/Security/AuthCredentials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Collections;
using System.Security;

namespace Zyan.Communication.Security
{
/// <summary>
/// Authentication credentials used by <see cref="ZyanConnection"/> to authenticate.
/// </summary>
public class AuthCredentials
{
/// <summary>
/// Initializes a new instance of the <see cref="AuthCredentials"/> class.
/// </summary>
/// <param name="credentials">The credentials.</param>
public AuthCredentials(Hashtable credentials = null)
{
CredentialsHashtable = credentials ?? new Hashtable();
}

/// <summary>
/// Initializes a new instance of the <see cref="AuthCredentials"/> class.
/// </summary>
/// <param name="userName">Name of the user.</param>
/// <param name="password">The password.</param>
public AuthCredentials(string userName, string password)
{
CredentialsHashtable = new Hashtable
{
{ AuthRequestMessage.CREDENTIAL_USERNAME, userName },
{ AuthRequestMessage.CREDENTIAL_PASSWORD, password },
};
}

/// <summary>
/// Gets or sets the name of the user.
/// </summary>
public string UserName
{
get { return (string)CredentialsHashtable[AuthRequestMessage.CREDENTIAL_USERNAME]; }
set { CredentialsHashtable[AuthRequestMessage.CREDENTIAL_USERNAME] = value; }
}

/// <summary>
/// Gets or sets the password.
/// </summary>
public string Password
{
get { return (string)CredentialsHashtable[AuthRequestMessage.CREDENTIAL_PASSWORD]; }
set { CredentialsHashtable[AuthRequestMessage.CREDENTIAL_PASSWORD] = value; }
}

/// <summary>
/// Gets or sets the domain.
/// </summary>
public string Domain
{
get { return (string)CredentialsHashtable[AuthRequestMessage.CREDENTIAL_DOMAIN]; }
set { CredentialsHashtable[AuthRequestMessage.CREDENTIAL_DOMAIN] = value; }
}

/// <summary>
/// Gets or sets the Windows security token.
/// </summary>
public string WindowsSecurityToken
{
get { return (string)CredentialsHashtable[AuthRequestMessage.CREDENTIAL_WINDOWS_SECURITY_TOKEN]; }
set { CredentialsHashtable[AuthRequestMessage.CREDENTIAL_WINDOWS_SECURITY_TOKEN] = value; }
}

/// <summary>
/// Gets or sets the credentials hashtable.
/// </summary>
protected Hashtable CredentialsHashtable { get; set; }

/// <summary>
/// Adds the specified name/value pair.
/// </summary>
/// <param name="name">The name of the property, like UserName.</param>
/// <param name="value">The value of the property.</param>
public virtual void Add(string name, string value)
{
CredentialsHashtable = CredentialsHashtable ?? new Hashtable();
CredentialsHashtable.Add(name, value);
}

/// <summary>
/// Authenticates a specific user based on their credentials.
/// </summary>
/// <param name="sessionId">Session identity.</param>
/// <param name="dispatcher">Remote dispatcher</param>
public virtual void Authenticate(Guid sessionId, IZyanDispatcher dispatcher)
{
var credentials = CredentialsHashtable;
if (credentials != null && credentials.Count == 0)
credentials = null;

var response = dispatcher.Logon(sessionId, credentials);
if (!response.Completed)
{
throw new SecurityException(response.ErrorMessage ?? "Authentication is not completed.");
}

// this case is likely to be handled by ZyanDispatcher itself
if (!response.Success)
{
throw new SecurityException(response.ErrorMessage ?? "Authentication is not successful.");
}
}

/// <summary>
/// Performs an implicit conversion from <see cref="Hashtable"/> to <see cref="AuthCredentials"/>.
/// </summary>
/// <param name="credentials">The credentials.</param>
public static implicit operator AuthCredentials(Hashtable credentials) => new AuthCredentials(credentials);

/// <summary>
/// Gets a value indicating whether this instance has no credentials specified.
/// </summary>
public bool IsEmpty => CredentialsHashtable == null || CredentialsHashtable.Count == 0;
}
}
19 changes: 0 additions & 19 deletions source/Zyan.Communication/Security/IAuthenticationClient.cs

This file was deleted.

33 changes: 0 additions & 33 deletions source/Zyan.Communication/Security/SimpleAuthenticationClient.cs

This file was deleted.

3 changes: 1 addition & 2 deletions source/Zyan.Communication/Zyan.Communication.Fx3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,11 @@
<Compile Include="Protocols\Tcp\TcpDuplexClientProtocolSetup.cs" />
<Compile Include="Protocols\Tcp\TcpDuplexServerProtocolSetup.cs" />
<Compile Include="Protocols\Versioning.cs" />
<Compile Include="Security\AuthCredentials.cs" />
<Compile Include="Security\BasicWindowsAuthProvider.cs" />
<Compile Include="Security\Exceptions\AccountLockedException.cs" />
<Compile Include="Security\Exceptions\AccountNotFoundException.cs" />
<Compile Include="Security\Exceptions\PasswordExpiredException.cs" />
<Compile Include="Security\IAuthenticationClient.cs" />
<Compile Include="Security\SecureRemotePassword\BigIntegerFx3.cs" />
<Compile Include="Security\SecureRemotePassword\ISrpHash.cs" />
<Compile Include="Security\SecureRemotePassword\SrpClient.cs" />
Expand All @@ -282,7 +282,6 @@
<Compile Include="Security\SecureRemotePassword\SrpParameters.cs" />
<Compile Include="Security\SecureRemotePassword\SrpServer.cs" />
<Compile Include="Security\SecureRemotePassword\SrpSession.cs" />
<Compile Include="Security\SimpleAuthenticationClient.cs" />
<Compile Include="Security\WindowsSecurityTools.cs" />
<Compile Include="CustomSerializationContainer.cs" />
<Compile Include="SerializationHandlerRepository.cs" />
Expand Down
3 changes: 1 addition & 2 deletions source/Zyan.Communication/Zyan.Communication.Fx4.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,11 @@
<Compile Include="Protocols\Versioning.cs" />
<Compile Include="Protocols\Wrapper\ChannelWrapper.cs" />
<Compile Include="Protocols\Wrapper\MessageSinkWrapper.cs" />
<Compile Include="Security\AuthCredentials.cs" />
<Compile Include="Security\BasicWindowsAuthProvider.cs" />
<Compile Include="Security\Exceptions\AccountLockedException.cs" />
<Compile Include="Security\Exceptions\AccountNotFoundException.cs" />
<Compile Include="Security\Exceptions\PasswordExpiredException.cs" />
<Compile Include="Security\IAuthenticationClient.cs" />
<Compile Include="Security\SecureRemotePassword\ISrpHash.cs" />
<Compile Include="Security\SecureRemotePassword\SrpClient.cs" />
<Compile Include="Security\SecureRemotePassword\SrpEphemeral.cs" />
Expand All @@ -277,7 +277,6 @@
<Compile Include="Security\SecureRemotePassword\SrpParameters.cs" />
<Compile Include="Security\SecureRemotePassword\SrpServer.cs" />
<Compile Include="Security\SecureRemotePassword\SrpSession.cs" />
<Compile Include="Security\SimpleAuthenticationClient.cs" />
<Compile Include="Security\WindowsSecurityTools.cs" />
<Compile Include="CustomSerializationContainer.cs" />
<Compile Include="SerializationHandlerRepository.cs" />
Expand Down
3 changes: 1 addition & 2 deletions source/Zyan.Communication/Zyan.Communication.Fx45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@
<Compile Include="Protocols\Versioning.cs" />
<Compile Include="Protocols\Wrapper\ChannelWrapper.cs" />
<Compile Include="Protocols\Wrapper\MessageSinkWrapper.cs" />
<Compile Include="Security\AuthCredentials.cs" />
<Compile Include="Security\BasicWindowsAuthProvider.cs" />
<Compile Include="Security\Exceptions\AccountLockedException.cs" />
<Compile Include="Security\Exceptions\AccountNotFoundException.cs" />
<Compile Include="Security\Exceptions\PasswordExpiredException.cs" />
<Compile Include="Security\IAuthenticationClient.cs" />
<Compile Include="Security\SecureRemotePassword\ISrpHash.cs" />
<Compile Include="Security\SecureRemotePassword\SrpClient.cs" />
<Compile Include="Security\SecureRemotePassword\SrpEphemeral.cs" />
Expand All @@ -297,7 +297,6 @@
<Compile Include="Security\SecureRemotePassword\SrpParameters.cs" />
<Compile Include="Security\SecureRemotePassword\SrpServer.cs" />
<Compile Include="Security\SecureRemotePassword\SrpSession.cs" />
<Compile Include="Security\SimpleAuthenticationClient.cs" />
<Compile Include="Security\WindowsSecurityTools.cs" />
<Compile Include="CustomSerializationContainer.cs" />
<Compile Include="SerializationHandlerRepository.cs" />
Expand Down
3 changes: 1 addition & 2 deletions source/Zyan.Communication/Zyan.Communication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@
<Compile Include="Protocols\Versioning.cs" />
<Compile Include="Protocols\Wrapper\ChannelWrapper.cs" />
<Compile Include="Protocols\Wrapper\MessageSinkWrapper.cs" />
<Compile Include="Security\AuthCredentials.cs" />
<Compile Include="Security\BasicWindowsAuthProvider.cs" />
<Compile Include="Security\Exceptions\AccountLockedException.cs" />
<Compile Include="Security\Exceptions\AccountNotFoundException.cs" />
<Compile Include="Security\Exceptions\PasswordExpiredException.cs" />
<Compile Include="Security\IAuthenticationClient.cs" />
<Compile Include="Security\SecureRemotePassword\ISrpHash.cs" />
<Compile Include="Security\SecureRemotePassword\SrpClient.cs" />
<Compile Include="Security\SecureRemotePassword\SrpEphemeral.cs" />
Expand All @@ -294,7 +294,6 @@
<Compile Include="Security\SecureRemotePassword\SrpParameters.cs" />
<Compile Include="Security\SecureRemotePassword\SrpServer.cs" />
<Compile Include="Security\SecureRemotePassword\SrpSession.cs" />
<Compile Include="Security\SimpleAuthenticationClient.cs" />
<Compile Include="Security\WindowsSecurityTools.cs" />
<Compile Include="CustomSerializationContainer.cs" />
<Compile Include="SerializationHandlerRepository.cs" />
Expand Down
Loading

0 comments on commit efd81f0

Please sign in to comment.