Skip to content

Instantly share code, notes, and snippets.

View nmchenry01's full-sized avatar

Nicholas McHenry nmchenry01

  • Boulder, CO
View GitHub Profile
@nmchenry01
nmchenry01 / ProductRepository.cs
Last active November 28, 2021 16:23
An example of using LINQ in a C# repository to filter data by tenant ID
public class ProductRepository : IProductRepository
{
private readonly DatabaseContext _context;
public ProductRepository(DatabaseContext context)
{
_context = context;
}
public async Task<ProductModel> Get(int tenantId)
@nmchenry01
nmchenry01 / AdminProductController.cs
Last active November 28, 2021 15:43
An example of using the AuthorizeAttribute with admin authorization on a .NET Controller
[ApiController]
public class AdminProductController : ControllerBase
{
[HttpPost("product/{Id}")]
[Authorize("admin")]
public ActionResult Post(AddProductRequestDto addProductRequest)
{
// Add some product
}
}
public class HasScopeHandler : AuthorizationHandler<HasScopeRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasScopeRequirement requirement)
{
// If user does not have the scope claim, get out of here
if (!context.User.HasClaim(c => c.Type == "scope" && c.Issuer == requirement.Issuer))
return Task.CompletedTask;
// Split the scopes string into an array
var scopes = context.User.FindFirst(c => c.Type == "scope" && c.Issuer == requirement.Issuer).Value.Split(' ');
public class HasScopeRequirement : IAuthorizationRequirement
{
public string Issuer { get; }
public string Scope { get; }
public HasScopeRequirement(string scope, string issuer)
{
Scope = scope ?? throw new ArgumentNullException(nameof(scope));
Issuer = issuer ?? throw new ArgumentNullException(nameof(issuer));
}
@nmchenry01
nmchenry01 / AuthorizationStartup.cs
Last active November 28, 2021 15:35
An example of registering an Authorization middleware in .NET Core
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
@nmchenry01
nmchenry01 / Auth0CustomAction.js
Created November 28, 2021 14:38
An example of an Auth0 custom action that appends a tenantId to a user on login
/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
const redirectURI = event.request.query.redirect_uri;
const appMetadata = event.user.app_metadata;
@nmchenry01
nmchenry01 / ProductController.cs
Last active November 26, 2021 19:55
An example of using the AuthorizeAttribute on a .NET Controller
[ApiController]
public class ProductController : ControllerBase
{
[HttpGet("product/{Id}")]
[Authorize]
public ActionResult Get(int Id)
{
// Get some product by ID
}
}
@nmchenry01
nmchenry01 / AuthenticationStartup.cs
Last active November 26, 2021 19:55
An example of registering an Authentication middleware in .NET Core
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
@nmchenry01
nmchenry01 / multi-tenant-db-schema.sql
Last active November 26, 2021 23:46
A example of a PostgreSQL schema for multi-tenancy
CREATE TABLE tenant (
id INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name CHARACTER VARYING NOT NULL UNIQUE,
created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
last_modified_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW()
);
CREATE TABLE product (
id INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name CHARACTER VARYING NOT NULL,
@nmchenry01
nmchenry01 / sqlalchemy_2.py
Created November 23, 2021 20:15
An asynchronous SQLAlchemy example
from sqlalchemy import Column, Integer, String, select
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
import asyncio
# Create database schema
Base = declarative_base()
class Task(Base):
__tablename__ = "task"