예제 #1
0
        /// <inheritdoc />
        public virtual async Task <EphemeralChallengeResponseMessage> CreateProofChallengeResponseAsync(IAgentContext agentContext, EphemeralChallengeMessage message, RequestedCredentials credentials)
        {
            if (message.Challenge.Type != ChallengeType.Proof)
            {
                throw new AgentFrameworkException(ErrorCode.InvalidMessage, "Challenge.Type != Proof");
            }

            var challengeResponse = new EphemeralChallengeResponseMessage
            {
                Id     = Guid.NewGuid().ToString(),
                Status = EphemeralChallengeResponseStatus.Accepted
            };

            var proofRequest = message.Challenge.Contents.ToObject <ProofRequest>();

            var proof = await ProofService.CreateProofAsync(agentContext, proofRequest, credentials);

            challengeResponse.Response = new EphemeralChallengeContents
            {
                Type     = ChallengeType.Proof,
                Contents = JsonConvert.DeserializeObject <JObject>(proof)
            };

            challengeResponse.ThreadFrom(message);

            return(challengeResponse);
        }
예제 #2
0
        /// <inheritdoc />
        public virtual async Task <string> ProcessChallengeResponseAsync(IAgentContext agentContext,
                                                                         EphemeralChallengeResponseMessage challengeResponse)
        {
            var threadId = challengeResponse.GetThreadId();

            //TODO improve this
            var results = await ListChallengesAsync(agentContext, new EqSubquery(TagConstants.LastThreadId, threadId));

            var record = results.FirstOrDefault();

            if (record == null)
            {
                throw new AgentFrameworkException(ErrorCode.RecordNotFound, "Challenge not found");
            }

            if (record.State != ChallengeState.Challenged)
            {
                throw new AgentFrameworkException(ErrorCode.RecordInInvalidState,
                                                  $"Challenge state was invalid. Expected '{ChallengeState.Challenged}', found '{record.State}'");
            }

            if (record.State != ChallengeState.Challenged)
            {
                throw new AgentFrameworkException(ErrorCode.RecordNotFound, "Challenge not found");
            }

            record.Response = challengeResponse.Response;

            if (challengeResponse.Status == EphemeralChallengeResponseStatus.Accepted)
            {
                var result = await ProofService.VerifyProofAsync(agentContext, record.Challenge.Contents.ToJson(),
                                                                 record.Response.Contents.ToJson());

                if (result)
                {
                    await record.TriggerAsync(ChallengeTrigger.AcceptChallenge);
                }
                else
                {
                    await record.TriggerAsync(ChallengeTrigger.InvalidChallengeResponse);
                }
            }
            else
            {
                await record.TriggerAsync(ChallengeTrigger.RejectChallenge);
            }

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

            EventAggregator.Publish(new ServiceMessageProcessingEvent
            {
                MessageType = MessageTypes.EphemeralChallenge,
                RecordId    = record.Id,
                ThreadId    = challengeResponse.GetThreadId()
            });

            return(record.Id);
        }