Example #1
0
        public override IEnumerator <AuthCommand> GetEnumerator()
        {
            IEnumerator <AuthCommand> replies = Peer.GetEnumerator();

            while (true)
            {
                Peer.isFinalRead = false;

                AuthCommand reply;
                if (!replies.MoveNext())
                {
                    yield return(null);

                    yield break;
                    //continue;
                }
                reply = replies.Current;

                Match m = authRegex.Match(reply.Value);
                if (!m.Success)
                {
                    yield return(new AuthCommand("ERROR"));

                    continue;
                }

                string mechanism       = m.Groups[1].Value;
                string initialResponse = m.Groups[2].Value;

                if (mechanism == "EXTERNAL")
                {
                    try {
                        byte[] bs      = Sasl.FromHex(initialResponse);
                        string authStr = Encoding.ASCII.GetString(bs);
                        uid = UInt32.Parse(authStr);
                    } catch {
                        uid = 0;
                    }
                    //return RunExternal (Run (), initialResponse);
                }
                else
                {
                    yield return(new AuthCommand("REJECTED " + String.Join(" ", supportedMechanisms)));

                    continue;
                }

                if (Guid == UUID.Zero)
                {
                    yield return(new AuthCommand("OK"));
                }
                else
                {
                    yield return(new AuthCommand("OK " + Guid.ToString()));
                }

                Peer.isFinalRead = true;

                if (!replies.MoveNext())
                {
                    /*
                     * yield break;
                     * continue;
                     */
                    yield return(null);

                    yield break;
                }

                reply = replies.Current;
                if (reply.Value != "BEGIN")
                {
                    yield return(new AuthCommand("ERROR"));

                    continue;
                }

                yield break;
            }
        }
Example #2
0
        public override IEnumerator <AuthCommand> GetEnumerator()
        {
            IEnumerator <AuthCommand> replies = Peer.GetEnumerator();

            AuthMech currMech = AuthMech.External;

            while (true)
            {
                Peer.isFinalRead = false;

                if (currMech == AuthMech.External)
                {
                    string str         = Identity;
                    byte[] bs          = Encoding.ASCII.GetBytes(str);
                    string initialData = Sasl.ToHex(bs);
                    yield return(new AuthCommand("AUTH EXTERNAL " + initialData));

                    currMech = AuthMech.Anonymous;
                }
                else if (currMech == AuthMech.Anonymous)
                {
                    yield return(new AuthCommand("AUTH ANONYMOUS"));

                    currMech = AuthMech.None;
                }
                else
                {
                    throw new Exception("Authentication failure");
                }

                Peer.isFinalRead = true;

                AuthCommand reply;
                if (!replies.MoveNext())
                {
                    yield break;
                }
                reply = replies.Current;

                if (reply[0] == "REJECTED")
                {
                    continue;
                }

                /*
                 * Match m = rejectedRegex.Match (reply.Value);
                 * if (m.Success) {
                 *      string[] mechanisms = m.Groups[1].Value.Split (' ');
                 *      //yield return new AuthCommand ("CANCEL");
                 *      continue;
                 * }
                 */

                if (reply[0] != "OK")
                {
                    yield return(new AuthCommand("ERROR"));

                    continue;
                }

                if (reply[1] == String.Empty)
                {
                    ActualId = UUID.Zero;
                }
                else
                {
                    ActualId = UUID.Parse(reply[1]);
                }

                yield return(new AuthCommand("BEGIN"));

                yield break;
            }
        }