Skip to content

Commit b60e6f6

Browse files
Update auth
1 parent 40757da commit b60e6f6

File tree

3 files changed

+45
-130
lines changed

3 files changed

+45
-130
lines changed

DocuSign.Workspaces/DocuSign.Workspaces/Controllers/Admin/AdminController.cs

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using DocuSign.Workspaces.Controllers.Common.Models;
2-
using DocuSign.Workspaces.Domain.Admin.Models;
1+
using DocuSign.Workspaces.Domain.Admin.Models;
32
using DocuSign.Workspaces.Domain.Admin.Services.Interfaces;
43
using Microsoft.AspNetCore.Authentication;
54
using Microsoft.AspNetCore.Authentication.Cookies;
@@ -16,40 +15,26 @@
1615

1716
namespace DocuSign.Workspaces.Controllers.Admin
1817
{
19-
public class AdminController : Controller
18+
public class AdminController(
19+
IAuthenticationService authenticationService,
20+
ISettingsRepository settingsRepository,
21+
IAccountRepository accountRepository,
22+
IDocuSignApiProvider docuSignApiProvider,
23+
ITestAccountConnectionSettingsRepository testAccountConnectionSettingsRepository)
24+
: Controller
2025
{
21-
private readonly IAuthenticationService _authenticationService;
22-
private readonly ISettingsRepository _settingsRepository;
23-
private readonly IAccountRepository _accountRepository;
24-
private readonly IDocuSignApiProvider _docuSignApiProvider;
25-
private readonly ITestAccountConnectionSettingsRepository _testAccountConnectionSettingsRepository;
26-
27-
public AdminController(
28-
IAuthenticationService authenticationService,
29-
ISettingsRepository settingsRepository,
30-
IAccountRepository accountRepository,
31-
IDocuSignApiProvider docuSignApiProvider,
32-
ITestAccountConnectionSettingsRepository testAccountConnectionSettingsRepository)
33-
{
34-
_authenticationService = authenticationService;
35-
_settingsRepository = settingsRepository;
36-
_docuSignApiProvider = docuSignApiProvider;
37-
_accountRepository = accountRepository;
38-
_testAccountConnectionSettingsRepository = testAccountConnectionSettingsRepository;
39-
}
4026

4127
[HttpPost]
4228
[Route("/api/account/consent/obtain")]
4329
public IActionResult ObtainConsent([FromBody] RequestAccountAuthorizeModel model)
4430
{
45-
var settings = _settingsRepository.Get();
31+
var settings = settingsRepository.Get();
4632
settings.BasePath = model.BasePath;
47-
_settingsRepository.Save(settings);
33+
settingsRepository.Save(settings);
4834

4935
return model.ConsentType switch
5036
{
51-
ConsentType.Admin => Ok(new ResponseAccountAuthorizeModel(_authenticationService.CreateAdminConsentUrl(model.BasePath, "api/consentcallback"))),
52-
ConsentType.Individual => Ok(new ResponseAccountAuthorizeModel(_authenticationService.CreateUserConsentUrl(model.BasePath, "api/consentcallback"))),
37+
ConsentType.Individual => Ok(new ResponseAccountAuthorizeModel(authenticationService.CreateUserConsentUrl(model.BasePath, "api/consentcallback"))),
5338
_ => BadRequest("Unknown consent type")
5439
};
5540
}
@@ -58,21 +43,21 @@ public IActionResult ObtainConsent([FromBody] RequestAccountAuthorizeModel model
5843
[Route("/api/account/consent/remove")]
5944
public IActionResult RemoveConsent()
6045
{
61-
var settings = _settingsRepository.Get();
46+
var settings = settingsRepository.Get();
6247
settings.IsConsentGranted = false;
63-
_settingsRepository.Save(settings);
48+
settingsRepository.Save(settings);
6449
return NoContent();
6550
}
6651

6752
[HttpGet]
6853
[Route("/api/consentcallback")]
6954
public IActionResult ConsentCallback(string code)
7055
{
71-
var settings = _settingsRepository.Get();
56+
var settings = settingsRepository.Get();
7257
settings.IsConsentGranted = true;
73-
settings.UserId = _authenticationService.PrePopulateUserId(settings.BasePath, code);
58+
settings.UserId = authenticationService.PrePopulateUserId(settings.BasePath, code);
7459
settings.AuthCode = code;
75-
_settingsRepository.Save(settings);
60+
settingsRepository.Save(settings);
7661
return LocalRedirect("/");
7762
}
7863

@@ -82,7 +67,7 @@ public async Task<IActionResult> GetAccounts(string basePath, string userId)
8267
{
8368
try
8469
{
85-
var result = await _authenticationService.GetAccountsAsync(
70+
var result = await authenticationService.GetAccountsAsync(
8671
basePath, userId);
8772
return Ok(result);
8873
}
@@ -99,26 +84,27 @@ public async Task<IActionResult> Connect([FromBody] RequestAccountConnectModel m
9984
var connectionSettings = CreateConnectionSettings(model);
10085
try
10186
{
102-
var principal = await _authenticationService.AuthenticateFromJwtAsync(connectionSettings);
87+
var principal = await authenticationService.AuthenticateFromJwtAsync(connectionSettings);
10388

10489
await HttpContext.SignInAsync(
10590
CookieAuthenticationDefaults.AuthenticationScheme,
10691
principal);
10792
HttpContext.User = principal;
10893

109-
var settings = _settingsRepository.Get();
94+
var settings = settingsRepository.Get();
11095
settings.AuthenticationType = model.AuthenticationType;
11196
settings.UserId = model.UserId;
11297
settings.AccountId = model.AccountId;
11398
settings.BaseUri = model.BaseUri;
11499
settings.Template = TemplateNames.DefaultTemplateId;
115100
settings.SignatureTypesDataSource = GetSignatureTypesDataSource(connectionSettings.AccountId);
116101
settings.SignatureType = SignatureInfo.DefaultProviderName;
117-
_settingsRepository.Save(settings);
102+
settingsRepository.Save(settings);
118103
}
119-
catch (ApplicationApiException ex)
104+
catch (ApplicationApiException)
120105
{
121-
return BadRequest(CreateErrorDetails(ex.Details, model));
106+
var consentUrl = authenticationService.CreateTestAccountConsentUrl(connectionSettings.BasePath, "");
107+
Redirect(consentUrl);
122108
}
123109

124110
return NoContent();
@@ -133,10 +119,10 @@ public IActionResult GetStatus()
133119
ConnectedUser = new ConnectedUserModel
134120
{
135121
Name = HttpContext.User.Identity?.Name ?? string.Empty,
136-
Email = _accountRepository.Email,
137-
AccountName = _accountRepository.AccountName
122+
Email = accountRepository.Email,
123+
AccountName = accountRepository.AccountName
138124
},
139-
IsConsentGranted = _settingsRepository.Get().IsConsentGranted,
125+
IsConsentGranted = settingsRepository.Get().IsConsentGranted,
140126
IsConnected = HttpContext.User.Identity != null && HttpContext.User.Identity.IsAuthenticated
141127
};
142128
return Ok(model);
@@ -148,12 +134,12 @@ public IActionResult GetStatus()
148134
public async Task<IActionResult> Disconnect()
149135
{
150136
await HttpContext.SignOutAsync();
151-
var settings = _settingsRepository.Get();
137+
var settings = settingsRepository.Get();
152138
settings.Template = null;
153139
settings.SignatureType = null;
154140
settings.TemplatesDataSource = null;
155141
settings.SignatureTypesDataSource = null;
156-
_settingsRepository.Save(settings);
142+
settingsRepository.Save(settings);
157143
return NoContent();
158144
}
159145

@@ -172,7 +158,7 @@ public async Task<IActionResult> Logout()
172158
[Route("/api/settings")]
173159
public IActionResult GetSetting()
174160
{
175-
var settings = _settingsRepository.Get();
161+
var settings = settingsRepository.Get();
176162
return Ok(new SettingsModel
177163
{
178164
BasePath = settings.BasePath,
@@ -189,7 +175,7 @@ public IActionResult GetSetting()
189175
[Route("/api/settings/datasource")]
190176
public IActionResult GetDatasource()
191177
{
192-
var settings = _settingsRepository.Get();
178+
var settings = settingsRepository.Get();
193179
return Ok(new DataSourceModel
194180
{
195181
SignatureTypes = settings.SignatureTypesDataSource,
@@ -201,14 +187,14 @@ public IActionResult GetDatasource()
201187
[Route("/api/settings")]
202188
public IActionResult SetSettings([FromBody] SettingsModel model)
203189
{
204-
var settings = _settingsRepository.Get();
190+
var settings = settingsRepository.Get();
205191
settings.BaseUri = settings.BaseUri;
206192
settings.UserId = model.UserId;
207193
settings.AccountId = model.AccountId;
208194
settings.Template = model.Template;
209195
settings.SignatureType = model.SignatureType;
210196
settings.UserProfile = model.UserProfile;
211-
_settingsRepository.Save(settings);
197+
settingsRepository.Save(settings);
212198

213199
return Ok(model);
214200
}
@@ -224,28 +210,16 @@ private AccountConnectionSettings CreateConnectionSettings(RequestAccountConnect
224210
AccountId = model.AccountId,
225211
UserId = model.UserId
226212
},
227-
AuthenticationType.TestAccount => _testAccountConnectionSettingsRepository.Get(),
213+
AuthenticationType.TestAccount => testAccountConnectionSettingsRepository.Get(),
228214
_ => null
229215
};
230216

231217
return connectionSettings;
232218
}
233219

234-
private ErrorDetailsModel CreateErrorDetails(ApiErrorDetails error, RequestAccountConnectModel model)
235-
{
236-
return error.Error switch
237-
{
238-
ApiErrorDetails.InvalidBasePath => ErrorDetailsModel.CreateErrorDetailsForOneModelProperty(error.Error, model, connectModel => connectModel.BasePath),
239-
ApiErrorDetails.InvalidBaseUri => ErrorDetailsModel.CreateErrorDetailsForOneModelProperty(error.Error, model, connectModel => connectModel.BaseUri),
240-
ApiErrorDetails.InvalidUserId => ErrorDetailsModel.CreateErrorDetailsForOneModelProperty(error.Error, model, connectModel => connectModel.UserId),
241-
ApiErrorDetails.InvalidAccountId => ErrorDetailsModel.CreateErrorDetailsForOneModelProperty(error.Error, model, connectModel => connectModel.AccountId),
242-
_ => ErrorDetailsModel.CreateGeneralErrorDetails(error.Error)
243-
};
244-
}
245-
246220
private IEnumerable<DataSourceItem> GetSignatureTypesDataSource(string accountId)
247221
{
248-
var signatureProviders = _docuSignApiProvider.AccountsApi.ListSignatureProviders(accountId);
222+
var signatureProviders = docuSignApiProvider.AccountsApi.ListSignatureProviders(accountId);
249223
var result = new List<DataSourceItem>
250224
{
251225
new()

DocuSign.Workspaces/DocuSign.Workspaces/Domain/Admin/Services/Interfaces/IAuthenticationService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ public interface IAuthenticationService
1010
{
1111
Task<ClaimsPrincipal> AuthenticateFromJwtAsync(AccountConnectionSettings accountConnectionSettings);
1212

13-
string CreateAdminConsentUrl(string baseUrl, string redirectUrl);
14-
1513
string CreateUserConsentUrl(string baseUrl, string redirectUrl);
1614

17-
void AuthenticateForProfileManagement(string login, string password);
15+
string CreateTestAccountConsentUrl(string baseUrl, string redirectUrl);
1816

1917
string PrePopulateUserId(string basePath, string code);
2018

0 commit comments

Comments
 (0)