private async Task <string> SignAndEncodePresentationRequestBody(
            HttpClient client,
            V1_GetDidResponse did,
            V1_CreatePresentationRequestResponse v1CreatePresentationRequestResponse)
        {
            var createDidUrl = $"https://{_mattrConfiguration.TenantSubdomain}/v1/messaging/sign";

            object didUrlArray;

            did.DidDocument.AdditionalProperties.TryGetValue("authentication", out didUrlArray);
            var didUrl  = didUrlArray.ToString().Split("\"")[1];
            var payload = new MattrOpenApiClient.SignMessageRequest
            {
                DidUrl  = didUrl,
                Payload = v1CreatePresentationRequestResponse.Request
            };
            var payloadJson = JsonConvert.SerializeObject(payload);
            var uri         = new Uri(createDidUrl);

            using (var content = new StringContentWithoutCharset(payloadJson, "application/json"))
            {
                var response = await client.PostAsync(uri, content);

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var result = await response.Content.ReadAsStringAsync();

                    return(result);
                }

                var error = await response.Content.ReadAsStringAsync();
            }

            return(null);
        }
        private async Task <V1_CreatePresentationRequestResponse> InvokePresentationRequest(
            HttpClient client,
            string didId,
            string templateId,
            string challenge,
            string callbackUrl)
        {
            var createDidUrl = $"https://{_mattrConfiguration.TenantSubdomain}/v1/presentations/requests";

            var payload = new MattrOpenApiClient.V1_CreatePresentationRequestRequest
            {
                Did         = didId,
                TemplateId  = templateId,
                Challenge   = challenge,
                CallbackUrl = new Uri(callbackUrl),
                ExpiresTime = MATTR_EPOCH_EXPIRES_TIME_VERIFIY // Epoch time
            };
            var payloadJson = JsonConvert.SerializeObject(payload);
            var uri         = new Uri(createDidUrl);

            using (var content = new StringContentWithoutCharset(payloadJson, "application/json"))
            {
                var response = await client.PostAsync(uri, content);

                if (response.StatusCode == System.Net.HttpStatusCode.Created)
                {
                    var v1CreatePresentationRequestResponse = JsonConvert.DeserializeObject <V1_CreatePresentationRequestResponse>(
                        await response.Content.ReadAsStringAsync());

                    return(v1CreatePresentationRequestResponse);
                }

                var error = await response.Content.ReadAsStringAsync();
            }

            return(null);
        }
Ejemplo n.º 3
0
        private async Task <V1_PresentationTemplateResponse> CreateMattrPresentationTemplate(
            HttpClient client, string didId)
        {
            // create presentation, post to presentations templates api
            // https://learn.mattr.global/tutorials/verify/presentation-request-template

            var createPresentationsTemplatesUrl = $"https://{_mattrConfiguration.TenantSubdomain}/v1/presentations/templates";

            var additionalProperties = new Dictionary <string, object>();

            additionalProperties.Add("type", "QueryByExample");
            additionalProperties.Add("credentialQuery", new List <CredentialQuery> {
                new CredentialQuery
                {
                    Reason   = "Please provide your driving license",
                    Required = true,
                    Example  = new Example
                    {
                        Context = new List <object> {
                            "https://schema.org"
                        },
                        Type          = "VerifiableCredential",
                        TrustedIssuer = new List <TrustedIssuer2>
                        {
                            new TrustedIssuer2
                            {
                                Required = true,
                                Issuer   = didId // DID use to create the oidc
                            }
                        }
                    }
                }
            });

            var payload = new MattrOpenApiClient.V1_CreatePresentationTemplate
            {
                Domain = _mattrConfiguration.TenantSubdomain,
                Name   = "certificate-presentation",
                Query  = new List <Query>
                {
                    new Query
                    {
                        AdditionalProperties = additionalProperties
                    }
                }
            };

            var payloadJson = JsonConvert.SerializeObject(payload);

            var uri = new Uri(createPresentationsTemplatesUrl);

            using (var content = new StringContentWithoutCharset(payloadJson, "application/json"))
            {
                var presentationTemplateResponse = await client.PostAsync(uri, content);

                if (presentationTemplateResponse.StatusCode == System.Net.HttpStatusCode.Created)
                {
                    var v1PresentationTemplateResponse = JsonConvert
                                                         .DeserializeObject <MattrOpenApiClient.V1_PresentationTemplateResponse>(
                        await presentationTemplateResponse.Content.ReadAsStringAsync());

                    return(v1PresentationTemplateResponse);
                }

                var error = await presentationTemplateResponse.Content.ReadAsStringAsync();
            }

            throw new Exception("whoops something went wrong");
        }