Skip to content

Commit 3007ec7

Browse files
Access token implement delete
1 parent 6187235 commit 3007ec7

File tree

8 files changed

+132
-4
lines changed

8 files changed

+132
-4
lines changed

src/Certify.Core/Management/Access/AccessControl.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,17 @@ public async Task<bool> AddAssignedAccessToken(string contextUserId, AssignedAcc
610610
return true;
611611
}
612612

613+
public async Task<bool> DeleteAssignedAccessToken(string contextUserId, string id)
614+
{
615+
if (!await IsPrincipleInRole(contextUserId, contextUserId, StandardRoles.Administrator.Id))
616+
{
617+
await AuditWarning("User {contextUserId} attempted to delete an assigned access token without being in required role.", contextUserId);
618+
return false;
619+
}
620+
621+
return await _store.Delete<AssignedAccessToken>(nameof(AssignedAccessToken), id);
622+
}
623+
613624
public string GetSHA256Hash(string val)
614625
{
615626
using (var sha256Hash = SHA256.Create())

src/Certify.Core/Management/Access/IAccessControl.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public interface IAccessControl
3333

3434
Task<List<AssignedAccessToken>> GetAssignedAccessTokens(string contextUserId);
3535
Task<bool> AddAssignedAccessToken(string contextUserId, AssignedAccessToken token);
36+
37+
Task<bool> DeleteAssignedAccessToken(string contextUserId, string id);
3638
Task<bool> IsInitialized();
3739
}
3840
}

src/Certify.Server/Certify.Server.Core/Certify.Server.Core/Controllers/AccessController.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ public async Task<ICollection<AssignedAccessToken>> GetAssignedAccessTokens()
121121
public async Task<Models.Config.ActionResult> AddAssignedccessToken([FromBody] AssignedAccessToken token)
122122
{
123123
var accessControl = await _certifyManager.GetCurrentAccessControl();
124+
125+
token.AccessTokens?.ForEach(a => a.DateCreated = DateTime.UtcNow);
126+
124127
var addResultOk = await accessControl.AddAssignedAccessToken(GetContextUserId(), token);
125128

126129
return new Models.Config.ActionResult
@@ -130,6 +133,19 @@ public async Task<ICollection<AssignedAccessToken>> GetAssignedAccessTokens()
130133
};
131134
}
132135

136+
[HttpDelete, Route("assignedtoken/{id}")]
137+
public async Task<Models.Config.ActionResult> RemoveAssignedAccessToken(string id)
138+
{
139+
var accessControl = await _certifyManager.GetCurrentAccessControl();
140+
var addResultOk = await accessControl.DeleteAssignedAccessToken(GetContextUserId(), id);
141+
142+
return new Models.Config.ActionResult
143+
{
144+
IsSuccess = addResultOk,
145+
Message = addResultOk ? "Added" : "Failed to add"
146+
};
147+
}
148+
133149
[HttpGet, Route("securityprinciple/{id}/assignedroles")]
134150
public async Task<ICollection<AssignedRole>> GetSecurityPrincipleAssignedRoles(string id)
135151
{

src/Certify.Server/Certify.Server.Hub.Api.Client/Certify.Server.Hub.Api.Client.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,96 @@ public virtual async System.Threading.Tasks.Task<ActionResult> AddAssignedAccess
600600
}
601601
}
602602

603+
/// <summary>
604+
/// Remove assigned access token [Generated]
605+
/// </summary>
606+
/// <returns>OK</returns>
607+
/// <exception cref="ApiException">A server side error occurred.</exception>
608+
public virtual System.Threading.Tasks.Task<ActionResult> RemoveAssignedAccessTokenAsync(string id)
609+
{
610+
return RemoveAssignedAccessTokenAsync(id, System.Threading.CancellationToken.None);
611+
}
612+
613+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
614+
/// <summary>
615+
/// Remove assigned access token [Generated]
616+
/// </summary>
617+
/// <returns>OK</returns>
618+
/// <exception cref="ApiException">A server side error occurred.</exception>
619+
public virtual async System.Threading.Tasks.Task<ActionResult> RemoveAssignedAccessTokenAsync(string id, System.Threading.CancellationToken cancellationToken)
620+
{
621+
var client_ = _httpClient;
622+
var disposeClient_ = false;
623+
try
624+
{
625+
using (var request_ = new System.Net.Http.HttpRequestMessage())
626+
{
627+
request_.Method = new System.Net.Http.HttpMethod("DELETE");
628+
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("text/plain"));
629+
630+
var urlBuilder_ = new System.Text.StringBuilder();
631+
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
632+
// Operation Path: "internal/v1/access/assignedtoken"
633+
urlBuilder_.Append("internal/v1/access/assignedtoken");
634+
urlBuilder_.Append('?');
635+
if (id != null)
636+
{
637+
urlBuilder_.Append(System.Uri.EscapeDataString("id")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture))).Append('&');
638+
}
639+
urlBuilder_.Length--;
640+
641+
PrepareRequest(client_, request_, urlBuilder_);
642+
643+
var url_ = urlBuilder_.ToString();
644+
request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
645+
646+
PrepareRequest(client_, request_, url_);
647+
648+
var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
649+
var disposeResponse_ = true;
650+
try
651+
{
652+
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
653+
foreach (var item_ in response_.Headers)
654+
headers_[item_.Key] = item_.Value;
655+
if (response_.Content != null && response_.Content.Headers != null)
656+
{
657+
foreach (var item_ in response_.Content.Headers)
658+
headers_[item_.Key] = item_.Value;
659+
}
660+
661+
ProcessResponse(client_, response_);
662+
663+
var status_ = (int)response_.StatusCode;
664+
if (status_ == 200)
665+
{
666+
var objectResponse_ = await ReadObjectResponseAsync<ActionResult>(response_, headers_, cancellationToken).ConfigureAwait(false);
667+
if (objectResponse_.Object == null)
668+
{
669+
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
670+
}
671+
return objectResponse_.Object;
672+
}
673+
else
674+
{
675+
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
676+
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
677+
}
678+
}
679+
finally
680+
{
681+
if (disposeResponse_)
682+
response_.Dispose();
683+
}
684+
}
685+
}
686+
finally
687+
{
688+
if (disposeClient_)
689+
client_.Dispose();
690+
}
691+
}
692+
603693
/// <summary>
604694
/// Get list of available security principles [Generated]
605695
/// </summary>

src/Certify.Server/Certify.Server.Hub.Api.Client/nswag.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@
114114
"newLineBehavior": "Auto"
115115
}
116116
}
117-
}
117+
}

src/Certify.Server/Certify.Server.HubService/Services/CertifyHubService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ private ServiceControllers.ManagedChallengeController _managedChallengeControlle
4949
public Task<RoleStatus> GetSecurityPrincipleRoleStatus(string id, AuthContext authContext) => _accessController(authContext).GetSecurityPrincipleRoleStatus(id);
5050
public Task<ICollection<SecurityPrinciple>> GetSecurityPrinciples(AuthContext authContext) => _accessController(authContext).GetSecurityPrinciples();
5151
public Task<ActionResult> AddAssignedAccessToken(AssignedAccessToken token, AuthContext authContext) => _accessController(authContext).AddAssignedccessToken(token);
52+
public Task<ActionResult> RemoveAssignedAccessToken(string id, AuthContext authContext) => _accessController(authContext).RemoveAssignedAccessToken(id);
5253
public Task<ActionResult> CheckApiTokenHasAccess(AccessToken token, AccessCheck check, AuthContext authContext = null) => _accessController(authContext).CheckApiTokenHasAccess(new AccessTokenCheck { Check = check, Token = token });
5354
public Task<ICollection<AssignedAccessToken>> GetAssignedAccessTokens(AuthContext authContext) => _accessController(authContext).GetAssignedAccessTokens();
5455
public Task<ActionResult> RemoveSecurityPrinciple(string id, AuthContext authContext) => _accessController(authContext).DeleteSecurityPrinciple(id);
@@ -90,8 +91,6 @@ private ServiceControllers.ManagedChallengeController _managedChallengeControlle
9091
public Task<List<ManagedCertificate>> GetManagedCertificates(ManagedCertificateFilter filter, AuthContext authContext = null) => throw new NotImplementedException();
9192
public Task<ManagedCertificateSearchResult> GetManagedCertificateSearchResult(ManagedCertificateFilter filter, AuthContext authContext = null) => throw new NotImplementedException();
9293
public Task<StatusSummary> GetManagedCertificateSummary(ManagedCertificateFilter filter, AuthContext authContext = null) => throw new NotImplementedException();
93-
94-
9594
public Task<List<DomainOption>> GetServerSiteDomains(StandardServerTypes serverType, string serverSiteId, AuthContext authContext = null) => throw new NotImplementedException();
9695
public Task<List<SiteInfo>> GetServerSiteList(StandardServerTypes serverType, string itemId = null, AuthContext authContext = null) => throw new NotImplementedException();
9796
public Task<Version> GetServerVersion(StandardServerTypes serverType, AuthContext authContext = null) => throw new NotImplementedException();

src/Certify.SourceGenerators/ApiMethods.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public static List<GeneratedAPI> GetApiDefinitions()
9090
ServiceAPIRoute = "access/assignedtoken",
9191
ReturnType = "Models.Config.ActionResult",
9292
Params = new Dictionary<string, string>{{"token", "Certify.Models.Hub.AssignedAccessToken" } }
93+
},
94+
new() {
95+
OperationName = "RemoveAssignedAccessToken",
96+
OperationMethod = HttpDelete,
97+
Comment = "Remove assigned access token",
98+
PublicAPIController = "Access",
99+
PublicAPIRoute = "assignedtoken",
100+
ServiceAPIRoute = "access/assignedtoken/{id}",
101+
ReturnType = "Models.Config.ActionResult",
102+
Params = new Dictionary<string, string>{{"id", "string" } }
93103
},
94104
new() {
95105

src/Certify.Tests/Certify.Core.Tests.Unit/Tests/AccessControlTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Linq;

0 commit comments

Comments
 (0)