Skip to content

Instantly share code, notes, and snippets.

View Lutando's full-sized avatar
💻
developing.

Lutando Ngqakaza Lutando

💻
developing.
View GitHub Profile
using Microsoft.AspNetCore.Authorization.Infrastructure;
namespace Formum.Api.Authorization.Operations
{
public class PostOperations
{
public static OperationAuthorizationRequirement Delete = new OperationAuthorizationRequirement { Name = "PostDelete" };
public static OperationAuthorizationRequirement Edit = new OperationAuthorizationRequirement { Name = "PostEdit" };
}
}
using System;
namespace Forum.Models
{
public class Post
{
public Guid Id { get; private set; }
public Guid UserId { get; private set; }
public DateTime CreatedAt { get; private set; }
public string Text { get; private set; }
public class PostAuthorizationModel : PostAuthorizationModel<Guid>
{
public Guid OwnerId {get; private set;}
public Guid ResourceId {get; private set;}
private PostAuthorizationModel(Guid ownerId, Guid postId)
{
OwnerId = ownerId;
ResourceId = postId;
}
using System.Threading.Tasks;
using Formum.Api.Authorization.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Infrastructure;
namespace Formum.Api.Authorization.Handlers
{
public class PostAuthorizationHandler : AuthorizationHandler<OperationAuthorizationRequirement, PostAuthorizationModel>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, OperationAuthorizationRequirement requirement,
[Fact]
[Trait("Category", Category)]
public async void HandleEdit_WhenCalledWithResourceOwner_ShouldSucceed()
{
var resource = make_PostDefault();
var authorizationModel = PostAuthorizationModel.From(resource);
var user = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim> { new Claim("sub", UserIdDefault.ToString()) }));
var requirement = PostOperations.Edit;
var authorizationContext = new AuthorizationHandlerContext(new List<IAuthorizationRequirement> { requirement }, user, authorizationModel);
var authorizationHandler = new PostAuthorizationHandler();
[Fact]
[Trait("Category", Category)]
public async void HandleEdit_WhenCalledWithResourceOwner_ShouldSucceed()
{
var resource = make_PostDefault();
var authorizationModel = PostAuthorizationModel.From(resource);
var user = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim> { new Claim("sub", UserIdDefault.ToString()) }));
var requirement = PostOperations.Edit;
var authorizationContext = new AuthorizationHandlerContext(new List<IAuthorizationRequirement> { requirement }, user, authorizationModel);
var authorizationHandler = new PostAuthorizationHandler();
[Fact]
[Trait("Category", Category)]
public async void HandleDelete_WhenCalledWithNonResourceOwner_ShouldFail()
{
var resource = make_PostDefault();
var authorizationModel = PostAuthorizationModel.From(resource);
//we use an invalid user this time
var user = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim> { new Claim("sub", InvalidUserIdDefault.ToString()) }));
var requirement = PostOperations.Delete;
var authorizationContext = new AuthorizationHandlerContext(new List<IAuthorizationRequirement> { requirement }, user, authorizationModel);
using Forum.Interfaces;
using System;
using System.Threading.Tasks;
using Formum.Api.Authorization.Models;
using Formum.Api.Authorization.Operations;
using Forum.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Forum.Api.Controllers
public static class WebSockets
{
public static void Main(string[] args)
{
RunWebSockets().GetAwaiter().GetResult();
}
private static async Task RunWebSockets()
{
var ws = new ClientWebSocket(); //------v please use a fresh token to test out this is just a PoC client
public class CustomAdapter : DefaultEventAdapter
{
protected override byte[] ToBytes(object @event, JObject metadata, out string type, out bool isJson)
{
var bytes = base.ToBytes(@event, metadata, out type, out isJson);
//Add some additional metadata:
metadata["additionalProp"] = true;