# engflow/auth architecture

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

- Interactive diagram: https://gitdiagram.com/engflow/auth
- Repository: https://github.com/engflow/auth

## Overview

##### Project Type and Purpose
- Type: A command‑line credential helper tool, written in Go, invoked by Bazel.
- Purpose: Automate “login/export/import/logout” of EngFlow auth tokens, storing them securely and supplying them to Bazel via the `--credential_helper` flag.

##### File Structure
- Top‑level build: Bazel (`WORKSPACE`, `BUILD`, `.bazelrc`, `MODULE.bazel`), Go modules (`go.mod`, `go.sum`).
- `cmd/engflow_auth`: Main entrypoint (`main.go`) and `tokens.go` define CLI commands: login, export, import, logout.
- `internal/…` packages implement core logic and abstractions:
– `oauthdevice`: implements OAuth2 device‑flow handshake.
– `oauthtoken`: defines token stores (keyring, file, debug, fake).
– `browser`: opens URLs in user’s default browser.
– `autherr`: centralized error types.
– `buildstamp`: embeds build metadata.
- `infra/…`: CI and release scripts, GitHub workflows, credential‑management helpers (`login.sh`, `logout.sh`).
- `platform/…`: cross‑compilation toolchain definitions for Linux/macOS/Windows.

##### Extract Architecture Insights from the README
- Auth flow:
– Interactive login: `engflow_auth login [CLUSTER URL]` → open browser → complete flow → token stored in system keyring.
– Non‑interactive (CI): `export` reads token, inject as secret, `import` writes token to file store, `logout` to clean up.
- Integration point: Bazel calls `engflow_auth` as credential helper to fetch tokens during remote operations.

- engflow_auth CLI binary
– OAuth device flow (`oauthdevice`)
– Token store abstraction (`oauthtoken`)
– Browser launcher (`browser`)
– Error handler (`autherr`)
- External Services:
– EngFlow Authentication Server (OAuth 2.0 endpoint)
– User’s Web Browser (for interactive flow)
- Local Persistence:
– System keyring (encrypted store)
– File store (plaintext, for CI)
- Bazel process (invoker of CLI)
- CI System (e.g., GitHub Actions)

- User / CI → engflow_auth (CLI invocation)
- engflow_auth → OAuth Server: device‑flow initiation & polling
- engflow_auth → Browser: open URL for user to authenticate
- OAuth Server → engflow_auth: token issuance
- engflow_auth → Token Store (keyring or file)
- Bazel → engflow_auth: fetch token at build time
- CI System → engflow_auth import/export/logout via scripts

c. Architectural Patterns & Principles:
- Modular separation: `cmd/` vs `internal/` packages.
- Abstraction of token storage (allows keyring or file).
- Single responsibility per internal package (device flow, storage, errors).
- Use of Bazel for reproducible builds and cross‑compilation.

d. Key Technologies / Frameworks:
- Go (language)
- Bazel (build & release artifacts)
- OAuth 2.0 device flow (authentication protocol)
- System keyring APIs (platform‐specific)
- GitHub Actions or other CI for non‑interactive use cases

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main CLI with internal modules
    subgraph "engflow_auth CLI" 
        direction TB
        main["main.go"]:::internal
        tokensDef["tokens.go"]:::internal
        oauthdevice["Device Flow Authenticator"]:::oauth
        oauthtoken["Token Store Abstraction"]:::internal
        browser["Browser Launcher"]:::browser
        autherr["Error Handler"]:::internal
        buildstamp["Build Metadata"]:::internal
    end

    %% External services
    OAuthServer["EngFlow OAuth 2.0 Server"]:::external
    UserBrowser["User's Web Browser"]:::external

    %% Persistence stores
    keyring["System Keyring"]:::store
    fileStore["File Store (CI)"]:::store

    %% Invokers
    User["End User"]:::external
    CI["CI System"]:::ci
    Bazel["Bazel Process"]:::bazel

    %% Bazel and Build Config
    subgraph "Bazel Configuration"
        direction TB
        workspace["WORKSPACE"]:::config
        buildFile["BUILD"]:::config
        moduleFile["MODULE.bazel"]:::config
        bazelrc[".bazelrc"]:::config
        bazelver[".bazelversion"]:::config
    end

    %% Infrastructure Scripts
    infra["infra (CI/Release Scripts)"]:::ci

    %% Platform Toolchains
    platform["platform (Cross-Compile Toolchains)"]:::config

    %% Relationships - Interactive Login
    User -->|invokes login| main
    main -->|initiate device flow| oauthdevice
    oauthdevice -->|open auth URL| browser
    browser --> UserBrowser
    oauthdevice -->|poll and retrieve token| OAuthServer
    OAuthServer -->|issue token| oauthdevice
    oauthdevice -->|store token| oauthtoken

    %% Token storage choices
    oauthtoken -->|to keyring| keyring
    oauthtoken -->|to file| fileStore

    %% Invocations by Bazel and CI
    Bazel -->|credential_helper fetch| main
    CI -->|export/import/logout| tokensDef

    %% Error handling and build metadata
    main --> autherr
    main --> buildstamp

    %% Config and infra relationships
    Bazel --> workspace
    Bazel --> buildFile
    Bazel --> moduleFile
    Bazel --> bazelrc
    Bazel --> bazelver
    CI --> infra
    Bazel --> platform

    %% Click events
    click main "https://github.com/engflow/auth/blob/main/cmd/engflow_auth/main.go"
    click tokensDef "https://github.com/engflow/auth/blob/main/cmd/engflow_auth/tokens.go"
    click oauthdevice "https://github.com/engflow/auth/blob/main/internal/oauthdevice/authenticator.go"
    click oauthtoken "https://github.com/engflow/auth/blob/main/internal/oauthtoken/load_storer.go"
    click browser "https://github.com/engflow/auth/blob/main/internal/browser/browser.go"
    click autherr "https://github.com/engflow/auth/blob/main/internal/autherr/coded_error.go"
    click buildstamp "https://github.com/engflow/auth/blob/main/internal/buildstamp/buildstamp.go"
    click workspace "https://github.com/engflow/auth/tree/main/WORKSPACE"
    click buildFile "https://github.com/engflow/auth/tree/main/BUILD"
    click moduleFile "https://github.com/engflow/auth/blob/main/MODULE.bazel"
    click bazelrc "https://github.com/engflow/auth/blob/main/.bazelrc"
    click bazelver "https://github.com/engflow/auth/blob/main/.bazelversion"
    click infra "https://github.com/engflow/auth/tree/main/infra/BUILD"
    click platform "https://github.com/engflow/auth/tree/main/platform/linux_x64/BUILD"

    %% Styles
    classDef internal fill:#D6E9C6,stroke:#3C763D,color:#3C763D;
    classDef external fill:#F2DEDE,stroke:#A94442,color:#A94442,stroke-dasharray: 5 5;
    classDef oauth fill:#FCF8E3,stroke:#8A6D3B,color:#8A6D3B;
    classDef browser fill:#D9EDF7,stroke:#31708F,color:#31708F;
    classDef store fill:#EBEDF0,stroke:#636363,color:#636363,shape:cylinder;
    classDef ci fill:#FAF2CC,stroke:#8A6D3B,color:#8A6D3B;
    classDef bazel fill:#E8E8FF,stroke:#4B4B8C,color:#4B4B8C;
    classDef config fill:#FFFFFF,stroke:#666666,color:#666666;
```
