aboutsummaryrefslogtreecommitdiff
path: root/Plugins/OAuth2ClientApplications/src/Endpoints
diff options
context:
space:
mode:
Diffstat (limited to 'Plugins/OAuth2ClientApplications/src/Endpoints')
-rw-r--r--Plugins/OAuth2ClientApplications/src/Endpoints/ApplicationEndpoint.cs62
1 files changed, 21 insertions, 41 deletions
diff --git a/Plugins/OAuth2ClientApplications/src/Endpoints/ApplicationEndpoint.cs b/Plugins/OAuth2ClientApplications/src/Endpoints/ApplicationEndpoint.cs
index 4f9d057..99a2a50 100644
--- a/Plugins/OAuth2ClientApplications/src/Endpoints/ApplicationEndpoint.cs
+++ b/Plugins/OAuth2ClientApplications/src/Endpoints/ApplicationEndpoint.cs
@@ -53,7 +53,7 @@ namespace OAuth2ClientApplications.Endpoints
private readonly ApplicationStore Applications;
private readonly int MaxAppsPerUser;
- private readonly string MaxAppOverloadMessage;
+ private readonly string MaxAppOverloadMessage;
private static readonly UserAppValidator Validator = new();
@@ -79,16 +79,12 @@ namespace OAuth2ClientApplications.Endpoints
//Get a single specific application from an appid
if (ev.QueryArgs.TryGetNonEmptyValue("Id", out string? appid))
{
- appid = ValidatorExtensions.OnlyAlphaRegx.Replace(appid, "");
+ appid = ValidatorExtensions.OnlyAlphaRegx.Replace(appid, string.Empty);
+
//Execute get single app
UserApplication? singeApp = await Applications.GetSingleAsync(appid, ev.Session.UserID);
- if (singeApp == null)
- {
- ev.CloseResponse(HttpStatusCode.NotFound);
- return VfReturnType.VirtualSkip;
- }
- ev.CloseResponseJson(HttpStatusCode.OK, singeApp);
- return VfReturnType.VirtualSkip;
+
+ return singeApp == null ? VfReturnType.NotFound : VirtualOkJson(ev, singeApp);
}
//Process a "get all"
else
@@ -100,8 +96,7 @@ namespace OAuth2ClientApplications.Endpoints
//Get all applications to fill the list
_ = await Applications.GetCollectionAsync(applications, ev.Session.UserID, MaxAppsPerUser, ev.EventCancellation);
//Write response (will convert json as needed before releasing the list)
- ev.CloseResponseJson(HttpStatusCode.OK, applications);
- return VfReturnType.VirtualSkip;
+ return VirtualOkJson(ev, applications);
}
finally
{
@@ -119,8 +114,7 @@ namespace OAuth2ClientApplications.Endpoints
if (!entity.Session.HasLocalAccount())
{
webm.Result = "OAuth is only available for internal user accounts";
- entity.CloseResponseJson(HttpStatusCode.Forbidden, webm);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, webm, HttpStatusCode.Forbidden);
}
if (entity.QueryArgs.IsArgumentSet("action", "create"))
{
@@ -134,8 +128,7 @@ namespace OAuth2ClientApplications.Endpoints
if(webm.Assert(update != null, "Invalid request"))
{
- entity.CloseResponseJson(HttpStatusCode.BadRequest, webm);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, webm, HttpStatusCode.BadRequest);
}
//Update message will include a challenge and an app id
@@ -151,8 +144,7 @@ namespace OAuth2ClientApplications.Endpoints
if (webm.Assert(secret != null, "Failed to update the application secret"))
{
- entity.CloseResponseJson(HttpStatusCode.InternalServerError, webm);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, webm, HttpStatusCode.InternalServerError);
}
/*
@@ -169,8 +161,7 @@ namespace OAuth2ClientApplications.Endpoints
};
//Must write response while password is in scope
- entity.CloseResponseJson(HttpStatusCode.OK, result);
- return VfReturnType.VirtualSkip;
+ return VirtualOkJson(entity, result);
}
else if (entity.QueryArgs.IsArgumentSet("action", "delete"))
{
@@ -178,8 +169,7 @@ namespace OAuth2ClientApplications.Endpoints
if(webm.Assert(update != null, "Invalid request"))
{
- entity.CloseResponseJson(HttpStatusCode.BadRequest, webm);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, webm, HttpStatusCode.BadRequest);
}
//Update message will include a challenge and an app id
@@ -193,15 +183,13 @@ namespace OAuth2ClientApplications.Endpoints
//Try to delete the app
if (await Applications.DeleteAsync(appId, entity.Session.UserID))
{
- entity.CloseResponse(HttpStatusCode.NoContent);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, HttpStatusCode.NoContent);
}
}
else
{
webm.Result = "The update type specified is not defined";
- entity.CloseResponseJson(HttpStatusCode.UnprocessableEntity, webm);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, webm, HttpStatusCode.UnprocessableEntity);
}
return VfReturnType.BadRequest;
}
@@ -214,8 +202,7 @@ namespace OAuth2ClientApplications.Endpoints
if (!entity.Session.HasLocalAccount())
{
webm.Result = "OAuth is only available for internal user accounts";
- entity.CloseResponseJson(HttpStatusCode.Forbidden, webm);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, webm, HttpStatusCode.Forbidden);
}
//Get the application from client
@@ -223,8 +210,7 @@ namespace OAuth2ClientApplications.Endpoints
if (webm.Assert(app != null, "Application is empty"))
{
- entity.CloseResponseJson(HttpStatusCode.BadRequest, app);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, webm, HttpStatusCode.BadRequest);
}
//set user-id
@@ -235,16 +221,14 @@ namespace OAuth2ClientApplications.Endpoints
//perform validation on the application update (should remove unused fields)
if (!Validator.Validate(app, webm))
{
- entity.CloseResponseJson(HttpStatusCode.UnprocessableEntity, webm);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, webm, HttpStatusCode.UnprocessableEntity);
}
//Update the app's meta
if (await Applications.UpdateAsync(app))
{
//Send the app to the client
- entity.CloseResponse(HttpStatusCode.NoContent);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, HttpStatusCode.NoContent);
}
//The app was not found and could not be updated
@@ -260,15 +244,13 @@ namespace OAuth2ClientApplications.Endpoints
if (webm.Assert(newApp != null, "Application is empty"))
{
- entity.CloseResponseJson(HttpStatusCode.BadRequest, webm);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, webm, HttpStatusCode.BadRequest);
}
//Validate the new application
if (!Validator.Validate(newApp, webm))
{
- entity.CloseResponseJson(HttpStatusCode.UnprocessableEntity, webm);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, webm, HttpStatusCode.UnprocessableEntity);
}
//If no premissions are specified, set to "none"
@@ -284,13 +266,11 @@ namespace OAuth2ClientApplications.Endpoints
{
webm.Result = $"There was a server error during creation of your application";
Log.Error("There was an error retreiving the number of applications for user {id}", entity.Session.UserID);
- entity.CloseResponseJson(HttpStatusCode.InternalServerError, webm);
- return VfReturnType.VirtualSkip;
+ return VirtualClose(entity, webm, HttpStatusCode.InternalServerError);
}
if (webm.Assert(appCount < MaxAppsPerUser, MaxAppOverloadMessage))
{
- entity.CloseResponse(webm);
- return VfReturnType.VirtualSkip;
+ return VirtualOk(entity, webm);
}
//Parse permission string an re-build it to clean it up