-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/docusign/code-examples-cs…
- Loading branch information
Showing
20 changed files
with
1,773 additions
and
1,563 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
launcher-csharp/Connect/Controllers/ValidateWebhookMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// <copyright file="ValidateWebhookMessage.cs" company="DocuSign"> | ||
// Copyright (c) DocuSign. All rights reserved. | ||
// </copyright> | ||
|
||
namespace DocuSign.CodeExamples.Connect.Controllers | ||
{ | ||
using DocuSign.CodeExamples.Common; | ||
using DocuSign.CodeExamples.Controllers; | ||
using DocuSign.CodeExamples.Models; | ||
using DocuSign.Connect.Examples; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
[Area("Connect")] | ||
[Route("con001")] | ||
public class ValidateWebhookMessage : EgController | ||
{ | ||
public ValidateWebhookMessage(DsConfiguration config, LauncherTexts launcherTexts, IRequestItemsService requestItemsService) | ||
: base(config, launcherTexts, requestItemsService) | ||
{ | ||
this.CodeExampleText = this.GetExampleText(this.EgName, ExamplesApiType.Connect); | ||
this.ViewBag.title = this.CodeExampleText.ExampleName; | ||
} | ||
|
||
public override string EgName => "con001"; | ||
|
||
[SetViewBag] | ||
[HttpPost] | ||
[ValidateAntiForgeryToken] | ||
public ActionResult ValidateMessageUsing(string hmacSecret, string jsonPayload) | ||
{ | ||
jsonPayload = jsonPayload.Replace("\\", string.Empty); | ||
|
||
string hashedOutput = HMACValidation.ComputeHash(hmacSecret, jsonPayload); | ||
|
||
this.ViewBag.h1 = this.CodeExampleText.ExampleName; | ||
this.ViewBag.message = this.CodeExampleText.ResultsPageText.Replace("{0}", hashedOutput); | ||
|
||
return this.View("example_done"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// <copyright file="HMACValidation.cs" company="DocuSign"> | ||
// Copyright (c) DocuSign. All rights reserved. | ||
// </copyright> | ||
|
||
namespace DocuSign.Connect.Examples | ||
{ | ||
using System; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
public static class HMACValidation | ||
{ | ||
//ds-snippet-start:Connect1Step1 | ||
public static string ComputeHash(string secret, string payload) | ||
{ | ||
byte[] bytes = Encoding.UTF8.GetBytes(secret); | ||
var hmac = new HMACSHA256(bytes); | ||
bytes = Encoding.UTF8.GetBytes(payload); | ||
|
||
return Convert.ToBase64String(hmac.ComputeHash(bytes)); | ||
} | ||
|
||
public static bool HashIsValid(string secret, string payload, string verify) | ||
{ | ||
ReadOnlySpan<byte> hashBytes = Convert.FromBase64String(ComputeHash(secret, payload)); | ||
ReadOnlySpan<byte> verifyBytes = Convert.FromBase64String(verify); | ||
|
||
return CryptographicOperations.FixedTimeEquals(hashBytes, verifyBytes); | ||
} | ||
|
||
//ds-snippet-end:Connect1Step1 | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
launcher-csharp/Connect/Views/ValidateWebhookMessage/Con001.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
@{ | ||
int formNumber = 0; | ||
int hmacInputNumber = 0; | ||
int jsonInputNumber = 1; | ||
} | ||
|
||
<h4>@Html.Raw(ViewBag.CodeExampleText.ExampleName)</h4> | ||
<p> | ||
@Html.Raw(ViewBag.CodeExampleText.ExampleDescription) | ||
</p> | ||
|
||
<partial name="../../../Views/Shared/LinkToMethodView" model="ViewBag.CodeExampleText" /> | ||
<p> | ||
@Html.Raw( | ||
@String.Format( | ||
ViewBag.SupportingTexts.ViewSourceFile, | ||
"<a target='_blank' href=" + @ViewBag.source + ">HMACValidation.cs</a>" | ||
) | ||
) | ||
</p> | ||
|
||
<p> | ||
<b>Prerequisites:</b> See <a href="https://developers.docusign.com/platform/webhooks/connect/validate/">How to validate an HMAC signature</a>. | ||
</p> | ||
|
||
<form class="eg" action="" method="post" data-busy="form"> | ||
<div class="form-group" style="display: flex;"> | ||
<label for="hmacSecret" style="width: 50%;"> | ||
@Html.Raw(ViewBag.CodeExampleText.Forms[formNumber].Inputs[hmacInputNumber].InputName) | ||
</label> | ||
|
||
<input type="text" | ||
class="form-control" | ||
id="hmacSecret" | ||
name="hmacSecret" | ||
required/> | ||
</div> | ||
<div class="form-group"> | ||
<label for="jsonPayload"> | ||
@Html.Raw(ViewBag.CodeExampleText.Forms[formNumber].Inputs[jsonInputNumber].InputName) | ||
</label> | ||
|
||
<textarea | ||
rows="6" | ||
class="form-control" | ||
id="jsonPayload" | ||
name="jsonPayload" | ||
required ></textarea> | ||
</div> | ||
<input type="hidden" name="_csrf" value="@ViewBag.csrfToken"> | ||
<button type="submit" class="btn btn-primary">@Html.Raw(ViewBag.SupportingTexts.SubmitButton)</button> | ||
</form> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
@model DocuSign.CodeExamples.ESignature.Models.CodeExampleText | ||
@using Microsoft.EntityFrameworkCore.Migrations | ||
@model DocuSign.CodeExamples.ESignature.Models.CodeExampleText | ||
<p> | ||
@if(Model.LinksToApiMethod.Count == 1) | ||
@if (Model.LinksToApiMethod.Count != 0) | ||
{ | ||
<span>@Html.Raw(ViewBag.SupportingTexts.ApiMethodUsed)</span> | ||
} | ||
else | ||
{ | ||
<span>@Html.Raw(ViewBag.SupportingTexts.ApiMethodUsedPlural)</span> | ||
} | ||
|
||
@for(int i = 0; i < Model.LinksToApiMethod.Count; ++i) | ||
{ | ||
<a target='_blank' href="@Model.LinksToApiMethod[i].Path">@Html.Raw(Model.LinksToApiMethod[i].PathName)</a> | ||
@if(i + 1 == Model.LinksToApiMethod.Count) | ||
@if (Model.LinksToApiMethod.Count == 1) | ||
{ | ||
<span></span> | ||
<span>@Html.Raw(ViewBag.SupportingTexts.ApiMethodUsed)</span> | ||
} | ||
else if(i + 1 == Model.LinksToApiMethod.Count - 1) | ||
else | ||
{ | ||
<span> and </span> | ||
<span>@Html.Raw(ViewBag.SupportingTexts.ApiMethodUsedPlural)</span> | ||
} | ||
else | ||
|
||
@for (int i = 0; i < Model.LinksToApiMethod.Count; ++i) | ||
{ | ||
<span>, </span> | ||
<a target='_blank' href="@Model.LinksToApiMethod[i].Path">@Html.Raw(Model.LinksToApiMethod[i].PathName)</a> | ||
@if (i + 1 == Model.LinksToApiMethod.Count) | ||
{ | ||
<span></span> | ||
} | ||
else if (i + 1 == Model.LinksToApiMethod.Count - 1) | ||
{ | ||
<span> and </span> | ||
} | ||
else | ||
{ | ||
<span>, </span> | ||
} | ||
} | ||
} | ||
</p> |
Oops, something went wrong.