Skip to content

Commit

Permalink
Adding Web Forms API code example
Browse files Browse the repository at this point in the history
  • Loading branch information
InbarGazit committed Feb 7, 2024
1 parent b26c209 commit 8fa98b5
Show file tree
Hide file tree
Showing 21 changed files with 533 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,5 @@ __pycache__/
#configuration files (secrets), should not be checked in to source control
appsettings.json
private.key
app.config
app.config
web-form-config.json
6 changes: 6 additions & 0 deletions ExamplesAPIType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public enum ExamplesApiType
/// </summary>
[Description("con")]
Connect = 5,

/// <summary>
/// Web Forms API
/// </summary>
[Description("web")]
WebForms = 6,
}

public static class ExamplesApiTypeExtensions
Expand Down
6 changes: 6 additions & 0 deletions ExamplesApiTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public enum ExamplesApiType
/// </summary>
[Description("con")]
Connect = 5,

/// <summary>
/// Web Forms API
/// </summary>
[Description("web")]
WebForms = 6,
}

public static class ExamplesApiTypeExtensions
Expand Down
7 changes: 7 additions & 0 deletions JWTAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ public static OAuthToken AuthenticateWithJwt(string api, string clientId, string
});
}

if (apiType == ExamplesApiType.WebForms)
{
scopes.Add("webforms_read");
scopes.Add("webforms_instance_write");
scopes.Add("webforms_instance_read");
}

return docuSignClient.RequestJWTUserToken(
clientId,
impersonatedUserId,
Expand Down
3 changes: 3 additions & 0 deletions launcher-csharp.Tests/JwtLoginMethodUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ private string BuildConsentUrl(ExamplesApiType apiType, TestConfig testConfig)
scopes += "%20user_read%20user_write%20organization_read%20account_read%20group_read%20"
+ "permission_read%20identity_provider_read%20domain_read%20user_data_redact%20"
+ "asset_group_account_read%20asset_group_account_clone_write%20asset_group_account_clone_read";
} else if (apiType == ExamplesApiType.WebForms)
{
scopes += "%20webforms_manage";
}

string caret = "";
Expand Down
2 changes: 2 additions & 0 deletions launcher-csharp/Common/IRequestItemsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public interface IRequestItemsService

public string TemplateId { get; set; }

public string WebFormsTemplateId { get; set; }

public string PausedEnvelopeId { get; set; }

public string Status { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions launcher-csharp/Common/LocalsFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public void OnActionExecuting(ActionExecutingContext context)
BasePath = identity.FindFirst(x => x.Type.Equals("base_uri")).Value,
RoomsApiBasePath = this.configuration["DocuSign:RoomsApiEndpoint"],
AdminApiBasePath = this.configuration["DocuSign:AdminApiEndpoint"],
WebFormsBasePath = this.configuration["DocuSign:WebFormsBasePath"],
}
:
new Session
Expand All @@ -106,6 +107,7 @@ public void OnActionExecuting(ActionExecutingContext context)
BasePath = this.requestItemsService.Session.BasePath,
RoomsApiBasePath = this.configuration["DocuSign:RoomsApiEndpoint"],
AdminApiBasePath = this.configuration["DocuSign:AdminApiEndpoint"],
WebFormsBasePath = this.configuration["DocuSign:WebFormsBasePath"],
};

this.requestItemsService.Session = locals.Session;
Expand Down
11 changes: 11 additions & 0 deletions launcher-csharp/Common/RequestItemsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ public string TemplateId
set => this.cache.Set(this.GetKey("TemplateId"), value);
}

public string WebFormsTemplateId
{
get => this.cache.Get<string>(this.GetKey("WebFormsTemplateId"));
set => this.cache.Set(this.GetKey("WebFormsTemplateId"), value);
}

public string ClickwrapId
{
get => this.cache.Get<string>(this.GetKey("ClickwrapId"));
Expand Down Expand Up @@ -206,6 +212,7 @@ public void UpdateUserFromJwt()
BasePath = account.BaseUri,
RoomsApiBasePath = this.Configuration["DocuSign:RoomsApiEndpoint"],
AdminApiBasePath = this.Configuration["DocuSign:AdminApiEndpoint"],
WebFormsBasePath = this.Configuration["DocuSign:WebFormsBasePath"],
};
}

Expand Down Expand Up @@ -255,6 +262,10 @@ public string IdentifyApiOfCodeExample(string eg)
{
currentApiType = ExamplesApiType.Connect.ToString();
}
else if (eg.Contains(ExamplesApiType.WebForms.ToKeywordString()))
{
currentApiType = ExamplesApiType.WebForms.ToString();
}
else
{
currentApiType = ExamplesApiType.ESignature.ToString();
Expand Down
41 changes: 18 additions & 23 deletions launcher-csharp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ namespace DocuSign.CodeExamples
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using DocuSign.CodeExamples.Common;
using DocuSign.CodeExamples.Models;
using DocuSign.Rooms.Api;
Expand All @@ -35,7 +38,7 @@ public Startup(IConfiguration configuration)
{
this.Configuration = configuration;

this.apiTypes.Add(ExamplesApiType.ESignature, new List<string> { "signature" });
this.apiTypes.Add(ExamplesApiType.ESignature, new List<string> { "signature", });

this.apiTypes.Add(ExamplesApiType.Rooms, new List<string>
{
Expand Down Expand Up @@ -77,6 +80,11 @@ public Startup(IConfiguration configuration)
"asset_group_account_clone_write",
"asset_group_account_clone_read",
});

this.apiTypes.Add(ExamplesApiType.WebForms, new List<string>
{
"signature", "webforms_read", "webforms_instance_write", "webforms_instance_read",
});
}

public IConfiguration Configuration { get; }
Expand Down Expand Up @@ -175,28 +183,7 @@ public void ConfigureServices(IServiceCollection services)
{
List<string> scopesForCurrentApi = this.apiTypes.GetValueOrDefault(Enum.Parse<ExamplesApiType>(this.Configuration["API"]));

Check warning on line 184 in launcher-csharp/Startup.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

foreach (var api in this.apiTypes)
{
if (this.Configuration["API"] != api.Key.ToString())
{
foreach (var scope in api.Value)
{
if (scopesForCurrentApi != null && !scopesForCurrentApi.Contains(scope))
{
var scopeWithSeperator = scope + "%20";

if (redirectContext.RedirectUri.Contains(scopeWithSeperator))
{
redirectContext.RedirectUri = redirectContext.RedirectUri.Replace(scopeWithSeperator, string.Empty);
}
else
{
redirectContext.RedirectUri = redirectContext.RedirectUri.Replace(scope, string.Empty);
}
}
}
}
}
redirectContext.RedirectUri = this.UpdateRedirectUriScopes(redirectContext.RedirectUri, scopesForCurrentApi);

Check warning on line 186 in launcher-csharp/Startup.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'wantedScopes' in 'string Startup.UpdateRedirectUriScopes(string uri, List<string> wantedScopes)'.

redirectContext.HttpContext.Response.Redirect(redirectContext.RedirectUri);
return Task.FromResult(0);
Expand Down Expand Up @@ -327,5 +314,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

return keyValue;
}

private string UpdateRedirectUriScopes(string uri, List<string> wantedScopes)
{
const string pattern = @"(?:&|\?)scope=([^&]+)";

var encodedScopes = string.Join(" ", wantedScopes);
return Regex.Replace(uri, pattern, $"&scope={HttpUtility.UrlPathEncode(encodedScopes)}");
}
}
}
1 change: 1 addition & 0 deletions launcher-csharp/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
: ((ApIs) api).Name.ToLower() == ExamplesApiType.Monitor.ToString().ToLower() ? ExamplesApiType.Monitor.ToKeywordString()
: ((ApIs) api).Name.ToLower() == ExamplesApiType.Rooms.ToString().ToLower() ? ExamplesApiType.Rooms.ToKeywordString()
: ((ApIs) api).Name.ToLower() == ExamplesApiType.Connect.ToString().ToLower() ? ExamplesApiType.Connect.ToKeywordString()
: ((ApIs) api).Name.ToLower() == ExamplesApiType.WebForms.ToString().ToLower() ? ExamplesApiType.WebForms.ToKeywordString()
: ExamplesApiType.Admin.ToKeywordString();

@foreach(var exampleGroup in api.Groups)
Expand Down
127 changes: 127 additions & 0 deletions launcher-csharp/WebForms/Controllers/CreateAndEmbedForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// <copyright file="CreateAndEmbedForm.cs" company="DocuSign">
// Copyright (c) DocuSign. All rights reserved.
// </copyright>

namespace DocuSign.CodeExamples.WebForms.Controllers
{
using System;
using System.Collections.Generic;
using System.Linq;
using DocuSign.CodeExamples.Common;
using DocuSign.CodeExamples.Controllers;
using DocuSign.CodeExamples.Models;
using DocuSign.eSign.Client;
using DocuSign.eSign.Model;
using DocuSign.WebForms.Examples;
using DocuSign.WebForms.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

[Area("WebForms")]
[Route("web001")]
public class CreateAndEmbedForm : EgController
{
public const string TemplateName = "Web Form Example Template";

private IConfiguration configuration;

public CreateAndEmbedForm(
DsConfiguration config,
IConfiguration configuration,
LauncherTexts launcherTexts,
IRequestItemsService requestItemsService)
: base(config, launcherTexts, requestItemsService)
{
this.configuration = configuration;
this.CodeExampleText = this.GetExampleText(this.EgName, ExamplesApiType.WebForms);
this.ViewBag.title = this.CodeExampleText.ExampleName;
}

public override string EgName => "web001";

[SetViewBag]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CheckTemplates()
{
string basePath = this.RequestItemsService.Session.BasePath + "/restapi";
string accessToken = this.RequestItemsService.User.AccessToken;
string accountId = this.RequestItemsService.Session.AccountId;

//ds-snippet-start:WebForms1Step2
var docuSignClient = new DocuSignClient(basePath);
docuSignClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);
//ds-snippet-end:WebForms1Step2

List<EnvelopeTemplate> templates = CreateAndEmbedFormService.GetTemplatesByName(
docuSignClient,
accountId,
TemplateName);

if (templates == null || templates.Count == 0)
{
TemplateSummary template = CreateAndEmbedFormService.CreateTemplate(
docuSignClient,
accountId,
this.Config.DocumentTemplatePdf,
TemplateName);

this.RequestItemsService.WebFormsTemplateId = template.TemplateId;
}
else
{
this.RequestItemsService.WebFormsTemplateId = templates.First().TemplateId;
}

CreateAndEmbedFormService.AddTemplateIdToForm(
this.Config.WebFormConfig,
this.RequestItemsService.WebFormsTemplateId);

this.ViewBag.CodeExampleText = this.CodeExampleText;
this.ViewBag.Description = this.CodeExampleText.AdditionalPages
.First(x => x.Name == "create_web_form").ResultsPageText;

return this.View("embedForm");
}

[MustAuthenticate]
[SetViewBag]
[Route("EmbedForm")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EmbedForm()
{
string basePath = this.RequestItemsService.Session.WebFormsBasePath;
string accessToken = this.RequestItemsService.User.AccessToken;
string accountId = this.RequestItemsService.Session.AccountId;

var docuSignClient = new DocuSign.WebForms.Client.DocuSignClient(basePath);
docuSignClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);

WebFormSummaryList forms = CreateAndEmbedFormService.GetForms(
docuSignClient,
accountId);

if (forms.Items == null || forms.Items.Count == 0)
{
this.ViewBag.CodeExampleText = this.CodeExampleText;
this.ViewBag.Description = this.CodeExampleText.AdditionalPages
.First(x => x.Name == "create_web_form").ResultsPageText;

return this.View("embedForm");
}

string formId = forms.Items.First(x => x.FormProperties.Name == TemplateName).Id;
WebFormInstance form = CreateAndEmbedFormService.CreateInstance(
docuSignClient,
accountId,
formId);

this.ViewBag.InstanceToken = form.InstanceToken;
this.ViewBag.Url = form.FormUrl;
this.ViewBag.IntegrationKey = this.configuration["DocuSign:ClientId"];

return this.View("Embed");
}
}
}
Loading

0 comments on commit 8fa98b5

Please sign in to comment.