# bs3dev/jwt-refresh-token architecture

> Architecture diagram of the GitHub repository bs3dev/jwt-refresh-token, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bs3dev/jwt-refresh-token
- Repository: https://github.com/bs3dev/jwt-refresh-token

## Overview

##### Project type and purpose
- This is a .NET library implementing the “Refresh Token” pattern for JWT authentication.
- It follows Onion Architecture and is intended to be consumed by client applications (e.g., minimal API sample under `/sample/Ui/...`).
- It provides core services for issuing, refreshing, and revoking JWT access tokens, backed by configurable data stores (currently Azure Cosmos DB).

##### File structure
- Top-level folder `src` contains four projects:
- Domain (core entities, DTOs, service interfaces, configuration).
- Application (application-level services and interfaces, e.g. ITokenAppService).
- Infrastructure (two subfolders: one for Cosmos DB implementation, one for generic HTTP/serialization extensions).
- Tests (unit tests under `Tests.Unit`, integration tests under `Tests.Integration.Cosmos`).
- A separate `sample` folder holds a minimal .NET API demonstrating usage, with its own minimal controllers, Program.cs, and entities/repositories for credentials.
- Build pipelines live in `.github/workflows/build.yml`.

##### README for additional insights
- Confirms Onion Architecture layering: Infrastructure depends on Application, Application depends on Domain.
- Lists supported databases (Cosmos DB current, others planned).
- Describes registration in Program.cs (DI of CosmosClient, registration of services and endpoints).

##### Detailed capturing of essential elements
- JSON config being read by both API and Infrastructure for descriptor and Cosmos settings.
- Test flows separately if desired: Integration tests directly call TokenAppService with a fake repository or real CosmosClient.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client["Client (Postman/browser/mobile)"]:::external
    click Client "https://github.com/bs3dev/jwt-refresh-token/blob/main/sample/Jwt.Refresh.Token.postman_collection.json"

    %% API Host - Sample UI
    subgraph "API Host - Sample UI" 
        direction TB
        Program["Program.cs"]:::host
        UserEntity["UserEntity"]:::host
        CredentialRepo["CredentialRepository"]:::host
        EndpointExt["TokenEndpointExtension"]:::infra
        TokenResultExt["TokenResultExtension"]:::infra
        DIInfra["DiExtension (Generic Infra)"]:::infra
        DIcosmos["DiExtension (Cosmos DB)"]:::infra
    end
    click Program "https://github.com/bs3dev/jwt-refresh-token/blob/main/sample/Ui/Jwt.Refresh.Token.Sample.Ui.Api/Program.cs"
    click UserEntity "https://github.com/bs3dev/jwt-refresh-token/blob/main/sample/Ui/Jwt.Refresh.Token.Sample.Ui.Api/Entities/UserEntity.cs"
    click CredentialRepo "https://github.com/bs3dev/jwt-refresh-token/blob/main/sample/Ui/Jwt.Refresh.Token.Sample.Ui.Api/Entities/Repositories/CredentialRepository.cs"
    click EndpointExt "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Infrastructure/Jwt.Refresh.Token.Infra/Extensions/TokenEndpointExtension.cs"
    click TokenResultExt "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Infrastructure/Jwt.Refresh.Token.Infra/Extensions/TokenResultExtension.cs"
    click DIInfra "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Infrastructure/Jwt.Refresh.Token.Infra/Extensions/DiExtension.cs"
    click DIcosmos "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Infrastructure/Jwt.Refresh.Token.Infra.Cosmos/Extensions/DiExtension.cs"

    %% Onion Layers
    subgraph "Domain Layer" 
        direction TB
        TokenEntity["TokenEntity"]:::domain
        TokenDTO["Token DTO"]:::domain
        ITokenRepoIF["ITokenRepository"]:::domain
        ICredRepoIF["ICredentialRepository"]:::domain
        IJwtDbConfig["IJwtRefreshTokenDatabaseConfig"]:::domain
        IJwtAccessSvcIF["IJwtAccessTokenService"]:::domain
        IClaimsSvcIF["IClaimsService"]:::domain
        IClaimsIdSvcIF["IClaimsIdentityService"]:::domain
        JwtAccessSvc["JwtAccessTokenService"]:::domain
        ClaimsIdSvc["ClaimsIdentityService"]:::domain
    end
    click TokenEntity "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Domain/Jwt.Refresh.Token.Domain/Entities/TokenEntity.cs"
    click TokenDTO "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Domain/Jwt.Refresh.Token.Domain/DataTransferObjects/Token.cs"
    click ITokenRepoIF "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Domain/Jwt.Refresh.Token.Domain/Entities/Repositories/ITokenRepository.cs"
    click ICredRepoIF "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Domain/Jwt.Refresh.Token.Domain/Entities/Repositories/ICredentialRepository.cs"
    click IJwtDbConfig "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Domain/Jwt.Refresh.Token.Domain/Configs/IJwtRefreshTokenDatabaseConfig.cs"
    click IJwtAccessSvcIF "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Domain/Jwt.Refresh.Token.Domain/Services/Interfaces/IJwtAccessTokenService.cs"
    click IClaimsSvcIF "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Domain/Jwt.Refresh.Token.Domain/Services/Interfaces/IClaimsService.cs"
    click IClaimsIdSvcIF "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Domain/Jwt.Refresh.Token.Domain/Services/Interfaces/IClaimsIdentityService.cs"
    click JwtAccessSvc "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Domain/Jwt.Refresh.Token.Domain/Services/JwtAccessTokenService.cs"
    click ClaimsIdSvc "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Domain/Jwt.Refresh.Token.Domain/Services/ClaimsIdentityService.cs"

    subgraph "Application Layer" 
        direction TB
        ITokenAppSvcIF["ITokenAppService"]:::app
        TokenAppSvc["TokenAppService"]:::app
    end
    click ITokenAppSvcIF "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Application/Jwt.Refresh.Token.Application/Interfaces/ITokenAppService.cs"
    click TokenAppSvc "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Application/Jwt.Refresh.Token.Application/TokenAppService.cs"

    subgraph "Infrastructure Layer" 
        direction TB
        subgraph "Cosmos DB Implementation"
            direction TB
            CosmosConfig["JwtRefreshTokenCosmosConfig"]:::infra
            TokenRepo["TokenRepository"]:::infra
            CosmosSerializer["SystemTextJsonCosmosSerializer"]:::infra
        end
        subgraph "HTTP/Serialization Infra"
            direction TB
            JsonContext["AppJsonContext"]:::infra
        end
    end
    click CosmosConfig "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Infrastructure/Jwt.Refresh.Token.Infra.Cosmos/Configs/JwtRefreshTokenCosmosConfig.cs"
    click TokenRepo "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Infrastructure/Jwt.Refresh.Token.Infra.Cosmos/Entities/Repositories/TokenRepository.cs"
    click CosmosSerializer "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Infrastructure/Jwt.Refresh.Token.Infra.Cosmos/Serializers/SystemTextJsonCosmosSerializer.cs"
    click JsonContext "https://github.com/bs3dev/jwt-refresh-token/blob/main/src/Infrastructure/Jwt.Refresh.Token.Infra/Serializers/AppJsonContext.cs"

    %% External Database
    CosmosDB[(Azure Cosmos DB)]:::external

    %% Connections
    Client -->|"HTTP Request"| EndpointExt
    Program -->|"calls MapTokenEndpoints"| EndpointExt
    Program -->|"calls MapTokenResult"| TokenResultExt
    Program -->|"DI registration"| DIInfra
    Program -->|"DI registration"| DIcosmos
    EndpointExt -->|"calls ITokenAppService"| ITokenAppSvcIF
    TokenResultExt -->|"formats DTO"| TokenDTO

    ITokenAppSvcIF -->|"implemented by"| TokenAppSvc
    TokenAppSvc -->|"uses IClaimsIdentityService"| IClaimsIdSvcIF
    TokenAppSvc -->|"uses IJwtAccessTokenService"| IJwtAccessSvcIF
    TokenAppSvc -->|"uses ITokenRepository"| ITokenRepoIF
    TokenAppSvc -->|"uses ICredentialRepository"| ICredRepoIF

    IClaimsIdSvcIF -->|"implemented by"| ClaimsIdSvc
    IJwtAccessSvcIF -->|"implemented by"| JwtAccessSvc

    DIInfra -->|"wires ICredentialRepository to host impl"| ICredRepoIF
    DIInfra -->|"wires Domain services to host"| IClaimsIdSvcIF & IClaimsSvcIF & IJwtAccessSvcIF
    DIcosmos -->|"wires ITokenRepository to Cosmos impl"| ITokenRepoIF & IJwtDbConfig

    CosmosConfig --> CosmosSerializer
    TokenRepo --> CosmosSerializer
    TokenRepo --> CosmosDB

    JsonContext --> EndpointExt
    JsonContext --> CosmosSerializer

    %% Styles
    classDef domain fill:#e0f7fa,stroke:#006064,color:#004d40
    classDef app fill:#e8f5e9,stroke:#1b5e20,color:#2e7d32
    classDef infra fill:#fff3e0,stroke:#e65100,color:#ef6c00
    classDef host fill:#f3e5f5,stroke:#6a1b9a,color:#4a148c
    classDef external fill:#eceff1,stroke:#607d8b,color:#455a64

    class Client,CosmosDB external
    class Program,UserEntity,CredentialRepo host
    class TokenEntity,TokenDTO,ITokenRepoIF,ICredRepoIF,IJwtDbConfig,IJwtAccessSvcIF,IClaimsSvcIF,IClaimsIdSvcIF,JwtAccessSvc,ClaimsIdSvc domain
    class ITokenAppSvcIF,TokenAppSvc app
    class EndpointExt,TokenResultExt,DIInfra,DIcosmos,CosmosConfig,TokenRepo,CosmosSerializer,JsonContext infra
```
