public async Task <IActionResult> VerifyPresentation(OperationBody body) { var context = await _agentContextProvider.GetContextAsync(); var threadId = body.Id; var THPresentationExchange = _proofCache.Get <TestHarnessPresentationExchange>(threadId); var proofRecord = await _proofService.GetByThreadIdAsync(context, THPresentationExchange.ThreadId); var connectionRecord = await _connectionService.GetAsync(context, proofRecord.ConnectionId); _logger.LogInformation("VerifyPresentation {proofRecord}", proofRecord.ToJson()); var isValid = await _proofService.VerifyProofAsync(context, THPresentationExchange.RecordId); if (!isValid) { return(Problem("Proof is not valid")); } THPresentationExchange.State = TestHarnessPresentationExchangeState.Done; var ackPresentationMessage = new AckPresentationMessage() { Status = "OK" }; ackPresentationMessage.ThreadFrom(threadId); await _messageService.SendAsync(context, ackPresentationMessage, connectionRecord); return(Ok(THPresentationExchange)); }
async Task VerifyProof() { var dialog = UserDialogs.Instance.Loading("Verifying"); try { var context = await agentContextProvider.GetContextAsync(); bool success = await proofService.VerifyProofAsync(context, proofRecord.Id); if (dialog.IsShowing) { dialog.Hide(); dialog.Dispose(); } DialogService.Alert( success ? "Verified" : "Failed" ); } catch (Exception ex) { if (dialog.IsShowing) { dialog.Hide(); dialog.Dispose(); } DialogService.Alert(ex.Message); } }
/// <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); }
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.CreateRequestAsync(requestorContext, proofRequestObject, requestorConnection.Id); messages.TryAdd(message); // Holder accepts the proof requests and builds a proof var proofRequest = FindContentMessage <RequestPresentationMessage>(messages); Assert.NotNull(proofRequest); //Holder stores the proof request var holderProofRequestRecord = await proofService.ProcessRequestAsync(holderContext, proofRequest, holderConnection); var holderProofRecord = await proofService.GetAsync(holderContext, holderProofRequestRecord.Id); 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.CreatePresentationAsync( holderContext, holderProofRequestRecord.Id, requestedCredentials); messages.TryAdd(proofMessage); //Requestor retrives proof message from their cloud agent var proof = FindContentMessage <PresentationMessage>(messages); Assert.NotNull(proof); //Requestor stores proof requestorProofRecord = await proofService.ProcessPresentationAsync(requestorContext, proof); //Requestor verifies proof var requestorVerifyResult = await proofService.VerifyProofAsync(requestorContext, requestorProofRecord.Id); //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); }
public async Task <IActionResult> VerifyPresentation(OperationBody body) { // Adding a delay here. It seems that with the removal of the state checks in the tests, // Some agents are not yet in the appropriate state for this call. // TODO There may be a better way to do this but adding the wait is a quick fix to get the tests // running again. await Task.Delay(10000); var context = await _agentContextProvider.GetContextAsync(); var threadId = body.Id; var THPresentationExchange = _proofCache.Get <TestHarnessPresentationExchange>(threadId); var proofRecord = await _proofService.GetByThreadIdAsync(context, THPresentationExchange.ThreadId); var connectionRecord = await _connectionService.GetAsync(context, proofRecord.ConnectionId); _logger.LogInformation("VerifyPresentation {proofRecord}", proofRecord.ToJson()); var isValid = await _proofService.VerifyProofAsync(context, THPresentationExchange.RecordId); if (!isValid) { return(Problem("Proof is not valid")); } THPresentationExchange.State = TestHarnessPresentationExchangeState.Done; var ackPresentationMessage = new AckPresentationMessage() { Status = "OK" }; ackPresentationMessage.ThreadFrom(threadId); await _messageService.SendAsync(context, ackPresentationMessage, connectionRecord); return(Ok(THPresentationExchange)); }
public async Task CredentialProofDemo() { int events = 0; _eventAggregator.GetEventByType <ServiceMessageProcessingEvent>() .Where(_ => (_.MessageType == MessageTypes.ProofRequest || _.MessageType == MessageTypes.DisclosedProof)) .Subscribe(_ => { events++; }); //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, _pool, MasterSecretId, true); _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 = "123", RequestedAttributes = new Dictionary <string, ProofAttributeInfo> { { "first-name-requirement", new ProofAttributeInfo { Name = "first_name" } } } }; //Requestor sends a proof request await _proofService.SendProofRequestAsync(_requestorWallet, requestorConnection.Id, proofRequestObject); } // 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); _holderWallet.Connection = holderConnection; //Holder stores the proof request var holderProofRequestId = await _proofService.ProcessProofRequestAsync(_holderWallet, proofRequest); 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, Timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds() }); } 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 = await _proofService.AcceptProofRequestAsync(_holderWallet, holderProofRequestId, requestedCredentials); _messages.Add(proofMessage); } //Requestor retrives proof message from their cloud agent var proof = FindContentMessage <ProofMessage>(); Assert.NotNull(proof); _requestorWallet.Connection = requestorConnection; //Requestor stores proof var requestorProofId = await _proofService.ProcessProofAsync(_requestorWallet, proof); //Requestor verifies proof var requestorVerifyResult = await _proofService.VerifyProofAsync(_requestorWallet, requestorProofId); ////Verify the proof is valid Assert.True(requestorVerifyResult); Assert.True(events == 2); ////Get the proof from both parties wallets //var requestorProof = await _proofService.GetProof(_requestorWallet, requestorProofId); //var holderProof = await _proofService.GetProof(_holderWallet, holderProofRequestId); ////Verify that both parties have a copy of the proof //Assert.Equal(requestorProof, holderProof); }