# jasonraimondi/ts-oauth2-server architecture

> Architecture diagram of the GitHub repository jasonraimondi/ts-oauth2-server, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jasonraimondi/ts-oauth2-server
- Repository: https://github.com/jasonraimondi/ts-oauth2-server

## Overview

##### Main Components
- Authorization Server Core: Represented by src/authorization_server.ts, this is the heart of the OAuth2 logic.
- Grant Implementations: Under src/grants, you have distinct modules for each grant type (authorization code, client credentials, implicit, password, refresh token, and token exchange). These modules define the different flows.
- Adapters: The src/adapters folder shows implementations for Express, Fastify, and VanillaJS. They provide framework-specific interfaces for handling OAuth requests.
- Code Verifiers: Located in src/code_verifiers, these support PKCE via S256 and plain verifier modules.
- Data Models/Entities: Found in src/entities, these modules represent the core data (clients, tokens, auth codes, scopes, users).
- Data Repositories: The src/repositories folder contains modules responsible for data persistence/access (for example, client, auth_code, access_token or user repositories).
- Request and Response Handling: The src/requests and src/responses directories encapsulate the incoming OAuth requests and the corresponding responses (e.g., bearer token or redirect responses).
- Utilities & Exceptions: Utility modules (in src/utils) provide supportive functions (e.g., error handling, token generation, date handling) while exceptions are defined in src/exceptions.
- Tests and Examples: The test folder (with both end-to-end and unit tests) and the example directory illustrate usage and validate functionality.

##### Component Relationships and Interactions
- The Authorization Server Core interacts with:
- Grant Implementations to process specific OAuth flows.
- Adapters that integrate with various Node.js frameworks.
- Code Verifiers when handling PKCE-enabled grant flows.
- Repositories to persist and retrieve entities (tokens, clients, etc.).
- Incoming requests (handled by adapters) are routed into the Authorization Server, where the appropriate grant is selected based on the request type.
- The Request modules parse the incoming OAuth requests and then trigger internal business logic.
- Response modules take the results from OAuth processes and format them for delivery back to the client.
- The utility modules and exception handling ensure consistency and error management across these interactions.

##### Architectural Patterns and Design Principles
- Modularity: Clear separation between the core authorization logic, grant implementations, data repositories, and adapter integrations.
- Adapter Pattern: The different adapters (Express, Fastify, VanillaJS) allow the library to remain framework-agnostic.
- Separation of Concerns: The division into entities, repositories, requests, and responses reflects the design’s emphasis on clear responsibility boundaries.
- Extensibility: Developers can add custom adapters, grant types, or extend repositories without changing the core logic.
- Testability: The presence of both unit and end-to-end tests demonstrates a design conducive to automated testing.

##### Be Detailed but Clear
- Each module/component should be represented with a label that matches the directory structure (e.g., src/adapters, src/grants, src/entities, etc.).
- How the system’s extensibility is achieved, indicating where custom grants or adapters could be plugged in.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry Points Subgraph: Adapters
    subgraph "Adapters"
        a1("Express Adapter"):::adapter
        a2("Fastify Adapter"):::adapter
        a3("VanillaJS Adapter"):::adapter
    end

    %% I/O Subgraph: Request Handling
    subgraph "Request Handling"
        rq1("Authorization Request"):::request
        rq2("Generic Request"):::request
    end

    %% Core Processing Subgraph
    subgraph "Core Processing"
        ac("Authorization Server Core"):::core

        %% Grants Subgraph inside Core
        subgraph "Grants"
            g1("Auth Code Grant"):::grant
            g2("Client Credentials Grant"):::grant
            g3("Implicit Grant"):::grant
            g4("Password Grant"):::grant
            g5("Refresh Token Grant"):::grant
            g6("Token Exchange Grant"):::grant
            subgraph "Abstract/Custom Grants"
                g_custom("Abstract Grants"):::grant
            end
        end

        %% Code Verifiers Subgraph inside Core
        subgraph "Code Verifiers"
            cv1("S256 Verifier"):::code
            cv2("Plain Verifier"):::code
            cv3("Verifier"):::code
        end

        %% Utilities & Exceptions Subgraph inside Core
        subgraph "Utilities & Exceptions"
            ut1("Utilities Module"):::util
            ex1("OAuth Exception"):::exception
        end

        %% Response Handling Subgraph inside Core
        subgraph "Response Handling"
            rs1("Bearer Token Response"):::response
            rs2("Redirect Response"):::response
            rs3("Generic Response"):::response
        end
    end

    %% Data Persistence Subgraph
    subgraph "Data Persistence"
        subgraph "Entities"
            e1("Client Entity"):::entity
            e2("Auth Code Entity"):::entity
            e3("Scope Entity"):::entity
            e4("Token Entity"):::entity
            e5("User Entity"):::entity
        end
        subgraph "Repositories"
            r1("Access Token Repository"):::repo
            r2("Auth Code Repository"):::repo
            r3("Client Repository"):::repo
            r4("Scope Repository"):::repo
            r5("User Repository"):::repo
        end
    end

    %% Tests & Examples Subgraph
    subgraph "Tests & Examples"
        t1("Tests"):::test
        exa1("Examples"):::example
    end

    %% Connections: Adapters to Core
    a1 -->|"routes"| ac
    a2 -->|"routes"| ac
    a3 -->|"routes"| ac

    %% Connections: Request Handling to Core
    rq1 -->|"parsed"| ac
    rq2 -->|"parsed"| ac

    %% Connections: Core to Grants
    ac -->|"invokes"| g1
    ac -->|"invokes"| g2
    ac -->|"invokes"| g3
    ac -->|"invokes"| g4
    ac -->|"invokes"| g5
    ac -->|"invokes"| g6
    ac -->|"invokes"| g_custom

    %% Connections: Core to Code Verifiers
    ac -->|"validates"| cv1
    ac -->|"validates"| cv2
    ac -->|"validates"| cv3

    %% Connections: Core to Utilities & Exceptions
    ac -->|"utilizes"| ut1
    ac -->|"handles"| ex1

    %% Connections: Core to Response Handling
    ac -->|"formats"| rs1
    ac -->|"formats"| rs2
    ac -->|"formats"| rs3

    %% Connections: Core to Repositories
    ac -->|"persists"| r1
    ac -->|"persists"| r2
    ac -->|"persists"| r3
    ac -->|"persists"| r4
    ac -->|"persists"| r5

    %% Connections: Repositories to Entities
    r1 -->|"manages"| e4
    r2 -->|"manages"| e2
    r3 -->|"manages"| e1
    r4 -->|"manages"| e3
    r5 -->|"manages"| e5

    %% Click Events
    click ac "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/authorization_server.ts"
    click g1 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/grants/auth_code.grant.ts"
    click g2 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/grants/client_credentials.grant.ts"
    click g3 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/grants/implicit.grant.ts"
    click g4 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/grants/password.grant.ts"
    click g5 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/grants/refresh_token.grant.ts"
    click g6 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/grants/token_exchange.grant.ts"
    click g_custom "https://github.com/jasonraimondi/ts-oauth2-server/tree/main/src/grants/abstract/"
    click a1 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/adapters/express.ts"
    click a2 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/adapters/fastify.ts"
    click a3 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/adapters/vanilla.ts"
    click cv1 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/code_verifiers/S256.verifier.ts"
    click cv2 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/code_verifiers/plain.verifier.ts"
    click cv3 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/code_verifiers/verifier.ts"
    click e1 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/entities/client.entity.ts"
    click e2 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/entities/auth_code.entity.ts"
    click e3 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/entities/scope.entity.ts"
    click e4 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/entities/token.entity.ts"
    click e5 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/entities/user.entity.ts"
    click r1 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/repositories/access_token.repository.ts"
    click r2 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/repositories/auth_code.repository.ts"
    click r3 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/repositories/client.repository.ts"
    click r4 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/repositories/scope.repository.ts"
    click r5 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/repositories/user.repository.ts"
    click rq1 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/requests/authorization.request.ts"
    click rq2 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/requests/request.ts"
    click rs1 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/responses/bearer_token.response.ts"
    click rs2 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/responses/redirect.response.ts"
    click rs3 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/responses/response.ts"
    click ut1 "https://github.com/jasonraimondi/ts-oauth2-server/tree/main/src/utils"
    click ex1 "https://github.com/jasonraimondi/ts-oauth2-server/blob/main/src/exceptions/oauth.exception.ts"
    click t1 "https://github.com/jasonraimondi/ts-oauth2-server/tree/main/test/"
    click exa1 "https://github.com/jasonraimondi/ts-oauth2-server/tree/main/example/"

    %% Styles
    classDef core fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef adapter fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef grant fill:#FFFF99,stroke:#000,stroke-width:1px;
    classDef code fill:#FFC0CB,stroke:#000,stroke-width:1px;
    classDef entity fill:#D3D3D3,stroke:#000,stroke-width:1px;
    classDef repo fill:#D3D3D3,stroke:#000,stroke-width:1px;
    classDef request fill:#F0E68C,stroke:#000,stroke-width:1px;
    classDef response fill:#F0E68C,stroke:#000,stroke-width:1px;
    classDef util fill:#E6E6FA,stroke:#000,stroke-width:1px;
    classDef exception fill:#E6E6FA,stroke:#000,stroke-width:1px;
    classDef test fill:#FFE4B5,stroke:#000,stroke-width:1px;
    classDef example fill:#FFE4B5,stroke:#000,stroke-width:1px;
```
