Skip to content

Commit

Permalink
Update assigned access token API
Browse files Browse the repository at this point in the history
  • Loading branch information
webprofusion-chrisc committed Jan 23, 2025
1 parent 538c81c commit f875a66
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 51 deletions.
24 changes: 6 additions & 18 deletions src/Certify.Core/Management/Access/AccessControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,18 +477,6 @@ public async Task<bool> AddAssignedRole(string contextUserId, AssignedRole r)
return true;
}

public async Task<bool> AddAssignedAccessToken(string contextUserId, AssignedAccessToken t)
{
if (!await IsPrincipleInRole(contextUserId, contextUserId, StandardRoles.Administrator.Id))
{
await AuditWarning("User {contextUserId} attempted to add an assigned access token without being in required role.", contextUserId);
return false;
}

await _store.Add(nameof(AssignedAccessToken), t);
return true;
}

public async Task<bool> AddResourceAction(string contextUserId, ResourceAction action)
{
if (!await IsPrincipleInRole(contextUserId, contextUserId, StandardRoles.Administrator.Id))
Expand Down Expand Up @@ -597,26 +585,26 @@ await GetSecurityPrincipleByUsername(contextUserId, passwordCheck.Username) :
}
}

public async Task<List<AccessToken>> GetAccessTokens(string contextUserId)
public async Task<List<AssignedAccessToken>> GetAssignedAccessTokens(string contextUserId)
{
if (!await IsPrincipleInRole(contextUserId, contextUserId, StandardRoles.Administrator.Id))
{
await AuditWarning("User {contextUserId} attempted to list access tokens without being in required role.", contextUserId);
await AuditWarning("User {contextUserId} attempted to list assigned access tokens without being in required role.", contextUserId);
return [];
}

return await _store.GetItems<AccessToken>(nameof(AccessToken));
return await _store.GetItems<AssignedAccessToken>(nameof(AssignedAccessToken));
}

public async Task<bool> AddAccessToken(string contextUserId, AccessToken a)
public async Task<bool> AddAssignedAccessToken(string contextUserId, AssignedAccessToken a)
{
if (!await IsPrincipleInRole(contextUserId, contextUserId, StandardRoles.Administrator.Id))
{
await AuditWarning("User {contextUserId} attempted to add an access token without being in required role.", contextUserId);
await AuditWarning("User {contextUserId} attempted to add an assigned access token without being in required role.", contextUserId);
return false;
}

await _store.Add(nameof(AccessToken), a);
await _store.Add(nameof(AssignedAccessToken), a);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Certify.Core/Management/Access/IAccessControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public interface IAccessControl
Task<bool> AddAssignedRole(string contextUserId, AssignedRole assignedRole);
Task<bool> AddResourceAction(string contextUserId, ResourceAction action);

Task<List<AccessToken>> GetAccessTokens(string contextUserId);
Task<bool> AddAccessToken(string contextUserId, AccessToken token);
Task<List<AssignedAccessToken>> GetAssignedAccessTokens(string contextUserId);
Task<bool> AddAssignedAccessToken(string contextUserId, AssignedAccessToken token);
Task<bool> IsInitialized();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -429,22 +429,22 @@ public virtual async System.Threading.Tasks.Task<RoleStatus> GetSecurityPrincipl
}

/// <summary>
/// Get list of API access tokens [Generated]
/// Get list of API assigned access tokens [Generated]
/// </summary>
/// <returns>OK</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual System.Threading.Tasks.Task<System.Collections.Generic.ICollection<AccessToken>> GetAccessTokensAsync()
public virtual System.Threading.Tasks.Task<System.Collections.Generic.ICollection<AssignedAccessToken>> GetAssignedAccessTokensAsync()
{
return GetAccessTokensAsync(System.Threading.CancellationToken.None);
return GetAssignedAccessTokensAsync(System.Threading.CancellationToken.None);
}

/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <summary>
/// Get list of API access tokens [Generated]
/// Get list of API assigned access tokens [Generated]
/// </summary>
/// <returns>OK</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task<System.Collections.Generic.ICollection<AccessToken>> GetAccessTokensAsync(System.Threading.CancellationToken cancellationToken)
public virtual async System.Threading.Tasks.Task<System.Collections.Generic.ICollection<AssignedAccessToken>> GetAssignedAccessTokensAsync(System.Threading.CancellationToken cancellationToken)
{
var client_ = _httpClient;
var disposeClient_ = false;
Expand All @@ -457,8 +457,8 @@ public virtual async System.Threading.Tasks.Task<RoleStatus> GetSecurityPrincipl

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "internal/v1/access/token"
urlBuilder_.Append("internal/v1/access/token");
// Operation Path: "internal/v1/access/assignedtoken"
urlBuilder_.Append("internal/v1/access/assignedtoken");

PrepareRequest(client_, request_, urlBuilder_);

Expand All @@ -485,7 +485,7 @@ public virtual async System.Threading.Tasks.Task<RoleStatus> GetSecurityPrincipl
var status_ = (int)response_.StatusCode;
if (status_ == 200)
{
var objectResponse_ = await ReadObjectResponseAsync<System.Collections.Generic.ICollection<AccessToken>>(response_, headers_, cancellationToken).ConfigureAwait(false);
var objectResponse_ = await ReadObjectResponseAsync<System.Collections.Generic.ICollection<AssignedAccessToken>>(response_, headers_, cancellationToken).ConfigureAwait(false);
if (objectResponse_.Object == null)
{
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
Expand Down Expand Up @@ -513,22 +513,22 @@ public virtual async System.Threading.Tasks.Task<RoleStatus> GetSecurityPrincipl
}

/// <summary>
/// Add new access token [Generated]
/// Add new assigned access token [Generated]
/// </summary>
/// <returns>OK</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual System.Threading.Tasks.Task<ActionResult> AddAccessTokenAsync(AccessToken body)
public virtual System.Threading.Tasks.Task<ActionResult> AddAssignedAccessTokenAsync(AssignedAccessToken body)
{
return AddAccessTokenAsync(body, System.Threading.CancellationToken.None);
return AddAssignedAccessTokenAsync(body, System.Threading.CancellationToken.None);
}

/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <summary>
/// Add new access token [Generated]
/// Add new assigned access token [Generated]
/// </summary>
/// <returns>OK</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task<ActionResult> AddAccessTokenAsync(AccessToken body, System.Threading.CancellationToken cancellationToken)
public virtual async System.Threading.Tasks.Task<ActionResult> AddAssignedAccessTokenAsync(AssignedAccessToken body, System.Threading.CancellationToken cancellationToken)
{
var client_ = _httpClient;
var disposeClient_ = false;
Expand All @@ -545,8 +545,8 @@ public virtual async System.Threading.Tasks.Task<ActionResult> AddAccessTokenAsy

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "internal/v1/access/token"
urlBuilder_.Append("internal/v1/access/token");
// Operation Path: "internal/v1/access/assignedtoken"
urlBuilder_.Append("internal/v1/access/assignedtoken");

PrepareRequest(client_, request_, urlBuilder_);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@ public async Task<bool> CheckSecurityPrincipleHasAccess(AccessCheck check)
return await accessControl.IsAccessTokenAuthorised(GetContextUserId(), tokenCheck.Token, tokenCheck.Check);
}

[HttpGet, Route("apitoken/list/")]
public async Task<List<AccessToken>> GetAccessTokens()
[HttpGet, Route("assignedtoken/list/")]
public async Task<List<AssignedAccessToken>> GetAssignedAccessTokens()
{
var accessControl = await _certifyManager.GetCurrentAccessControl();

return await accessControl.GetAccessTokens(GetContextUserId());
return await accessControl.GetAssignedAccessTokens(GetContextUserId());
}

[HttpPost, Route("apitoken/")]
public async Task<Models.Config.ActionResult> AddAccessToken([FromBody] AccessToken token)
[HttpPost, Route("assignedtoken/")]
public async Task<Models.Config.ActionResult> AddAAssignedccessToken([FromBody] AssignedAccessToken token)
{
var accessControl = await _certifyManager.GetCurrentAccessControl();
var addResultOk = await accessControl.AddAccessToken(GetContextUserId(), token);
var addResultOk = await accessControl.AddAssignedAccessToken(GetContextUserId(), token);

return new Models.Config.ActionResult
{
Expand Down
20 changes: 10 additions & 10 deletions src/Certify.SourceGenerators/ApiMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ public static List<GeneratedAPI> GetApiDefinitions()
ReturnType = "ICollection<Role>"
},
new() {
OperationName = "GetAccessTokens",
OperationName = "GetAssignedAccessTokens",
OperationMethod = HttpGet,
Comment = "Get list of API access tokens",
Comment = "Get list of API assigned access tokens",
PublicAPIController = "Access",
PublicAPIRoute = "token",
ServiceAPIRoute = "access/apitoken/list",
ReturnType = "ICollection<Certify.Models.Hub.AccessToken>"
PublicAPIRoute = "assignedtoken",
ServiceAPIRoute = "access/assignedtoken/list",
ReturnType = "ICollection<Certify.Models.Hub.AssignedAccessToken>"
},
new() {
OperationName = "AddAccessToken",
OperationName = "AddAssignedAccessToken",
OperationMethod = HttpPost,
Comment = "Add new access token",
Comment = "Add new assigned access token",
PublicAPIController = "Access",
PublicAPIRoute = "token",
ServiceAPIRoute = "access/apitoken",
PublicAPIRoute = "assignedtoken",
ServiceAPIRoute = "access/assignedtoken",
ReturnType = "Models.Config.ActionResult",
Params = new Dictionary<string, string>{{"token", "Certify.Models.Hub.AccessToken" } }
Params = new Dictionary<string, string>{{"token", "Certify.Models.Hub.AssignedAccessToken" } }
},
new() {

Expand Down

0 comments on commit f875a66

Please sign in to comment.