# simformsolutionspvtltd/flutter_ssl_pinning architecture

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

- Interactive diagram: https://gitdiagram.com/simformsolutionspvtltd/flutter_ssl_pinning
- Repository: https://github.com/simformsolutionspvtltd/flutter_ssl_pinning

## Overview

##### Project type and purpose
- This is a Flutter cross-platform client application (mobile, desktop, web) whose sole responsibility is to demonstrate and implement SSL/TLS certificate pinning in outgoing HTTP requests.
- There is no backend code in this repo; instead the app communicates with an external API server over HTTPS and ensures only a pinned certificate is accepted.

##### File structure
- Top-level “android”, “ios”, “linux”, “macos”, “windows”, “web” directories: platform shells and build scripts generated by Flutter.
- “lib/” contains the entire Dart codebase:
- main.dart – entry point and UI widget tree
- model/ – data model classes (e.g. UserResponseDm)
- network/ – repository and networking code:
· api_base_repository.dart – base class that configures Dio and HttpClient
· certificate_utils.dart – loads and pins the certificate into a SecurityContext
· user_repository.dart – concrete repository for user-related API calls
- pubspec.yaml – declares dependencies (dio, flutter) and asset (certificate file) locations.
- test/ – example widget test.

##### Key README insights
- Emphasizes use of dio package for network requests.
- Instructs on placing the server’s SSL certificate in assets.
- Describes creating and wiring a SecurityContext to an HttpClient for strict pinning and rejecting all other certificates.

##### Main components
A. Platform Shells
- Android / iOS / macOS / Linux / Windows / Web containers (Flutter engine + native bootstrap)
B. Flutter Engine & Runtime
- Skia/OpenGL renderer, Dart VM (in debug/profile) or AOT runtime (in release)
C. Dart Layers
1. Presentation Layer
– Widgets and UI in main.dart
2. Data Model Layer
– user_response_dm.dart and other DTOs
3. Network Layer
a. CertificateUtils
- Loads PEM file from assets → sets up SecurityContext
b. ApiBaseRepository
- Configures Dio’s HttpClientAdapter to use the SecurityContext
- Overrides badCertificateCallback → rejects untrusted certs
c. UserRepository
- Exposes methods (e.g., fetchUser) that call ApiBaseRepository
D. External Services
- Remote HTTPS API Server with pinned certificate

##### Interactions and data flow
- UI Widget issues a “fetch” action → calls UserRepository.fetchUser()
- UserRepository → ApiBaseRepository → constructs Dio request
- ApiBaseRepository → SecurityContext (from CertificateUtils) is attached to HttpClient
- HttpClient performs TLS handshake
- Only accepts the exact pinned certificate
- Rejects others via badCertificateCallback
- On success, JSON is returned → parsed into UserResponseDm → bubbled back to UI

##### Architectural patterns & principles
- Layered architecture: clear separation between UI, business logic (repositories), models, and networking.
- Single-responsibility: Certificate pinning logic isolated in CertificateUtils.
- Dependency inversion: repository depends on abstracted ApiBaseRepository rather than on Dio directly.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Platform Shells"
        Android["Android Shell"]:::platform
        IOS["iOS Shell"]:::platform
        Linux["Linux Shell"]:::platform
        macOS["macOS Shell"]:::platform
        Windows["Windows Shell"]:::platform
        Web["Web Shell"]:::platform
    end

    subgraph "Flutter Engine & Runtime"
        Engine["Flutter Engine & Dart VM/AOT"]:::engine
    end

    subgraph "Dart Layers"
        subgraph "Presentation Layer"
            UI["UI Widgets (main.dart)"]:::presentation
        end
        subgraph "Data Model Layer"
            DM["UserResponseDm"]:::model
        end
        subgraph "Network Layer"
            CertUtils["CertificateUtils"]:::network
            ApiBase["ApiBaseRepository"]:::network
            UserRepo["UserRepository"]:::network
        end
    end

    External["External HTTPS API Server"]:::external
    Assets["Certificates (assets/)"]:::asset

    Android --> Engine
    IOS --> Engine
    Linux --> Engine
    macOS --> Engine
    Windows --> Engine
    Web --> Engine

    Engine --> UI
    UI -->|"fetchUser()"| UserRepo
    UserRepo -->|"calls ApiBaseRepository"| ApiBase
    Assets -->|"loads PEM"| CertUtils
    CertUtils -->|"configures SecurityContext"| ApiBase
    ApiBase -->|"HTTPS request"| External
    External -->|"JSON response"| ApiBase
    ApiBase -->|"parses JSON"| DM
    DM -->|"returns data"| UI

    click Android "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/tree/master/android/"
    click IOS "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/tree/master/ios/"
    click Linux "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/tree/master/linux/"
    click macOS "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/tree/master/macos/"
    click Windows "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/tree/master/windows/"
    click Web "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/tree/master/web/"
    click UI "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/blob/master/lib/main.dart"
    click DM "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/blob/master/lib/model/user_response_dm.dart"
    click CertUtils "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/blob/master/lib/network/certificate_utils.dart"
    click ApiBase "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/blob/master/lib/network/api_base_repository.dart"
    click UserRepo "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/blob/master/lib/network/user_repository.dart"
    click pubspec "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/blob/master/pubspec.yaml"
    click test "https://github.com/simformsolutionspvtltd/flutter_ssl_pinning/blob/master/test/widget_test.dart"

    classDef platform fill:#D6EAF8,stroke:#2980B9
    classDef engine fill:#AED6F1,stroke:#21618C
    classDef presentation fill:#82E0AA,stroke:#117A65
    classDef model fill:#F9E79F,stroke:#B7950B
    classDef network fill:#F5B7B1,stroke:#C0392B
    classDef asset fill:#D2B4DE,stroke:#7D3C98
    classDef external fill:#BDC3C7,stroke:#7F8C8D
```
