diff --git a/src/Unosquare.Swan/Extensions.ByteArrays.cs b/src/Unosquare.Swan/Extensions.ByteArrays.cs index 8672103ba..57a918cc8 100644 --- a/src/Unosquare.Swan/Extensions.ByteArrays.cs +++ b/src/Unosquare.Swan/Extensions.ByteArrays.cs @@ -493,7 +493,7 @@ public static T[] SubArray(this T[] array, long startIndex, long length) /// Converts an array of sbytes to an array of bytes /// /// The sbyte array. - /// + /// The byte array from conversion public static byte[] ToByteArray(this sbyte[] sbyteArray) { var byteArray = new byte[sbyteArray.Length]; @@ -506,7 +506,7 @@ public static byte[] ToByteArray(this sbyte[] sbyteArray) /// Receives a byte array and returns it transformed in an sbyte array /// /// The byte array. - /// + /// The sbyte array from conversion public static sbyte[] ToSByteArray(this byte[] byteArray) { var sbyteArray = new sbyte[byteArray.Length]; @@ -520,7 +520,7 @@ public static sbyte[] ToSByteArray(this byte[] byteArray) /// /// The encoding. /// The s. - /// The sbyte array + /// The sbyte array from string public static sbyte[] GetSBytes(this Encoding encoding, string s) => encoding.GetBytes(s).ToSByteArray(); diff --git a/src/Unosquare.Swan/Formatters/CsvWriter.cs b/src/Unosquare.Swan/Formatters/CsvWriter.cs index af665e093..043f7641c 100644 --- a/src/Unosquare.Swan/Formatters/CsvWriter.cs +++ b/src/Unosquare.Swan/Formatters/CsvWriter.cs @@ -340,10 +340,7 @@ public void WriteHeadings(Type type) /// Writes the headings. /// /// The type of object to extract heads - public void WriteHeadings() - { - WriteHeadings(typeof(T)); - } + public void WriteHeadings() => WriteHeadings(typeof(T)); /// /// Writes the headings. diff --git a/src/Unosquare.Swan/Networking/Ldap/LdapConnection.cs b/src/Unosquare.Swan/Networking/Ldap/LdapConnection.cs index d94736a89..d9cac0073 100644 --- a/src/Unosquare.Swan/Networking/Ldap/LdapConnection.cs +++ b/src/Unosquare.Swan/Networking/Ldap/LdapConnection.cs @@ -209,7 +209,6 @@ public virtual LdapConstraints Constraints var newCons = (LdapSearchConstraints)_defSearchCons.Clone(); newCons.HopLimit = value.HopLimit; newCons.TimeLimit = value.TimeLimit; - newCons.SetReferralHandler(value.GetReferralHandler()); newCons.ReferralFollowing = value.ReferralFollowing; var lsc = value.GetControls(); if (lsc != null) diff --git a/src/Unosquare.Swan/Networking/Ldap/LdapConstraints.cs b/src/Unosquare.Swan/Networking/Ldap/LdapConstraints.cs index 41af45569..13043635b 100644 --- a/src/Unosquare.Swan/Networking/Ldap/LdapConstraints.cs +++ b/src/Unosquare.Swan/Networking/Ldap/LdapConstraints.cs @@ -3,15 +3,7 @@ namespace Unosquare.Swan.Networking.Ldap { using System; using System.Collections; - - /// - /// Shared ancestor to the two types of referral objects - LdapBindHandler and - /// LdapAuthHandler. - /// - public interface ILdapReferralHandler - { - } - + /// /// Defines options controlling Ldap operations on the directory. /// An LdapConstraints object is always associated with an LdapConnection @@ -25,7 +17,6 @@ public class LdapConstraints private int _msLimit; private int _hopLimit = 10; private bool _doReferrals; - private ILdapReferralHandler _refHandler; private LdapControl[] _controls; private Hashtable _properties; // Properties @@ -40,7 +31,7 @@ public LdapConstraints() } /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// Constructs a new LdapConstraints object specifying constraints that /// control wait time, and referral handling. /// @@ -59,16 +50,6 @@ public LdapConstraints() /// The way referrals are followed automatically is /// determined by the setting of the handler parameter. /// It is ignored for asynchronous operations. - /// The custom authentication handler called when - /// LdapConnection needs to authenticate, typically on - /// following a referral. A null may be specified to - /// indicate default authentication processing, i.e. - /// referrals are followed with anonymous authentication. - /// The handler object may be an implemention of either the - /// LdapBindHandler or LdapAuthHandler interface. - /// The implementation of these interfaces determines how - /// authentication is performed when following referrals. - /// It is ignored for asynchronous operations. /// The maximum number of referrals to follow in a /// sequence during automatic referral following. /// The default value is 10. A value of 0 means no limit. @@ -78,11 +59,10 @@ public LdapConstraints() /// number of referrals in a sequence exceeds the limit. /// It is ignored for asynchronous operations. /// - public LdapConstraints(int msLimit, bool doReferrals, ILdapReferralHandler handler, int hopLimit) + public LdapConstraints(int msLimit, bool doReferrals, int hopLimit) { _msLimit = msLimit; _doReferrals = doReferrals; - _refHandler = handler; _hopLimit = hopLimit; } @@ -240,16 +220,7 @@ public virtual void SetProperty(string name, object propertyValue) _properties[name] = propertyValue; } - - /// - /// Specifies the object that will process authentication requests - /// during automatic referral following. - /// The default is null. - /// - /// An object that implements LdapBindHandler or - /// LdapAuthHandler - public virtual void SetReferralHandler(ILdapReferralHandler handler) => _refHandler = handler; - + /// /// Clones an LdapConstraints object. /// @@ -279,16 +250,6 @@ public object Clone() throw new Exception("Internal error, cannot create clone", ce); } } - - /// - /// Returns an object that can process authentication for automatic - /// referral handling. - /// It may be null. - /// - /// - /// An LdapReferralHandler object that can process authentication. - /// - internal virtual ILdapReferralHandler GetReferralHandler() => _refHandler; } } diff --git a/src/Unosquare.Swan/Networking/Ldap/LdapControl.cs b/src/Unosquare.Swan/Networking/Ldap/LdapControl.cs index 061542d67..dbee74cde 100644 --- a/src/Unosquare.Swan/Networking/Ldap/LdapControl.cs +++ b/src/Unosquare.Swan/Networking/Ldap/LdapControl.cs @@ -327,7 +327,7 @@ public string[] Referrals { get { - var references = ((RfcSearchResultReference)message.Response).ToArray(); + var references = ((RfcSearchResultReference)Message.Response).ToArray(); srefs = new string[references.Length]; for (var i = 0; i < references.Length; i++) { @@ -412,14 +412,14 @@ public LdapSearchConstraints() /// /// The cons. public LdapSearchConstraints(LdapConstraints cons) - : base(cons.TimeLimit, cons.ReferralFollowing, cons.GetReferralHandler(), cons.HopLimit) + : base(cons.TimeLimit, cons.ReferralFollowing, cons.HopLimit) { var lsc = cons.GetControls(); if (lsc != null) { - var generated_var = new LdapControl[lsc.Length]; - lsc.CopyTo(generated_var, 0); - SetControls(generated_var); + var ldapControl = new LdapControl[lsc.Length]; + lsc.CopyTo(ldapControl, 0); + SetControls(ldapControl); } var lp = cons.Properties; @@ -436,71 +436,7 @@ public LdapSearchConstraints(LdapConstraints cons) BatchSize = scons.BatchSize; } } - - /// - /// Initializes a new instance of the class. - /// Constructs a new LdapSearchConstraints object and allows the - /// specification operational constraints in that object. - /// - /// The maximum time in milliseconds to wait for results. - /// The default is 0, which means that there is no - /// maximum time limit. This limit is enforced for an - /// operation by the API, not by the server. - /// The operation will be abandoned and terminated by the - /// API with an LdapException.Ldap_TIMEOUT if the - /// operation exceeds the time limit. - /// The maximum time in seconds that the server - /// should spend returning search results. This is a - /// server-enforced limit. The default of 0 means - /// no time limit. - /// The operation will be terminated by the server with an - /// LdapException.TIME_LIMIT_EXCEEDED if the search - /// operation exceeds the time limit. - /// Specifies when aliases should be dereferenced. - /// Must be either DEREF_NEVER, DEREF_FINDING, - /// DEREF_SEARCHING, or DEREF_ALWAYS from this class. - /// Default: DEREF_NEVER - /// The maximum number of search results to return - /// for a search request. - /// The search operation will be terminated by the server - /// with an LdapException.SIZE_LIMIT_EXCEEDED if the - /// number of results exceed the maximum. - /// Default: 1000 - /// Determines whether to automatically follow - /// referrals or not. Specify true to follow - /// referrals automatically, and false to throw - /// an LdapException.REFERRAL if the server responds - /// with a referral. - /// It is ignored for asynchronous operations. - /// Default: false - /// The number of results to return in a batch. Specifying - /// 0 means to block until all results are received. - /// Specifying 1 means to return results one result at a - /// time. Default: 1 - /// The custom authentication handler called when - /// LdapConnection needs to authenticate, typically on - /// following a referral. A null may be specified to - /// indicate default authentication processing, i.e. - /// referrals are followed with anonymous authentication. - /// ThE object may be an implemention of either the - /// the LdapBindHandler or LdapAuthHandler interface. - /// It is ignored for asynchronous operations. - /// The maximum number of referrals to follow in a - /// sequence during automatic referral following. - /// The default value is 10. A value of 0 means no limit. - /// It is ignored for asynchronous operations. - /// The operation will be abandoned and terminated by the - /// API with an LdapException.REFERRAL_LIMIT_EXCEEDED if the - /// number of referrals in a sequence exceeds the limit. - public LdapSearchConstraints(int msLimit, int serverTimeLimit, int dereference, int maxResults, bool doReferrals, int batchSize, ILdapReferralHandler handler, int hopLimit) - : base(msLimit, doReferrals, handler, hopLimit) - { - ServerTimeLimit = serverTimeLimit; - Dereference = dereference; - MaxResults = maxResults; - BatchSize = batchSize; - } - + /// /// Returns the number of results to block on during receipt of search /// results. @@ -1188,7 +1124,7 @@ internal LdapResponse(RfcLdapMessage message) /// /// Any error message in the response. /// - public string ErrorMessage => exception != null ? exception.LdapErrorMessage : ((IRfcResponse)message.Response).GetErrorMessage().StringValue(); + public string ErrorMessage => exception != null ? exception.LdapErrorMessage : ((IRfcResponse)Message.Response).GetErrorMessage().StringValue(); /// /// Returns the partially matched DN field from the server response, @@ -1197,7 +1133,7 @@ internal LdapResponse(RfcLdapMessage message) /// /// The partially matched DN field, if the response contains one. /// - public string MatchedDN => exception != null ? exception.MatchedDN : ((IRfcResponse)message.Response).GetMatchedDN().StringValue(); + public string MatchedDN => exception != null ? exception.MatchedDN : ((IRfcResponse)Message.Response).GetMatchedDN().StringValue(); /// /// Returns all referrals in a server response, if the response contains any. @@ -1210,7 +1146,7 @@ public string[] Referrals get { string[] referrals; - var reference = ((IRfcResponse)message.Response).GetReferral(); + var reference = ((IRfcResponse)Message.Response).GetReferral(); if (reference == null) { @@ -1270,10 +1206,10 @@ public LdapStatusCode ResultCode return exception.ResultCode; } - if ((IRfcResponse)message.Response is RfcIntermediateResponse) + if ((IRfcResponse)Message.Response is RfcIntermediateResponse) return LdapStatusCode.Success; - return (LdapStatusCode)((IRfcResponse)message.Response).GetResultCode().IntValue(); + return (LdapStatusCode)((IRfcResponse)Message.Response).GetResultCode().IntValue(); } } diff --git a/src/Unosquare.Swan/Networking/Ldap/LdapMessage.cs b/src/Unosquare.Swan/Networking/Ldap/LdapMessage.cs index e53694261..8adf4776a 100644 --- a/src/Unosquare.Swan/Networking/Ldap/LdapMessage.cs +++ b/src/Unosquare.Swan/Networking/Ldap/LdapMessage.cs @@ -10,8 +10,62 @@ namespace Unosquare.Swan.Networking.Ldap /// public class LdapMessage { + internal RfcLdapMessage Message; + + private int imsgNum = -1; // This instance LdapMessage number + + private LdapOperation _messageType = LdapOperation.Unknown; + + private string _stringTag; + + /// + /// Initializes a new instance of the class. + /// Dummy constuctor + /// + internal LdapMessage() + { + } + + /// + /// Initializes a new instance of the class. + /// Creates an LdapMessage when sending a protocol operation and sends + /// some optional controls with the message. + /// + /// The type. + /// The operation type of message. + /// The controls to use with the operation. + /// + internal LdapMessage(LdapOperation type, IRfcRequest op, LdapControl[] controls) + { + // Get a unique number for this request message + _messageType = type; + RfcControls asn1Ctrls = null; + + if (controls != null) + { + // Move LdapControls into an RFC 2251 Controls object. + asn1Ctrls = new RfcControls(); + + foreach (var t in controls) + { + asn1Ctrls.Add(t.Asn1Object); + } + } + + // create RFC 2251 LdapMessage + Message = new RfcLdapMessage(op, asn1Ctrls); + } + + /// + /// Initializes a new instance of the class. + /// Creates an Rfc 2251 LdapMessage when the libraries receive a response + /// from a command. + /// + /// A response message. + internal LdapMessage(RfcLdapMessage message) => Message = message; + /// Returns the LdapMessage request associated with this response - internal virtual LdapMessage RequestingMessage => message.RequestingMessage; + internal virtual LdapMessage RequestingMessage => Message.RequestingMessage; /// /// Returns any controls in the message. @@ -23,7 +77,7 @@ public virtual LdapControl[] Controls { get { - var asn1Ctrls = message.Controls; + var asn1Ctrls = Message.Controls; // convert from RFC 2251 Controls to LDAPControl[]. if (asn1Ctrls == null) return null; @@ -57,7 +111,7 @@ public virtual int MessageID { if (imsgNum == -1) { - imsgNum = message.MessageID; + imsgNum = Message.MessageID; } return imsgNum; @@ -71,7 +125,7 @@ public virtual int MessageID /// true if the message is a request, false if it is a response, /// a search result, or a search result reference. /// - public virtual bool Request => message.IsRequest(); + public virtual bool Request => Message.IsRequest(); /// /// Returns the Ldap operation type of the message. @@ -85,17 +139,17 @@ internal LdapOperation Type { get { - if (messageType == LdapOperation.Unknown) + if (_messageType == LdapOperation.Unknown) { - messageType = message.Type; + _messageType = Message.Type; } - return messageType; + return _messageType; } } /// Returns the RFC 2251 LdapMessage composed in this object. - internal virtual RfcLdapMessage Asn1Object => message; + internal virtual RfcLdapMessage Asn1Object => Message; private string Name => Type.ToString(); @@ -133,72 +187,25 @@ public virtual string Tag { get { - if (stringTag != null) + if (_stringTag != null) { - return stringTag; + return _stringTag; } - return Request ? null : RequestingMessage?.stringTag; + return Request ? null : RequestingMessage?._stringTag; } - set => stringTag = value; + set => _stringTag = value; } - /// A request or response message for an asynchronous Ldap operation. - internal RfcLdapMessage message; - - private int imsgNum = -1; // This instance LdapMessage number - - private LdapOperation messageType = LdapOperation.Unknown; - - private string stringTag; - /// - /// Initializes a new instance of the class. - /// Dummy constuctor - /// - internal LdapMessage() - { - } - - /// - /// Initializes a new instance of the class. - /// Creates an LdapMessage when sending a protocol operation and sends - /// some optional controls with the message. + /// Returns a that represents this instance. /// - /// The type. - /// The operation type of message. - /// The controls to use with the operation. - /// - internal LdapMessage(LdapOperation type, IRfcRequest op, LdapControl[] controls) - { - // Get a unique number for this request message - messageType = type; - RfcControls asn1Ctrls = null; - - if (controls != null) - { - // Move LdapControls into an RFC 2251 Controls object. - asn1Ctrls = new RfcControls(); - - foreach (var t in controls) - { - asn1Ctrls.Add(t.Asn1Object); - } - } - - // create RFC 2251 LdapMessage - message = new RfcLdapMessage(op, asn1Ctrls); - } + /// + /// A that represents this instance. + /// + public override string ToString() => Name + "(" + MessageID + "): " + Message; - /// - /// Initializes a new instance of the class. - /// Creates an Rfc 2251 LdapMessage when the libraries receive a response - /// from a command. - /// - /// A response message. - internal LdapMessage(RfcLdapMessage message) => this.message = message; - /// /// Instantiates an LdapControl. We search through our list of /// registered controls. If we find a matchiing OID we instantiate @@ -261,14 +268,6 @@ private LdapControl ControlFactory(string oid, bool critical, sbyte[] values) // for this oid. Return a default LDAPControl object. return new LdapControl(oid, critical, values); } - - /// - /// Returns a that represents this instance. - /// - /// - /// A that represents this instance. - /// - public override string ToString() => Name + "(" + MessageID + "): " + message; } } #endif \ No newline at end of file diff --git a/src/Unosquare.Swan/Networking/Ldap/RfcFilter.cs b/src/Unosquare.Swan/Networking/Ldap/RfcFilter.cs index 8a1766f1e..170192bd6 100644 --- a/src/Unosquare.Swan/Networking/Ldap/RfcFilter.cs +++ b/src/Unosquare.Swan/Networking/Ldap/RfcFilter.cs @@ -600,8 +600,10 @@ public virtual void AddSubstring(SubstringOp type, sbyte[] values) } catch (InvalidCastException e) { - throw new LdapLocalException("A call to addSubstring occured without calling startSubstring", - LdapStatusCode.FilterError, e); + throw new LdapLocalException( + "A call to addSubstring occured without calling startSubstring", + LdapStatusCode.FilterError, + e); } } diff --git a/src/Unosquare.Swan/Networking/Ldap/RfcLdap.cs b/src/Unosquare.Swan/Networking/Ldap/RfcLdap.cs index 7cb8fffbf..4cdfb32cd 100644 --- a/src/Unosquare.Swan/Networking/Ldap/RfcLdap.cs +++ b/src/Unosquare.Swan/Networking/Ldap/RfcLdap.cs @@ -68,28 +68,27 @@ public virtual LdapEntry Entry { get { - if (entry == null) - { - var attrs = new LdapAttributeSet(); - var attrList = ((RfcSearchResultEntry) message.Response).Attributes; - var seqArray = attrList.ToArray(); + if (entry != null) return entry; - foreach (Asn1Sequence seq in seqArray) - { - var attr = new LdapAttribute(((Asn1OctetString) seq.Get(0)).StringValue()); - var set = (Asn1Set) seq.Get(1); + var attrs = new LdapAttributeSet(); + var attrList = ((RfcSearchResultEntry) Message.Response).Attributes; + var seqArray = attrList.ToArray(); - foreach (var t in set.ToArray()) - { - attr.AddValue(((Asn1OctetString) t).ByteValue()); - } + foreach (Asn1Sequence seq in seqArray) + { + var attr = new LdapAttribute(((Asn1OctetString) seq.Get(0)).StringValue()); + var set = (Asn1Set) seq.Get(1); - attrs.Add(attr); + foreach (var t in set.ToArray()) + { + attr.AddValue(((Asn1OctetString) t).ByteValue()); } - entry = new LdapEntry(((RfcSearchResultEntry) message.Response).ObjectName.StringValue(), attrs); + attrs.Add(attr); } + entry = new LdapEntry(((RfcSearchResultEntry) Message.Response).ObjectName.StringValue(), attrs); + return entry; } } @@ -408,20 +407,21 @@ public Asn1SequenceOf GetReferral() } /// - /// Represents an LDAP Intermediate Response. - /// IntermediateResponse ::= [APPLICATION 25] SEQUENCE { - /// COMPONENTS OF LDAPResult, note: only present on incorrectly - /// encoded response from - /// pre Falcon-sp1 server - /// responseName [10] LDAPOID OPTIONAL, - /// responseValue [11] OCTET STRING OPTIONAL } + /// Represents an LDAP Intermediate Response. + /// IntermediateResponse ::= [APPLICATION 25] SEQUENCE { + /// COMPONENTS OF LDAPResult, note: only present on incorrectly + /// encoded response from pre Falcon-sp1 server + /// responseName [10] LDAPOID OPTIONAL, + /// responseValue [11] OCTET STRING OPTIONAL } /// + /// + /// internal class RfcIntermediateResponse : Asn1Sequence, IRfcResponse { - public const int TAG_RESPONSE_NAME = 0; - public const int TAG_RESPONSE = 1; - private readonly int m_responseNameIndex; - private readonly int m_responseValueIndex; + public const int TagResponseName = 0; + public const int TagResponse = 1; + private readonly int _mResponseNameIndex; + private readonly int _mResponseValueIndex; /// /// Initializes a new instance of the class. @@ -432,7 +432,7 @@ internal class RfcIntermediateResponse : Asn1Sequence, IRfcResponse public RfcIntermediateResponse(IAsn1Decoder dec, Stream stream, int len) : base(dec, stream, len) { - m_responseNameIndex = m_responseValueIndex = 0; + _mResponseNameIndex = _mResponseValueIndex = 0; var i = Size() >= 3 ? 3 : 0; for (; i < Size(); i++) @@ -441,13 +441,13 @@ public RfcIntermediateResponse(IAsn1Decoder dec, Stream stream, int len) switch (obj.GetIdentifier().Tag) { - case TAG_RESPONSE_NAME: + case TagResponseName: Set(i, new RfcLdapOID(((Asn1OctetString) obj.TaggedValue).ByteValue())); - m_responseNameIndex = i; + _mResponseNameIndex = i; break; - case TAG_RESPONSE: + case TagResponse: Set(i, obj.TaggedValue); - m_responseValueIndex = i; + _mResponseValueIndex = i; break; } } @@ -463,15 +463,15 @@ public RfcIntermediateResponse(IAsn1Decoder dec, Stream stream, int len) public RfcLdapOID GetResponseName() { - return m_responseNameIndex >= 0 - ? (RfcLdapOID) Get(m_responseNameIndex) + return _mResponseNameIndex >= 0 + ? (RfcLdapOID) Get(_mResponseNameIndex) : null; } public Asn1OctetString GetResponse() { - return m_responseValueIndex != 0 - ? (Asn1OctetString) Get(m_responseValueIndex) + return _mResponseValueIndex != 0 + ? (Asn1OctetString) Get(_mResponseValueIndex) : null; } diff --git a/src/Unosquare.Swan/Networking/Ldap/RfcLdapMessage.cs b/src/Unosquare.Swan/Networking/Ldap/RfcLdapMessage.cs index a8f6bf0e8..02ab8b478 100644 --- a/src/Unosquare.Swan/Networking/Ldap/RfcLdapMessage.cs +++ b/src/Unosquare.Swan/Networking/Ldap/RfcLdapMessage.cs @@ -293,7 +293,7 @@ internal interface IRfcResponse /// This interface represents Protocol Operations that are requests from a /// client. /// - public interface IRfcRequest + internal interface IRfcRequest { /// /// Builds a new request using the data from the this object.