Skip to content

Commit 2f6e8ce

Browse files
committed
v1
1 parent b84ab95 commit 2f6e8ce

File tree

108 files changed

+27985
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+27985
-0
lines changed

.gitattributes

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
###############################################################################
2+
# Set default behavior to automatically normalize line endings.
3+
###############################################################################
4+
* text=auto
5+
6+
###############################################################################
7+
# Set default behavior for command prompt diff.
8+
#
9+
# This is need for earlier builds of msysgit that does not have it on by
10+
# default for csharp files.
11+
# Note: This is only used by command line
12+
###############################################################################
13+
#*.cs diff=csharp
14+
15+
###############################################################################
16+
# Set the merge driver for project and solution files
17+
#
18+
# Merging from the command prompt will add diff markers to the files if there
19+
# are conflicts (Merging from VS is not affected by the settings below, in VS
20+
# the diff markers are never inserted). Diff markers may cause the following
21+
# file extensions to fail to load in VS. An alternative would be to treat
22+
# these files as binary and thus will always conflict and require user
23+
# intervention with every merge. To do so, just uncomment the entries below
24+
###############################################################################
25+
#*.sln merge=binary
26+
#*.csproj merge=binary
27+
#*.vbproj merge=binary
28+
#*.vcxproj merge=binary
29+
#*.vcproj merge=binary
30+
#*.dbproj merge=binary
31+
#*.fsproj merge=binary
32+
#*.lsproj merge=binary
33+
#*.wixproj merge=binary
34+
#*.modelproj merge=binary
35+
#*.sqlproj merge=binary
36+
#*.wwaproj merge=binary
37+
38+
###############################################################################
39+
# behavior for image files
40+
#
41+
# image files are treated as binary by default.
42+
###############################################################################
43+
#*.jpg binary
44+
#*.png binary
45+
#*.gif binary
46+
47+
###############################################################################
48+
# diff behavior for common document formats
49+
#
50+
# Convert binary document formats to text before diffing them. This feature
51+
# is only available from the command line. Turn it on by uncommenting the
52+
# entries below.
53+
###############################################################################
54+
#*.doc diff=astextplain
55+
#*.DOC diff=astextplain
56+
#*.docx diff=astextplain
57+
#*.DOCX diff=astextplain
58+
#*.dot diff=astextplain
59+
#*.DOT diff=astextplain
60+
#*.pdf diff=astextplain
61+
#*.PDF diff=astextplain
62+
#*.rtf diff=astextplain
63+
#*.RTF diff=astextplain

eg-03-csharp-auth-code-grant-core.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28010.2036
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "eg-03-csharp-auth-code-grant-core", "eg-03-csharp-auth-code-grant-core\eg-03-csharp-auth-code-grant-core.csproj", "{552068CF-E2F7-4575-A1B5-B5011C1CD039}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{552068CF-E2F7-4575-A1B5-B5011C1CD039}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{552068CF-E2F7-4575-A1B5-B5011C1CD039}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{552068CF-E2F7-4575-A1B5-B5011C1CD039}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{552068CF-E2F7-4575-A1B5-B5011C1CD039}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {D7225752-D9D5-4C8A-B834-48D81A8D8085}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using DocuSign.eSign.Client;
2+
using eg_03_csharp_auth_code_grant_core.Models;
3+
4+
namespace eg_03_csharp_auth_code_grant_core
5+
{
6+
public interface IRequestItemsService
7+
{
8+
ApiClient DefaultApiClient { get; }
9+
10+
Configuration DefaultConfiguration { get; }
11+
string EgName { get; set; }
12+
13+
Session Session { get; set; }
14+
15+
User User { get; set; }
16+
string EnvelopeId { get; set; }
17+
string DocumentId { get; set; }
18+
EnvelopeDocuments EnvelopeDocuments { get; set; }
19+
string TemplateId { get; set; }
20+
string Status { get; set; }
21+
}
22+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using DocuSign.eSign.Api;
2+
using DocuSign.eSign.Client;
3+
using eg_03_csharp_auth_code_grant_core.Models;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.AspNetCore.Mvc.Filters;
6+
using Microsoft.Extensions.Caching.Memory;
7+
using System;
8+
using System.Security.Claims;
9+
10+
namespace eg_03_csharp_auth_code_grant_core.Common
11+
{
12+
public class LocalsFilter : IActionFilter
13+
{
14+
DSConfiguration Config { get; }
15+
16+
private readonly IRequestItemsService _requestItemsService;
17+
private IMemoryCache _cache;
18+
19+
public LocalsFilter(DSConfiguration config, IRequestItemsService requestItemsService, IMemoryCache cache)
20+
{
21+
Config = config;
22+
_cache = cache;
23+
_requestItemsService = requestItemsService;
24+
}
25+
26+
public void OnActionExecuted(ActionExecutedContext context)
27+
{
28+
29+
}
30+
31+
public void OnActionExecuting(ActionExecutingContext context)
32+
{
33+
34+
Controller controller = context.Controller as Controller;
35+
36+
if (controller == null)
37+
{
38+
return;
39+
}
40+
var viewBag = controller.ViewBag;
41+
var httpContext = context.HttpContext;
42+
43+
var locals = httpContext.Session.GetObjectFromJson<Locals>("locals") ?? new Locals();
44+
locals.DsConfig = Config;
45+
locals.Session = httpContext.Session.GetObjectFromJson<Session>("session") ?? null;
46+
locals.Messages = "";
47+
locals.Json = null;
48+
locals.User = null;
49+
viewBag.Locals = locals;
50+
viewBag.showDoc = Config.documentation != null;
51+
52+
53+
var identity = httpContext.User.Identity as ClaimsIdentity;
54+
if (identity != null && !identity.IsAuthenticated)
55+
{
56+
locals.Session = new Session();
57+
return;
58+
}
59+
60+
61+
locals.User = httpContext.Session.GetObjectFromJson<User>("user");
62+
63+
if (locals.User == null)
64+
{
65+
locals.User = new User
66+
{
67+
Name = identity.FindFirst(x => x.Type.Equals(ClaimTypes.Name)).Value,
68+
AccessToken = identity.FindFirst(x => x.Type.Equals("access_token")).Value,
69+
RefreshToken = identity.FindFirst(x => x.Type.Equals("refresh_token")).Value,
70+
ExpireIn = DateTime.Parse(identity.FindFirst(x => x.Type.Equals("expires_in")).Value)
71+
};
72+
_requestItemsService.User = locals.User;
73+
}
74+
if (locals.Session == null)
75+
{
76+
locals.Session = new Session
77+
{
78+
AccountId = identity.FindFirst(x => x.Type.Equals("account_id")).Value,
79+
AccountName = identity.FindFirst(x => x.Type.Equals("account_name")).Value,
80+
BasePath = identity.FindFirst(x => x.Type.Equals("base_uri")).Value
81+
};
82+
83+
_requestItemsService.Session = locals.Session;
84+
}
85+
}
86+
}
87+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using DocuSign.eSign.Client;
2+
using eg_03_csharp_auth_code_grant_core.Models;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.Extensions.Caching.Memory;
5+
using System.Security.Claims;
6+
7+
namespace eg_03_csharp_auth_code_grant_core.Common
8+
{
9+
public class RequestItemsService : IRequestItemsService
10+
{
11+
private readonly IHttpContextAccessor _httpContextAccessor;
12+
private readonly IMemoryCache _cache;
13+
private readonly string _id;
14+
private string API_CLIENT_KEY = "{0}_ApiClient";
15+
private string CONFIG_KEY = "{0}_DocusignConfig";
16+
private string BASE_URI = "https://demo.docusign.net/restapi";
17+
private string _accessToken;
18+
19+
public RequestItemsService(IHttpContextAccessor httpContextAccessor, IMemoryCache cache)
20+
{
21+
_httpContextAccessor = httpContextAccessor;
22+
_cache = cache;
23+
Status = "sent";
24+
var identity = httpContextAccessor.HttpContext.User.Identity as ClaimsIdentity;
25+
26+
if (identity != null && identity.IsAuthenticated)
27+
{
28+
_accessToken = identity.FindFirst(x => x.Type.Equals("access_token")).Value;
29+
_id = httpContextAccessor.HttpContext.User.Identity.Name;
30+
}
31+
}
32+
33+
public ApiClient DefaultApiClient
34+
{
35+
get
36+
{
37+
var key = string.Format(API_CLIENT_KEY, _id);
38+
ApiClient apiClient = _cache.Get<ApiClient>("apiClient");
39+
if (apiClient == null)
40+
{
41+
apiClient = new ApiClient(BASE_URI);
42+
_cache.Set(key, apiClient);
43+
}
44+
45+
return apiClient;
46+
}
47+
}
48+
49+
public Configuration DefaultConfiguration
50+
{
51+
get
52+
{
53+
var key = string.Format(CONFIG_KEY, _id);
54+
var docuSignConfig = _cache.Get<Configuration>(key);
55+
56+
if (docuSignConfig == null)
57+
{
58+
docuSignConfig = new Configuration(new ApiClient(BASE_URI));
59+
_cache.Set(key, docuSignConfig);
60+
}
61+
docuSignConfig.AddDefaultHeader("Authorization", "Bearer " + _accessToken);
62+
return docuSignConfig;
63+
}
64+
}
65+
private string GetKey(string key)
66+
{
67+
return string.Format("{0}_{1}", _id, key);
68+
}
69+
public string EgName {
70+
get => _cache.Get<string>(GetKey("EgName"));
71+
set => _cache.Set(GetKey("EgName"), value);
72+
}
73+
74+
public Session Session {
75+
get => _cache.Get<Session>(GetKey("Session"));
76+
set => _cache.Set(GetKey("Session"), value);
77+
}
78+
79+
public User User {
80+
get => _cache.Get<User>(GetKey("User"));
81+
set => _cache.Set(GetKey("User"), value);
82+
}
83+
84+
public string EnvelopeId {
85+
get => _cache.Get<string>(GetKey("EnvelopeId"));
86+
set => _cache.Set(GetKey("EnvelopeId"), value);
87+
}
88+
89+
public string DocumentId {
90+
get => _cache.Get<string>(GetKey("DocumentId"));
91+
set => _cache.Set(GetKey("DocumentId"), value);
92+
}
93+
94+
public EnvelopeDocuments EnvelopeDocuments {
95+
get => _cache.Get<EnvelopeDocuments>(GetKey("EnvelopeDocuments"));
96+
set => _cache.Set(GetKey("EnvelopeDocuments"), value);
97+
}
98+
99+
public string TemplateId {
100+
get => _cache.Get<string>(GetKey("TemplateId"));
101+
set => _cache.Set(GetKey("TemplateId"), value);
102+
}
103+
104+
public string Status { get; set; }
105+
}
106+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace eg_03_csharp_auth_code_grant_core.Common
9+
{
10+
public static class SessionExtensions
11+
{
12+
public static void SetObjectAsJson(this ISession session, string key, object value)
13+
{
14+
session.SetString(key, JsonConvert.SerializeObject(value));
15+
}
16+
17+
public static T GetObjectFromJson<T>(this ISession session, string key)
18+
{
19+
var value = session.GetString(key);
20+
21+
return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
22+
}
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.Authentication.Cookies;
3+
using Microsoft.AspNetCore.Authentication.OAuth;
4+
using Microsoft.AspNetCore.Identity;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace eg_03_csharp_auth_code_grant_core.Controllers
8+
{
9+
[Route("ds/[action]")]
10+
public class AccountController : Controller
11+
{
12+
[HttpGet]
13+
public IActionResult Login(string returnUrl = "/")
14+
{
15+
return Challenge(new AuthenticationProperties() { RedirectUri = returnUrl });
16+
}
17+
18+
public IActionResult MustAuthenticate()
19+
{
20+
return View();
21+
}
22+
23+
public async System.Threading.Tasks.Task<IActionResult> logout()
24+
{
25+
await AuthenticationHttpContextExtensions.SignOutAsync(HttpContext);
26+
return LocalRedirect("/");
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)