Exemplo n.º 1
0
        private void OnInit(SaslInit init)
        {
            SaslCode saslCode = SaslCode.Ok;

            if (init.InitialResponse.Count > 0)
            {
                System.Text.Encoding uTF8 = System.Text.Encoding.UTF8;
                byte[] array  = init.InitialResponse.Array;
                int    offset = init.InitialResponse.Offset;
                ArraySegment <byte> initialResponse = init.InitialResponse;
                string str = uTF8.GetString(array, offset, initialResponse.Count);
                MessagingClientEtwProvider.TraceClient(() => {
                });
                if (this.tokenAuthenticator != null)
                {
                    try
                    {
                        string[] strArrays = this.tokenAuthenticator(str);
                        base.Principal = new GenericPrincipal(new GenericIdentity("acs-client", SaslSwtHandler.Name), strArrays);
                    }
                    catch (Exception exception)
                    {
                        if (Fx.IsFatal(exception))
                        {
                            throw;
                        }
                        saslCode = SaslCode.Auth;
                    }
                }
            }
            base.Negotiator.CompleteNegotiation(saslCode, null);
        }
Exemplo n.º 2
0
        internal ITransport Open(string hostname, ITransport transport)
        {
            ProtocolHeader myHeader = this.Start(hostname, transport);

            ProtocolHeader theirHeader = Reader.ReadHeader(transport);

            Trace.WriteLine(TraceLevel.Frame, "RECV AMQP {0}", theirHeader);
            this.OnHeader(myHeader, theirHeader);

            SaslCode code = SaslCode.SysTemp;

            while (true)
            {
                ByteBuffer buffer = Reader.ReadFrameBuffer(transport, new byte[4], uint.MaxValue);
                if (buffer == null)
                {
                    throw new ObjectDisposedException(transport.GetType().Name);
                }

                if (!this.OnFrame(transport, buffer, out code))
                {
                    break;
                }
            }

            if (code != SaslCode.Ok)
            {
                throw new AmqpException(ErrorCode.UnauthorizedAccess,
                                        Fx.Format(SRAmqp.SaslNegoFailed, code));
            }

            return(this.UpgradeTransport(transport));
        }
Exemplo n.º 3
0
        internal static async Task <IAsyncTransport> OpenAsync(this SaslProfile saslProfile, string hostname,
                                                               IBufferManager bufferManager, IAsyncTransport transport, DescribedList command)
        {
            // if transport is closed, pump reader should throw exception
            TransportWriter writer = new TransportWriter(transport, e => { });

            ProtocolHeader myHeader = saslProfile.Start(writer, command);

            AsyncPump pump = new AsyncPump(bufferManager, transport);
            SaslCode  code = SaslCode.Auth;

            await pump.PumpAsync(
                SaslProfile.MaxFrameSize,
                header =>
            {
                saslProfile.OnHeader(myHeader, header);
                return(true);
            },
                buffer =>
            {
                return(saslProfile.OnFrame(hostname, writer, buffer, out code));
            }).ConfigureAwait(false);

            await writer.FlushAsync().ConfigureAwait(false);

            if (code != SaslCode.Ok)
            {
                throw new AmqpException(ErrorCode.UnauthorizedAccess,
                                        Fx.Format(SRAmqp.SaslNegoFailed, code));
            }

            return((IAsyncTransport)saslProfile.UpgradeTransportInternal(transport));
        }
Exemplo n.º 4
0
        internal bool OnFrame(ITransport transport, ByteBuffer buffer, out SaslCode code)
        {
            ushort        channel;
            DescribedList command;

            Frame.GetFrame(buffer, out channel, out command);
            Trace.WriteLine(TraceLevel.Frame, "RECV {0}", command);

            bool shouldContinue = true;

            if (command.Descriptor.Code == Codec.SaslOutcome.Code)
            {
                code           = ((SaslOutcome)command).Code;
                shouldContinue = false;
            }
            else
            {
                code = SaslCode.Ok;
                DescribedList response = this.OnCommand(command);
                if (response != null)
                {
                    this.SendCommand(transport, response);
                    shouldContinue = response.Descriptor.Code != Codec.SaslOutcome.Code;
                }
            }

            return(shouldContinue);
        }
Exemplo n.º 5
0
        public void CompleteNegotiation(SaslCode code, Exception exception)
        {
            if (!this.transport.IsInitiator)
            {
                SaslOutcome outcome = new SaslOutcome();
                outcome.OutcomeCode = code;
                if (code == SaslCode.Ok)
                {
                    outcome.AdditionalData = new ArraySegment <byte>(Encoding.UTF8.GetBytes(welcome));
                }

                Utils.Trace(TraceLevel.Info, "{0}: sending sasl outcome {1}", this.transport, code);
                this.WriteFrame(outcome, false);
            }

            this.state = SaslState.End;
            if (exception != null)
            {
                this.transport.OnNegotiationFail(exception);
            }
            else
            {
                this.transport.OnNegotiationSucceed(this.saslHandler.Principal);
            }
        }
Exemplo n.º 6
0
        internal bool OnFrame(ITransport transport, ByteBuffer buffer, out SaslCode code)
        {
            ushort channel;
            DescribedList command;
            Frame.GetFrame(buffer, out channel, out command);
            Trace.WriteLine(TraceLevel.Frame, "RECV {0}", command);

            bool shouldContinue = true;
            if (command.Descriptor.Code == Codec.SaslOutcome.Code)
            {
                code = ((SaslOutcome)command).Code;
                shouldContinue = false;
            }
            else
            {
                code = SaslCode.Ok;
                DescribedList response = this.OnCommand(command);
                if (response != null)
                {
                    this.SendCommand(transport, response);
                    shouldContinue = response.Descriptor.Code != Codec.SaslOutcome.Code;
                }
            }

            return shouldContinue;
        }
Exemplo n.º 7
0
        void OnInit(SaslInit init)
        {
            SaslCode code = SaslCode.Ok;

            if (init.InitialResponse.Count > 0)
            {
                string token = Encoding.UTF8.GetString(init.InitialResponse.Array, init.InitialResponse.Offset, init.InitialResponse.Count);
                Utils.Trace(TraceLevel.Verbose, "Received token: {0}", token);

                if (this.tokenAuthenticator != null)
                {
                    try
                    {
                        string[] claimSet = this.tokenAuthenticator(token);
                        this.Principal = new GenericPrincipal(
                            new GenericIdentity("acs-client", SaslSwtHandler.Name),
                            claimSet);
                    }
                    catch (Exception exception)
                    {
                        if (Fx.IsFatal(exception))
                        {
                            throw;
                        }

                        code = SaslCode.Auth;
                    }
                }
            }

            this.Negotiator.CompleteNegotiation(code, null);
        }
Exemplo n.º 8
0
        internal SaslCode GetField(int index, SaslCode field, SaslCode defaultValue = default(SaslCode))
        {
            if ((this.fields & (1 << index)) == 0)
            {
                return(defaultValue);
            }

            return(field);
        }
Exemplo n.º 9
0
            protected override DescribedList OnCommand(DescribedList command)
            {
                if (command.Descriptor.Code == Codec.SaslInit.Code)
                {
                    SaslInit init = (SaslInit)command;
                    SaslCode code = this.ValidateCredentials(init);
                    return(new SaslOutcome()
                    {
                        Code = code
                    });
                }

                throw new AmqpException(ErrorCode.NotAllowed, command.ToString());
            }
Exemplo n.º 10
0
        internal override void ReadField(ByteBuffer buffer, int index, byte formatCode)
        {
            switch (index)
            {
            case 0:
                this.code = (SaslCode)Encoder.ReadUByte(buffer, formatCode);
                break;

            case 1:
                this.additionalData = Encoder.ReadBinary(buffer, formatCode);
                break;

            default:
                Fx.Assert(false, "Invalid field index");
                break;
            }
        }
Exemplo n.º 11
0
        public void CompleteNegotiation(SaslCode code, Exception exception)
        {
            this.state             = SaslNegotiator.SaslState.End;
            this.completeException = exception;
            if (this.isInitiator)
            {
                this.CompleteTransport();
                return;
            }
            SaslOutcome saslOutcome = new SaslOutcome()
            {
                OutcomeCode = new SaslCode?(code)
            };

            if (code == SaslCode.Ok)
            {
                saslOutcome.AdditionalData = new ArraySegment <byte>(System.Text.Encoding.UTF8.GetBytes(SaslNegotiator.welcome));
            }
            this.WriteFrame(saslOutcome, false);
        }
Exemplo n.º 12
0
        public void CompleteNegotiation(SaslCode code, Exception exception)
        {
            this.state             = SaslState.End;
            this.completeException = exception;

            if (!this.isInitiator)
            {
                SaslOutcome outcome = new SaslOutcome();
                outcome.OutcomeCode = code;
                if (code == SaslCode.Ok)
                {
                    outcome.AdditionalData = new ArraySegment <byte>(Encoding.UTF8.GetBytes(welcome));
                }

                // transported is completed in the callback
                this.WriteFrame(outcome, false);
            }
            else
            {
                this.CompleteTransport();
            }
        }
Exemplo n.º 13
0
        internal bool OnFrame(string hostname, ITransport transport, ByteBuffer buffer, out SaslCode code)
        {
            ushort        channel;
            DescribedList command;

            Frame.Decode(buffer, out channel, out command);
            Trace.WriteLine(TraceLevel.Frame, "RECV {0}", command);

            bool shouldContinue = true;

            if (command.Descriptor.Code == Codec.SaslOutcome.Code)
            {
                code           = ((SaslOutcome)command).Code;
                shouldContinue = false;
            }
            else if (command.Descriptor.Code == Codec.SaslMechanisms.Code)
            {
                code = SaslCode.Ok;
                SaslMechanisms mechanisms = (SaslMechanisms)command;
                Symbol         matched    = null;
                foreach (var m in mechanisms.SaslServerMechanisms)
                {
                    if (m.Equals(this.Mechanism))
                    {
                        matched = m;
                        break;
                    }
                }

                if (matched == null)
                {
                    throw new AmqpException(ErrorCode.NotImplemented, mechanisms.ToString());
                }

                DescribedList init = this.GetStartCommand(hostname);
                if (init != null)
                {
                    this.SendCommand(transport, init);
                }
            }
            else
            {
                code = SaslCode.Ok;
                DescribedList response = this.OnCommand(command);
                if (response != null)
                {
                    this.SendCommand(transport, response);
                    if (response.Descriptor.Code == Codec.SaslOutcome.Code)
                    {
                        code           = ((SaslOutcome)response).Code;
                        shouldContinue = false;
                    }
                }
            }

            return(shouldContinue);
        }
Exemplo n.º 14
0
        public void CompleteNegotiation(SaslCode code, Exception exception)
        {
            if (!this.transport.IsInitiator)
            {
                SaslOutcome outcome = new SaslOutcome();
                outcome.OutcomeCode = code;
                if (code == SaslCode.Ok)
                {
                    outcome.AdditionalData = new ArraySegment<byte>(Encoding.UTF8.GetBytes(welcome));
                }

                Utils.Trace(TraceLevel.Info, "{0}: sending sasl outcome {1}", this.transport, code);
                this.WriteFrame(outcome, false);
            }

            this.state = SaslState.End;
            if (exception != null)
            {
                this.transport.OnNegotiationFail(exception);
            }
            else
            {
                this.transport.OnNegotiationSucceed(this.saslHandler.Principal);
            }
        }
Exemplo n.º 15
0
 internal void SetField(int index, ref SaslCode field, SaslCode value)
 {
     this.fields |= (1 << index);
     field        = value;
 }
Exemplo n.º 16
0
        public void CompleteNegotiation(SaslCode code, Exception exception)
        {
            this.state = SaslState.End;
            this.completeException = exception;

            if (!this.isInitiator)
            {
                SaslOutcome outcome = new SaslOutcome();
                outcome.OutcomeCode = code;
                if (code == SaslCode.Ok)
                {
                    outcome.AdditionalData = new ArraySegment<byte>(Encoding.UTF8.GetBytes(welcome));
                }

                // transported is completed in the callback
                this.WriteFrame(outcome, false);
            }
            else
            {
                this.CompleteTransport();
            }
        }