# pinterest/snappass architecture

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

- Interactive diagram: https://gitdiagram.com/pinterest/snappass
- Repository: https://github.com/pinterest/snappass

## Overview

##### Major Components
- Client / User Interface:
- Web browser (or API client) interacting with the application.
- Uses a standard web UI (HTML templates, JavaScript, Bootstrap, and FontAwesome assets from snappass/templates and snappass/static).
- Flask Web Application:
- Located in the snappass folder with key files (__init__.py, main.py).
- Responsible for rendering templates, routing, and handling both Simple and REST APIs as described in the README.
- API Endpoints:
- Two API categories as described: the simple API (e.g., /api/set_password) and the REST API (e.g., /api/v2/passwords).
- These endpoints encapsulate the server’s logic to create, validate, and retrieve secure passwords.
- Encryption Layer:
- Uses the cryptography library’s Fernet encryption. This isn’t shown as a separate file, but it is an integral part of how the password is secured before storing.
- Redis Store:
- Acts as the temporary, in-memory data store for passwords.
- The README highlights that Redis is used to save encrypted and ephemeral password data. Environment settings (e.g., REDIS_HOST, REDIS_PORT) point to Redis configuration.
- External/Deployment Components:
- Docker and Docker Compose configuration for containerized deployment (Dockerfile, docker-compose.yml).
- Continuous Integration aspects (GitHub workflows, Makefile) – these are separate from runtime architecture but worth noting in the overall system for build and deployment pipelines.

##### Establish Relationships and Interactions
- The Client sends HTTP requests (GET, POST, HEAD) to the Flask application.
- Flask routes requests to the appropriate view functions based on the URL—this includes either rendering an HTML page (password preview or set_password screens) or processing an API request.
- When a password is created:
- The application encrypts the password on the fly using Fernet.
- It then stores metadata in Redis (keyed with a unique token) without saving the encryption key (passed with the link).
- For password retrieval:
- Validations are made (check if the password exists, is not expired or already read).
- The encrypted password is sent if the correct token is provided.
- Docker/Docker Compose is used to spin up both the Flask application and the Redis store, with configuration provided in the docker-compose.yml file.

##### Final Checklist
- Every component is clearly labeled and easy to understand.
- Annotations to represent the major configuration options and any notes on security (e.g., using Fernet) as per the README.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client / UI Layer
    subgraph "Client / UI"
        Templates("Templates"):::client
        Static("Static Assets"):::client
        Browser("Web Browser / API Client"):::client
    end
    Templates --> Browser
    Static --> Browser

    %% Application Layer
    subgraph "Flask Web Application"
        Init("Flask Init"):::flask
        Main("Flask Main & API Endpoints"):::flask
        Encryption("Encryption (Fernet)"):::flask
    end

    %% Data Store
    Redis["Redis Store"]:::database

    %% Deployment / CI Layer
    subgraph "Deployment / CI"
        Docker("Docker Container Environment"):::deployment
        Dockerfile("Dockerfile"):::deployment
        Compose("docker-compose.yml"):::deployment
        CI("Makefile"):::deployment
    end

    %% Data Flow
    Browser -->|"HTTPRequests(GET,POST,HEAD)"| Main
    Main -->|"Encrypt &Process"| Encryption
    Main -->|"Store/Retrieve\nencrypted data"| Redis
    Docker -->|"Orchestrates"| Main
    Docker -->|"Orchestrates"| Redis
    Docker --> Dockerfile
    Docker --> Compose
    Docker --> CI

    %% Click Events
    click Templates "https://github.com/pinterest/snappass/tree/master/snappass/templates"
    click Static "https://github.com/pinterest/snappass/tree/master/snappass/static"
    click Init "https://github.com/pinterest/snappass/blob/master/snappass/__init__.py"
    click Main "https://github.com/pinterest/snappass/blob/master/snappass/main.py"
    click Dockerfile "https://github.com/pinterest/snappass/tree/master/Dockerfile"
    click Compose "https://github.com/pinterest/snappass/blob/master/docker-compose.yml"
    click CI "https://github.com/pinterest/snappass/tree/master/Makefile"

    %% Classes and Styles
    classDef client fill:#a2d2ff,stroke:#000,stroke-width:1px
    classDef flask fill:#ffafcc,stroke:#000,stroke-width:1px
    classDef database fill:#caffbf,stroke:#000,stroke-width:1px
    classDef deployment fill:#ffd6a5,stroke:#000,stroke-width:1px
```
