Skip to content

GrydAuth Module

GrydAuth is a complete authentication and authorization module for .NET applications. It provides JWT/OAuth2 authentication, role-based access control (RBAC), permission-based authorization, and multi-tenancy support out of the box.

✨ Features

FeatureDescription
🔐 JWT AuthenticationComplete JWT token lifecycle with access/refresh tokens
👥 User ManagementUser registration, authentication, password management
🎭 Role-Based AccessFlexible role system with hierarchical permissions
🔑 Permission SystemGranular permission-based authorization with policies
🏢 Multi-TenancyBuilt-in support for tenant isolation and federation
🌐 OAuth2/OIDCIntegration with Auth0, Azure AD, and custom providers
📍 Security FeaturesRate limiting, audit logging, geo-location tracking
🔄 Token InvalidationReal-time token blacklisting and version control

📦 Packages

GrydAuth is organized in multiple packages following Clean Architecture:

📦 GrydAuth
├── GrydAuth.Domain           # Entities, Value Objects, Domain Events
├── GrydAuth.Application      # Commands, Queries, Interfaces
├── GrydAuth.Infrastructure   # JWT, Repositories, External Services
├── GrydAuth.API              # Controllers, Middlewares, Filters
└── GrydAuth.Infrastructure.Auth0  # Auth0 integration (optional)

🚀 Quick Start

Installation

bash
# Install all GrydAuth packages
dotnet add package GrydAuth.API
dotnet add package GrydAuth.Infrastructure

# Optional: Auth0 integration
dotnet add package GrydAuth.Infrastructure.Auth0
bash
Install-Package GrydAuth.API
Install-Package GrydAuth.Infrastructure

Basic Configuration

csharp
// Program.cs
using GrydAuth.API.Controllers;
using GrydAuth.Infrastructure;
using Gryd.API.Extensions;

var builder = WebApplication.CreateBuilder(args);

// Add GrydAuth services (reads from appsettings.json)
builder.Services.AddGrydAuth(builder.Configuration);

// Add Controllers (includes GrydAuth API controllers)
builder.Services.AddControllers()
    .AddApplicationPart(typeof(AuthController).Assembly);

// Add Exception Handlers
builder.Services.AddGrydAuthExceptionHandler();
builder.Services.AddCoreExceptionHandler();

var app = builder.Build();

// Exception handling first
app.UseExceptionHandler();

// Authentication middleware
app.UseAuthentication();

// GrydAuth middleware (token blacklist, SmartFederation)
// CRITICAL: AFTER Authentication, BEFORE Authorization
app.UseGrydAuth();

// Authorization middleware
app.UseAuthorization();

app.MapControllers();
app.Run();

Configuration File

json
// appsettings.json
{
  "JwtSettings": {
    "SecretKey": "your-256-bit-secret-key-here-min-32-chars",
    "Issuer": "gryd-app",
    "Audience": "gryd-app-users",
    "ExpirationMinutes": 60,
    "RefreshTokenExpirationDays": 7
  },
  "GrydAuth": {
    "Cache": {
      "IsEnabled": true,
      "Redis": { "ConnectionString": "localhost:6379" }
    },
    "PasswordPolicy": {
      "MinLength": 8,
      "RequireDigit": true,
      "RequireLowercase": true,
      "RequireUppercase": true,
      "RequireSpecialChar": true
    },
    "Security": {
      "MaxFailedAccessAttempts": 5,
      "LockoutDurationMinutes": 15,
      "EnableSecurityLogging": true
    }
  },
  "MultiTenancy": {
    "IsEnabled": true
  }
}

🏗️ Architecture Overview

┌─────────────────────────────────────────────────────────────────┐
│                       GrydAuth.API                              │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────────┐ │
│  │ Controllers │  │ Middlewares │  │ Exception Handlers      │ │
│  └─────────────┘  └─────────────┘  └─────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────┐
│                    GrydAuth.Application                         │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────────┐ │
│  │  Commands   │  │   Queries   │  │     Interfaces          │ │
│  │  - Login    │  │ - GetUser   │  │ - IAuthService          │ │
│  │  - Register │  │ - GetRoles  │  │ - ITokenService         │ │
│  │  - Refresh  │  │ - GetPerms  │  │ - IPermissionService    │ │
│  └─────────────┘  └─────────────┘  └─────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────┐
│                   GrydAuth.Infrastructure                       │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────────┐ │
│  │   JWT Svc   │  │ Repositories│  │ Permission Handler      │ │
│  │ - Generate  │  │ - User      │  │ - Policy Provider       │ │
│  │ - Validate  │  │ - Role      │  │ - Auth Handler          │ │
│  │ - Refresh   │  │ - Tenant    │  │ - Claim Builder         │ │
│  └─────────────┘  └─────────────┘  └─────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────┐
│                      GrydAuth.Domain                            │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────────┐ │
│  │  Entities   │  │Value Objects│  │    Domain Events        │ │
│  │  - User     │  │ - Email     │  │ - UserCreatedEvent      │ │
│  │  - Role     │  │ - Password  │  │ - RoleAssignedEvent     │ │
│  │  - Tenant   │  │ - Token     │  │ - PermissionChanged     │ │
│  └─────────────┘  └─────────────┘  └─────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘

📊 Entity Relationship Diagram

The following diagram shows the complete data model for GrydAuth, including all entities and their relationships:

100% 💡 Use Ctrl + Scroll para zoom | Arraste para navegar

Entity Descriptions

EntityTypeDescription
UserAggregate RootGlobal user entity, not tied to specific tenants
TenantAggregate RootOrganization/company in multi-tenancy system
UserTenantAggregate RootUser-Tenant relationship with roles and permissions
RoleAggregate RootGroups permissions together (tenant-scoped)
PermissionAggregate RootDefines actions that can be performed (tenant-scoped)
UserRoleEntityLinks UserTenant to Role
UserPermissionEntityDirect permission assignment to UserTenant
RolePermissionEntityLinks Role to Permission (tenant-scoped)
UserAppIdEntityMulti-app support - links User to application IDs
UserPasswordHistoryEntityPassword history for reuse prevention
FailedLoginAttemptEntitySecurity tracking for brute-force protection
PasswordResetTokenEntityToken for password reset flow

Key Relationships

  1. User ↔ Tenant (Many-to-Many via UserTenant)

    • Users exist globally, not tied to tenants
    • UserTenant manages the relationship with roles/permissions per tenant
  2. Role ↔ Permission (Many-to-Many via RolePermission)

    • Roles group permissions together
    • Both are tenant-scoped
  3. UserTenant ↔ Role/Permission

    • Users can have roles AND direct permissions per tenant
    • Permissions are evaluated from both sources
  4. User ↔ AppId (One-to-Many)

    • Users can access multiple applications
    • AppId is validated during login

📖 Documentation Sections

Released under the MIT License.