Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public static async Task <(ProofRecord holderProofRecord, ProofRecord RequestorProofRecord)> ProofProtocolAsync(
            IProofService proofService,
            IProducerConsumerCollection <AgentMessage> messages,
            ConnectionRecord holderConnection, ConnectionRecord requestorConnection,
            IAgentContext holderContext,
            IAgentContext requestorContext, ProofRequest proofRequestObject)
        {
            //Requestor sends a proof request
            var(message, requestorProofRecord) = await proofService.CreateProofRequestAsync(requestorContext, proofRequestObject, requestorConnection.Id);

            messages.TryAdd(message);

            // Holder accepts the proof requests and builds a proof
            var proofRequest = FindContentMessage <ProofRequestMessage>(messages);

            Assert.NotNull(proofRequest);

            //Holder stores the proof request
            var holderProofRequestId = await proofService.ProcessProofRequestAsync(holderContext, proofRequest, holderConnection);

            var holderProofRecord = await proofService.GetAsync(holderContext, holderProofRequestId);

            var holderProofRequest = JsonConvert.DeserializeObject <ProofRequest>(holderProofRecord.RequestJson);

            // Auto satify the proof with which ever credentials in the wallet are capable
            var requestedCredentials =
                await ProofServiceUtils.GetAutoRequestedCredentialsForProofCredentials(holderContext, proofService,
                                                                                       holderProofRequest);

            //Holder accepts the proof request and sends a proof
            (var proofMessage, _) = await proofService.CreateProofAsync(holderContext, holderProofRequestId, requestedCredentials);

            messages.TryAdd(proofMessage);

            //Requestor retrives proof message from their cloud agent
            var proof = FindContentMessage <ProofMessage>(messages);

            Assert.NotNull(proof);

            //Requestor stores proof
            var requestorProofId = await proofService.ProcessProofAsync(requestorContext, proof);

            //Requestor verifies proof
            var requestorVerifyResult = await proofService.VerifyProofAsync(requestorContext, requestorProofId);

            ////Verify the proof is valid
            Assert.True(requestorVerifyResult);

            var requestorProofRecordResult = await proofService.GetAsync(requestorContext, requestorProofRecord.Id);

            var holderProofRecordResult = await proofService.GetAsync(holderContext, holderProofRecord.Id);

            return(holderProofRecordResult, requestorProofRecordResult);
        }
Ejemplo n.º 3
0
        public async Task ProcessProofInvalidState()
        {
            //Setup a connection and issue the credentials to the holder
            var(issuerConnection, holderConnection) = await Scenarios.EstablishConnectionAsync(
                _connectionService, _messages, _issuerWallet, _holderWallet);

            await Scenarios.IssueCredentialAsync(
                _schemaService, _credentialService, _messages, issuerConnection,
                holderConnection, _issuerWallet, _holderWallet, await _holderWallet.Pool, TestConstants.DefaultMasterSecret, true, new List <CredentialPreviewAttribute>
            {
                new CredentialPreviewAttribute("first_name", "Test"),
                new CredentialPreviewAttribute("last_name", "Holder")
            });

            _messages.Clear();

            //Requestor initialize a connection with the holder
            var(_, requestorConnection) = await Scenarios.EstablishConnectionAsync(
                _connectionService, _messages, _holderWallet, _requestorWallet);

            // Verifier sends a proof request to prover
            {
                var proofRequestObject = new ProofRequest
                {
                    Name                = "ProofReq",
                    Version             = "1.0",
                    Nonce               = await AnonCreds.GenerateNonceAsync(),
                    RequestedAttributes = new Dictionary <string, ProofAttributeInfo>
                    {
                        { "first-name-requirement", new ProofAttributeInfo {
                              Name = "first_name"
                          } }
                    }
                };

                //Requestor sends a proof request
                var(message, _) = await _proofService.CreateProofRequestAsync(_requestorWallet, proofRequestObject, requestorConnection.Id);

                _messages.Add(message);
            }

            // Holder accepts the proof requests and builds a proof
            {
                //Holder retrives proof request message from their cloud agent
                var proofRequest = FindContentMessage <ProofRequestMessage>();
                Assert.NotNull(proofRequest);

                //Holder stores the proof request
                var holderProofRequestId = await _proofService.ProcessProofRequestAsync(_holderWallet, proofRequest, holderConnection);

                var holderProofRecord = await _proofService.GetAsync(_holderWallet, holderProofRequestId);

                var holderProofObject =
                    JsonConvert.DeserializeObject <ProofRequest>(holderProofRecord.RequestJson);

                var requestedCredentials = new RequestedCredentials();
                foreach (var requestedAttribute in holderProofObject.RequestedAttributes)
                {
                    var credentials =
                        await _proofService.ListCredentialsForProofRequestAsync(_holderWallet, holderProofObject,
                                                                                requestedAttribute.Key);

                    requestedCredentials.RequestedAttributes.Add(requestedAttribute.Key,
                                                                 new RequestedAttribute
                    {
                        CredentialId = credentials.First().CredentialInfo.Referent,
                        Revealed     = true
                    });
                }

                foreach (var requestedAttribute in holderProofObject.RequestedPredicates)
                {
                    var credentials =
                        await _proofService.ListCredentialsForProofRequestAsync(_holderWallet, holderProofObject,
                                                                                requestedAttribute.Key);

                    requestedCredentials.RequestedPredicates.Add(requestedAttribute.Key,
                                                                 new RequestedAttribute
                    {
                        CredentialId = credentials.First().CredentialInfo.Referent,
                        Revealed     = true
                    });
                }

                //Holder accepts the proof request and sends a proof
                (var proofMessage, var _) = await _proofService.CreateProofAsync(_holderWallet, holderProofRequestId,
                                                                                 requestedCredentials);

                _messages.Add(proofMessage);
            }

            //Requestor retrives proof message from their cloud agent
            var proof = FindContentMessage <ProofMessage>();

            Assert.NotNull(proof);

            //Requestor stores proof
            await _proofService.ProcessProofAsync(_requestorWallet, proof);

            var ex = await Assert.ThrowsAsync <AgentFrameworkException>(async() => await _proofService.ProcessProofAsync(_requestorWallet, proof));

            Assert.True(ex.ErrorCode == ErrorCode.RecordInInvalidState);
        }