/// <inheritdoc />
        public async Task <string> ProcessResponseAsync(IAgentContext agentContext, ConnectionResponseMessage response)
        {
            Logger.LogInformation(LoggingEvents.AcceptConnectionResponse, "To {1}", agentContext.Connection.MyDid);

            await Did.StoreTheirDidAsync(agentContext.Wallet,
                                         new { did = response.Did, verkey = response.Verkey }.ToJson());

            await Pairwise.CreateAsync(agentContext.Wallet, response.Did, agentContext.Connection.MyDid,
                                       response.Endpoint.ToJson());

            agentContext.Connection.TheirDid = response.Did;
            agentContext.Connection.TheirVk  = response.Verkey;

            if (response.Endpoint != null)
            {
                agentContext.Connection.Endpoint = response.Endpoint;
            }

            await agentContext.Connection.TriggerAsync(ConnectionTrigger.Response);

            await RecordService.UpdateAsync(agentContext.Wallet, agentContext.Connection);

            EventAggregator.Publish(new ServiceMessageProcessingEvent
            {
                RecordId    = agentContext.Connection.Id,
                MessageType = response.Type,
            });

            return(agentContext.Connection.Id);
        }
예제 #2
0
        /// <inheritdoc />
        public async Task ProcessResponseAsync(Wallet wallet, ConnectionResponseMessage response)
        {
            Logger.LogInformation(LoggingEvents.AcceptConnectionResponse, "To {0}", response.To);

            var(didOrKey, _) = MessageUtils.ParseMessageType(response.Type);

            var connectionSearch = await RecordService.SearchAsync <ConnectionRecord>(wallet,
                                                                                      new SearchRecordQuery { { TagConstants.MyDid, didOrKey } }, null, 1);

            var connection = connectionSearch.Single();
            await connection.TriggerAsync(ConnectionTrigger.Response);

            var(connectionDetails, _) = await MessageSerializer.UnpackSealedAsync <ConnectionDetails>(response.Content,
                                                                                                      wallet, connection.MyVk);

            await Did.StoreTheirDidAsync(wallet,
                                         new { did = connectionDetails.Did, verkey = connectionDetails.Verkey }.ToJson());

            await Pairwise.CreateAsync(wallet, connectionDetails.Did, connection.MyDid,
                                       connectionDetails.Endpoint.ToJson());

            connection.TheirDid = connectionDetails.Did;
            connection.TheirVk  = connectionDetails.Verkey;

            if (connectionDetails.Endpoint != null)
            {
                connection.Endpoint = connectionDetails.Endpoint;
            }

            connection.Tags.Add(TagConstants.TheirDid, connectionDetails.Did);
            await RecordService.UpdateAsync(wallet, connection);
        }
        /// <inheritdoc />
        public virtual async Task <(ConnectionResponseMessage, ConnectionRecord)> CreateResponseAsync(IAgentContext agentContext, string connectionId)
        {
            Logger.LogInformation(LoggingEvents.AcceptConnectionRequest, "ConnectionId {0}", connectionId);

            var connection = await GetAsync(agentContext, connectionId);

            if (connection.State != ConnectionState.Negotiating)
            {
                throw new AgentFrameworkException(ErrorCode.RecordInInvalidState,
                                                  $"Connection state was invalid. Expected '{ConnectionState.Negotiating}', found '{connection.State}'");
            }

            await Pairwise.CreateAsync(agentContext.Wallet, connection.TheirDid, connection.MyDid, connection.Endpoint.ToJson());

            await connection.TriggerAsync(ConnectionTrigger.Request);

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

            // Send back response message
            var provisioning = await ProvisioningService.GetProvisioningAsync(agentContext.Wallet);

            string responseEndpoint = string.Empty;
            var    records          = await CloudAgentRegistrationService.GetAllCloudAgentAsync(agentContext.Wallet);

            if (records.Count > 0)
            {
                var record = CloudAgentRegistrationService.getRandomCloudAgent(records);
                responseEndpoint = record.Endpoint.ResponseEndpoint + "/" + record.MyConsumerId;
            }

            var connectionData = new Connection
            {
                Did    = connection.MyDid,
                DidDoc = connection.MyDidDoc(provisioning, responseEndpoint)
            };

            var sigData = await SignatureUtils.SignData(agentContext, connectionData, connection.GetTag(TagConstants.ConnectionKey));

            var threadId = connection.GetTag(TagConstants.LastThreadId);

            var response = new ConnectionResponseMessage {
                ConnectionSig = sigData
            };

            response.ThreadFrom(threadId);

            return(response, connection);
        }
        public static async Task <ASConnection> AcceptConnectionAsync(AppServiceTriggerDetails e,
                                                                      BackgroundTaskDeferral connectionDeferral, IObjectSerializer serializer)
        {
            // Receive connection request, send connection response
            AppServiceRequest  request  = null;
            AppServiceDeferral deferral = null;

            var gotRequest = new SemaphoreSlim(0);

            e.AppServiceConnection.RequestReceived += OnRequestReceived;
            await gotRequest.WaitAsync();

            e.AppServiceConnection.RequestReceived -= OnRequestReceived;

            var message = serializer.DeserializeFromValueSet(request.Message);

            if (message is ConnectionRequestMessage connectionRequest)
            {
                // Accept connection request
                var connectionId       = "AS_" + Guid.NewGuid();
                var connectionResponse = new ConnectionResponseMessage(connectionId);
                await request.SendResponseAsync(serializer.SerializeToValueSet(connectionResponse));

                deferral.Complete();
                return(new ASConnection(
                           connectionId, e.AppServiceConnection, connectionDeferral,
                           e.IsRemoteSystemConnection, serializer));
            }
            else
            {
                // Wrong message received => reject connection
                var connectionResponse = new ConnectionResponseMessage(null);
                await request.SendResponseAsync(serializer.SerializeToValueSet(connectionResponse));

                deferral.Complete();
                connectionDeferral.Complete();
                e.AppServiceConnection.Dispose();
                return(null);
            }

            void OnRequestReceived(AppServiceConnection _, AppServiceRequestReceivedEventArgs r)
            {
                request  = r.Request;
                deferral = r.GetDeferral();
                gotRequest.Release();
            }
        }
        /// <inheritdoc />
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
                                        JsonSerializer serializer)
        {
            var item = JObject.Load(reader);

            var(_, messageType) = MessageUtils.ParseMessageType(item["@type"].ToObject <string>());

            IContentMessage message;

            switch (messageType)
            {
            case MessageTypes.ConnectionRequest:
                message = new ConnectionRequestMessage();
                break;

            case MessageTypes.ConnectionResponse:
                message = new ConnectionResponseMessage();
                break;

            case MessageTypes.CredentialOffer:
                message = new CredentialOfferMessage();
                break;

            case MessageTypes.CredentialRequest:
                message = new CredentialRequestMessage();
                break;

            case MessageTypes.Credential:
                message = new CredentialMessage();
                break;

            case MessageTypes.ProofRequest:
                message = new ProofRequestMessage();
                break;

            case MessageTypes.DisclosedProof:
                message = new ProofMessage();
                break;

            default: throw new TypeLoadException("Unsupported serialization type.");
            }

            serializer.Populate(item.CreateReader(), message);
            return(message);
        }
예제 #6
0
        internal StartSessionResult(ConnectionResponseMessage connectionResponseMessage, SpeechRecognitionSession session)
        {
            if (connectionResponseMessage == null)
            {
                throw new ArgumentNullException(nameof(connectionResponseMessage));
            }

            TransportStatus = TransportStatus.Ok;
            SocketError     = SocketError.Success;
            ResponseCode    = connectionResponseMessage.ResponseCode;
            ApiErrorMessage = connectionResponseMessage.Message;
            SessionId       = connectionResponseMessage.SessionId;

            if (ResponseCode == ResponseCode.Ok)
            {
                Session = session ?? throw new ArgumentNullException(nameof(session));
            }
        }
        /// <inheritdoc />
        public virtual async Task <(ConnectionResponseMessage, ConnectionRecord)> CreateResponseAsync(IAgentContext agentContext, string connectionId)
        {
            Logger.LogTrace(LoggingEvents.AcceptConnectionRequest, "ConnectionId {0}", connectionId);

            var connection = await GetAsync(agentContext, connectionId);

            if (connection.State != ConnectionState.Negotiating)
            {
                throw new AriesFrameworkException(ErrorCode.RecordInInvalidState,
                                                  $"Connection state was invalid. Expected '{ConnectionState.Negotiating}', found '{connection.State}'");
            }

            await connection.TriggerAsync(ConnectionTrigger.Response);

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

            // Send back response message
            var provisioning = await ProvisioningService.GetProvisioningAsync(agentContext.Wallet);

            var connectionData = new Common.Connection
            {
                Did    = connection.MyDid,
                DidDoc = connection.MyDidDoc(provisioning)
            };

            var sigData = await SignatureUtils.SignDataAsync(agentContext, connectionData, connection.GetTag(TagConstants.ConnectionKey));

            var threadId = connection.GetTag(TagConstants.LastThreadId);

            var response = new ConnectionResponseMessage(agentContext.UseMessageTypesHttps)
            {
                ConnectionSig = sigData
            };

            response.ThreadFrom(threadId);

            return(response, connection);
        }
예제 #8
0
 public Task <string> ProcessResponseAsync(IAgentContext agentContext, ConnectionResponseMessage response, ConnectionRecord connection)
 {
     throw new System.NotImplementedException();
 }
        /// <inheritdoc />
        public virtual async Task <string> ProcessResponseAsync(IAgentContext agentContext, ConnectionResponseMessage response, ConnectionRecord connection)
        {
            Logger.LogTrace(LoggingEvents.AcceptConnectionResponse, "To {1}", connection.MyDid);

            await connection.TriggerAsync(ConnectionTrigger.Response);

            //TODO throw exception or a problem report if the connection request features a did doc that has no indy agent did doc convention featured
            //i.e there is no way for this agent to respond to messages. And or no keys specified
            var connectionObj = await SignatureUtils.UnpackAndVerifyAsync <Common.Connection>(response.ConnectionSig);

            await Did.StoreTheirDidAsync(agentContext.Wallet,
                                         new { did = connectionObj.Did, verkey = connectionObj.DidDoc.Keys[0].PublicKeyBase58 }.ToJson());

            connection.TheirDid = connectionObj.Did;
            connection.TheirVk  = connectionObj.DidDoc.Keys[0].PublicKeyBase58;

            connection.SetTag(TagConstants.LastThreadId, response.GetThreadId());

            if (connectionObj.DidDoc.Services[0] is IndyAgentDidDocService service)
            {
                connection.Endpoint = new AgentEndpoint(service.ServiceEndpoint, null, service.RoutingKeys != null && service.RoutingKeys.Count > 0 ? service.RoutingKeys.ToArray() : null);
            }

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

            EventAggregator.Publish(new ServiceMessageProcessingEvent
            {
                RecordId    = connection.Id,
                MessageType = response.Type,
                ThreadId    = response.GetThreadId()
            });

            return(connection.Id);
        }
 Task <string> IConnectionService.ProcessResponseAsync(IAgentContext agentContext, ConnectionResponseMessage response)
 {
     throw new System.NotImplementedException();
 }