Ejemplo n.º 1
0
        async Task <Option <IClientCredentials> > GetUpdatedAuthenticatedIdentity()
        {
            var cbsNode = this.connection.FindExtension <ICbsNode>();

            if (cbsNode != null)
            {
                AmqpAuthentication updatedAmqpAuthentication = await cbsNode.GetAmqpAuthentication();

                if (updatedAmqpAuthentication.IsAuthenticated)
                {
                    return(updatedAmqpAuthentication.ClientCredentials);
                }
            }
            return(Option.None <IClientCredentials>());
        }
Ejemplo n.º 2
0
        async Task EnsureInitialized()
        {
            if (!this.isInitialized)
            {
                using (await this.initializationLock.LockAsync())
                {
                    if (!this.isInitialized)
                    {
                        AmqpAuthentication amqpAuth;
                        // Check if Principal is SaslPrincipal
                        if (this.connection.Principal is SaslPrincipal saslPrincipal)
                        {
                            amqpAuth = saslPrincipal.AmqpAuthentication;
                        }
                        else
                        {
                            // Else the connection uses CBS authentication. Get AmqpAuthentication from the CbsNode
                            var cbsNode = this.connection.FindExtension <ICbsNode>();
                            if (cbsNode == null)
                            {
                                throw new InvalidOperationException("CbsNode is null");
                            }

                            amqpAuth = await cbsNode.GetAmqpAuthentication();
                        }

                        if (!amqpAuth.IsAuthenticated)
                        {
                            throw new InvalidOperationException("Connection not authenticated");
                        }

                        IClientCredentials identity = amqpAuth.ClientCredentials.Expect(() => new InvalidOperationException("Authenticated connection should have a valid identity"));
                        this.deviceListener = await this.connectionProvider.GetDeviceListenerAsync(identity);

                        var deviceProxy = new DeviceProxy(this, identity.Identity);
                        this.deviceListener.BindDeviceProxy(deviceProxy);
                        this.amqpAuthentication = amqpAuth;
                        this.isInitialized      = true;
                        Events.InitializedConnectionHandler(identity.Identity);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        async Task <AmqpAuthentication> UpdateAmqpAuthentication(AmqpMessage message)
        {
            using (await this.identitySyncLock.LockAsync())
            {
                try
                {
                    (AmqpAuthentication amqpAuth, AmqpResponseStatusCode statusCode, string description) = await this.UpdateCbsToken(message);

                    await this.SendResponseAsync(message, statusCode, description);

                    this.amqpAuthentication = amqpAuth;
                    return(this.amqpAuthentication);
                }
                catch (Exception e)
                {
                    await this.SendResponseAsync(message, AmqpResponseStatusCode.InternalServerError, e.Message);

                    Events.ErrorUpdatingToken(e);
                    return(AmqpAuthentication.Unauthenticated);
                }
            }
        }
Ejemplo n.º 4
0
 public SaslPrincipal(AmqpAuthentication amqpAuthentication)
 {
     this.AmqpAuthentication = Preconditions.CheckNotNull(amqpAuthentication, nameof(amqpAuthentication));
     this.Identity           = new GenericIdentity(amqpAuthentication.ClientCredentials.Map(i => i.Identity.Id).GetOrElse(string.Empty));
 }