Esempio n. 1
0
        public static async Task <(ConnectionRecord firstParty, ConnectionRecord secondParty)> EstablishConnectionAsync(
            IConnectionService connectionService,
            IProducerConsumerCollection <AgentMessage> _messages,
            IAgentContext firstContext,
            IAgentContext secondContext,
            ConnectionInvitationMessage inviteMessage = null,
            string inviteeconnectionId = null)
        {
            // Create invitation by the issuer
            var connectionSecondId = Guid.NewGuid().ToString();

            var inviteConfig = new InviteConfiguration
            {
                ConnectionId = connectionSecondId,
                MyAlias      = new ConnectionAlias
                {
                    Name     = "Issuer",
                    ImageUrl = "www.issuerdomain.com/profilephoto"
                },
                TheirAlias = new ConnectionAlias
                {
                    Name     = "Holder",
                    ImageUrl = "www.holderdomain.com/profilephoto"
                }
            };

            if (inviteMessage == null)
            {
                (inviteMessage, _) = await connectionService.CreateInvitationAsync(firstContext, inviteConfig);
            }

            var connectionFirst = await connectionService.GetAsync(firstContext, inviteeconnectionId ?? inviteConfig.ConnectionId);

            Assert.Equal(ConnectionState.Invited, connectionFirst.State);

            // Holder accepts invitation and sends a message request
            (var request, var inviteeConnection) = await connectionService.CreateRequestAsync(secondContext, inviteMessage);

            var connectionSecond = inviteeConnection;

            _messages.TryAdd(request);

            Assert.Equal(ConnectionState.Negotiating, connectionSecond.State);

            // Issuer processes incoming message
            var issuerMessage = _messages.OfType <ConnectionRequestMessage>().FirstOrDefault();

            Assert.NotNull(issuerMessage);

            // Issuer processes the connection request by storing it and accepting it if auto connection flow is enabled
            connectionSecondId = await connectionService.ProcessRequestAsync(firstContext, issuerMessage, connectionFirst);

            connectionFirst = await connectionService.GetAsync(firstContext, connectionSecondId);

            Assert.Equal(ConnectionState.Negotiating, connectionFirst.State);

            // Issuer accepts the connection request
            (var response, var _) = await connectionService.CreateResponseAsync(firstContext, connectionSecondId);

            _messages.TryAdd(response);

            connectionFirst = await connectionService.GetAsync(firstContext, connectionSecondId);

            Assert.Equal(ConnectionState.Connected, connectionFirst.State);

            // Holder processes incoming message
            var holderMessage = _messages.OfType <ConnectionResponseMessage>().FirstOrDefault();

            Assert.NotNull(holderMessage);

            // Holder processes the response message by accepting it
            await connectionService.ProcessResponseAsync(secondContext, holderMessage, connectionSecond);

            // Retrieve updated connection state for both issuer and holder
            connectionFirst = await connectionService.GetAsync(firstContext, connectionFirst.Id);

            connectionSecond = await connectionService.GetAsync(secondContext, connectionSecond.Id);

            return(connectionFirst, connectionSecond);
        }
 public Task <(ConnectionInvitationMessage, ConnectionRecord)> CreateInvitationAsync(IAgentContext agentContext, InviteConfiguration config = null)
 {
     throw new System.NotImplementedException();
 }
        /// <inheritdoc />
        public virtual async Task <(ConnectionInvitationMessage, ConnectionRecord)> CreateInvitationAsync(IAgentContext agentContext,
                                                                                                          InviteConfiguration config = null)
        {
            var connectionId = !string.IsNullOrEmpty(config?.ConnectionId)
                ? config.ConnectionId
                : Guid.NewGuid().ToString();

            config = config ?? new InviteConfiguration();

            Logger.LogInformation(LoggingEvents.CreateInvitation, "ConnectionId {0}", connectionId);

            var connectionKey = await Crypto.CreateKeyAsync(agentContext.Wallet, "{}");

            var connection = new ConnectionRecord {
                Id = connectionId
            };

            connection.SetTag(TagConstants.ConnectionKey, connectionKey);

            //if (config.AutoAcceptConnection)
            connection.SetTag(TagConstants.AutoAcceptConnection, "true");

            connection.MultiPartyInvitation = config.MultiPartyInvitation;

            if (!config.MultiPartyInvitation)
            {
                connection.Alias = config.TheirAlias;
                if (!string.IsNullOrEmpty(config.TheirAlias.Name))
                {
                    connection.SetTag(TagConstants.Alias, config.TheirAlias.Name);
                }
            }

            foreach (var tag in config.Tags)
            {
                connection.SetTag(tag.Key, tag.Value);
            }

            var provisioning = await ProvisioningService.GetProvisioningAsync(agentContext.Wallet);

            string uri = "";

            if (string.IsNullOrEmpty(provisioning.Endpoint.Uri))
            {
                var records = await CloudAgentRegistrationService.GetAllCloudAgentAsync(agentContext.Wallet);

                if (records.Count > 0)
                {
                    var record = CloudAgentRegistrationService.getRandomCloudAgent(records);
                    uri = record.Endpoint.ResponseEndpoint + "/" + record.MyConsumerId;
                }
                else
                {
                    throw new AgentFrameworkException(ErrorCode.RecordInInvalidState, "No Cloud Agent Registered");
                }
            }

            await RecordService.AddAsync(agentContext.Wallet, connection);

            return(new ConnectionInvitationMessage
            {
                ServiceEndpoint = uri,
                RoutingKeys = null,        //provisioning.Endpoint.Verkey != null ? new[] {provisioning.Endpoint.Verkey} : null,
                RecipientKeys = new[] { connectionKey },
                Label = config.MyAlias.Name ?? provisioning.Owner.Name,
                ImageUrl = config.MyAlias.ImageUrl ?? provisioning.Owner.ImageUrl
            }, connection);
        }
Esempio n. 4
0
        /// <inheritdoc />
        public override async Task <(ConnectionInvitationMessage, ConnectionRecord)> CreateInvitationAsync(IAgentContext agentContext, InviteConfiguration config = null)
        {
            var(message, record) = await base.CreateInvitationAsync(agentContext, config);

            await edgeClientService.AddRouteAsync(agentContext, message.RecipientKeys.First());

            return(message, record);
        }
 public CreateInvitationRequest(InviteConfiguration aInviteConfiguration)
 {
     InviteConfiguration = aInviteConfiguration;
 }
 public Task <CreateInvitationResult> CreateInvitationAsync(IAgentContext agentContext, InviteConfiguration config = null)
 {
     throw new System.NotImplementedException();
 }