# do1e/njulogin architecture

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

- Interactive diagram: https://gitdiagram.com/do1e/njulogin
- Repository: https://github.com/do1e/njulogin

## Overview

##### Project Overview and Purpose
- NJUlogin is a Python library providing unified authentication for 南京大学 (NJU) services. It supports three main login methods: QR (扫码) login, password-based login, and cookie loading.
- The library interacts with NJU’s authentication servers and provides a layer on top of the HTTP request mechanism (via its own get/post methods) to maintain session information.
- The README and file structure confirm that it is a standalone authentication module (rather than a full-stack web application or compiler). Its primary goal is to handle different login flows and integrate a captcha recognition component.

##### Analyzing the File Structure
- Top-level direct items include configuration files (.gitignore, LICENSE, requirements.txt, etc.) and directories like NJUlogin (the package), demos (usage examples), and test.
- Within the NJUlogin package, there are three key areas:
- Login Strategy Modules:
▪ QRlogin.py – Implements QR code-based login.
▪ pwdLogin.py – Implements login based on username and password.
▪ base.py – Likely serves as the shared base layer for login sessions (including cookies export/load).
- Captcha OCR Module:
▪ Located in NJUlogin/captchaOCR; it includes scripts and an ONNX model (common.onnx) to handle captcha recognition. Files such as ocr.py, charsets.py, and utils.py assist in extracting the captcha text.
- Utility Components:
▪ Located in NJUlogin/utils; these provide various helper functionalities like configuration management (config.py), URL processing (urls.py), and request-related operations (get_post.py).

##### Architectural Components and Their Relationships
- Core Login Process:
- The main authentication process is built around creating a session that interacts with external authentication servers.
- The base component and exported login objects (from QRlogin, pwdLogin, etc.) wrap around the Python requests library to handle HTTP GET/POST calls.
- Login Strategies:
- QRlogin: Handles the generation, display (both printed QR code and saved image backup), and validation of the QR code.
- pwdLogin: Uses user-provided credentials to authenticate through the web gateway.
- Base login (and its cookie export/load functionality) provides a foundation that underpins both strategies.
- Captcha Recognition:
- The captchaOCR module is plugged into the login sequences to handle any captcha requirements. It processes images/captchas via an ONNX model and performs OCR to ease the login process.
- Utilities:
- Utilities support auxiliary tasks needed by the above modules such as configuration loading, URL handling, and request preparation.
- External Interaction:

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "NJUlogin Package"
        direction TB
        BASE["Core Session / Base Login"]:::internal
        QR["QR Login: Generates_and_validates_QR_code"]:::internal
        PWD["Password Login: Credential-based_authentication"]:::internal
    end

    CAP["Captcha OCR Module: Uses_ONNX_model_for_captcha"]:::captcha
    UTIL["Utility Functions: Config,_URLs,_HTTP_helpers"]:::utility
    HTTP["HTTP Requests Library"]:::external
    EXT["External Authentication Server"]:::external

    %% Relationships within NJUlogin Package
    QR -->|"uses"| BASE
    PWD -->|"uses"| BASE

    %% Captcha interaction from login strategies
    QR -->|"calls_captcha"| CAP
    PWD -->|"calls_captcha"| CAP

    %% Utilization of utility functions by login modules
    BASE -->|"uses"| UTIL
    QR -->|"uses"| UTIL
    PWD -->|"uses"| UTIL

    %% Base component handles HTTP communication and external interactions
    BASE -->|"wraps_requests"| HTTP
    BASE -->|"communicates_with"| EXT

    %% Styling definitions
    classDef internal fill:#B3E5FC,stroke:#0288D1,stroke-width:2px;
    classDef captcha fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
    classDef utility fill:#FFF9C4,stroke:#F9A825,stroke-width:2px;
    classDef external fill:#FFCDD2,stroke:#C62828,stroke-width:2px;

    %% Click Events for component mapping
    click BASE "https://github.com/do1e/njulogin/blob/main/NJUlogin/base.py"
    click QR "https://github.com/do1e/njulogin/blob/main/NJUlogin/QRlogin.py"
    click PWD "https://github.com/do1e/njulogin/blob/main/NJUlogin/pwdLogin.py"
    click CAP "https://github.com/do1e/njulogin/tree/main/NJUlogin/captchaOCR"
    click UTIL "https://github.com/do1e/njulogin/tree/main/NJUlogin/utils"
```
