Skip to content

Heyson-b/symmetrical-sniffle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

AI Solutions for CPA Firm Automation

License Python Security

A comprehensive AI-powered solution for automating finance, audit, and accounting tasks in CPA firm environments with enterprise-grade security controls.

🎯 Features

AI-Powered Automation

  • Invoice Processing: Automatically extract and validate data from invoices using NLP and pattern recognition
  • Expense Categorization: ML-powered expense classification with GL account suggestions
  • Audit Trail Automation: Comprehensive audit logging with anomaly detection
  • Financial Reconciliation: Fuzzy matching for bank and book transaction reconciliation
  • Fraud Detection: Multi-layered anomaly detection including Benford's Law analysis

Security & Compliance

  • Data Encryption: AES-256 encryption for sensitive financial data
  • Access Control: Role-based permissions with JWT authentication
  • Audit Logging: Comprehensive activity tracking for compliance
  • Secure API: Rate-limited REST API with HTTPS support
  • Input Sanitization: Protection against injection attacks
  • Data Retention: Configurable retention policies for regulatory compliance

πŸ“‹ Table of Contents

πŸš€ Installation

Prerequisites

  • Python 3.8 or higher
  • pip package manager
  • Virtual environment (recommended)

Step-by-Step Installation

  1. Clone the repository
git clone https://github.com/HHR-CPA/vigilant-octo-engine.git
cd vigilant-octo-engine
  1. Create and activate virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt
  1. Configure environment variables
cp .env.example .env
# Edit .env with your configuration
  1. Initialize secure storage directories
mkdir -p logs models secure_data
chmod 700 secure_data  # Restrict access on Unix systems

βš™οΈ Configuration

Environment Variables

Copy .env.example to .env and configure the following:

# Security Configuration
SECRET_KEY=your-secret-key-here-change-in-production
ENCRYPTION_KEY=your-encryption-key-here-change-in-production

# Database Configuration
DATABASE_URL=sqlite:///./cpa_finance.db

# Audit Logging
AUDIT_LOG_PATH=./logs/audit.log
AUDIT_LOG_RETENTION_DAYS=365

# API Configuration
API_HOST=0.0.0.0
API_PORT=8000
API_RATE_LIMIT=100/minute

⚠️ Important:

  • Generate secure random keys for production
  • Never commit .env to version control
  • Use PostgreSQL for production environments
  • Enable HTTPS with valid SSL certificates

πŸ“– Usage

Starting the API Server

python src/api.py

The API will be available at http://localhost:8000

Using the AI Modules

Invoice Processing

from src.invoice_processing import InvoiceProcessor

processor = InvoiceProcessor()

invoice_text = """
ACME Corp
Invoice #INV-2024-001
Date: 01/15/2024
Total: $1,250.00
"""

invoice = processor.extract_invoice_data(invoice_text)
is_valid, errors = processor.validate_invoice(invoice)
category = processor.categorize_expense(invoice)

print(f"Category: {category}")
print(f"Valid: {is_valid}")

Expense Categorization

from src.expense_categorization import ExpenseCategorizer

categorizer = ExpenseCategorizer()

category, confidence = categorizer.categorize(
    description="Microsoft Office 365 Subscription",
    vendor="Microsoft",
    amount=150.00
)

gl_account = categorizer.suggest_gl_account(category)

print(f"Category: {category} (Confidence: {confidence:.2%})")
print(f"GL Account: {gl_account}")

Anomaly Detection

from src.anomaly_detection import AnomalyDetector
import pandas as pd

detector = AnomalyDetector()

transactions = pd.DataFrame({
    'amount': [100, 150, 120, 5000, 110],
    'vendor': ['A', 'B', 'A', 'C', 'B'],
    'date': ['2024-01-15', '2024-01-16', '2024-01-17', '2024-01-18', '2024-01-19']
})

results = detector.detect_transaction_anomalies(transactions)
anomalies = results[results['is_anomaly']]

print(f"Detected {len(anomalies)} anomalies")

Secure Data Handling

from src.security import EncryptionManager, AccessControl

# Encryption
encryption = EncryptionManager()
sensitive_data = {"account": "123456", "balance": 50000}
encrypted = encryption.encrypt_dict(sensitive_data)
decrypted = encryption.decrypt_dict(encrypted)

# Authentication
access_control = AccessControl()
token = access_control.create_access_token({"user": "john", "role": "accountant"})
user_data = access_control.verify_token(token)

πŸ”Œ API Documentation

Authentication

All API endpoints (except /api/health) require authentication using JWT tokens.

Login

curl -X POST http://localhost:8000/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username": "demo", "password": "Demo123!"}'

Use Token

curl -X POST http://localhost:8000/api/invoice/process \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"invoice_text": "..."}'

Main Endpoints

Endpoint Method Description Auth Required
/api/auth/login POST Authenticate and get token No
/api/invoice/process POST Process invoice Yes
/api/expense/categorize POST Categorize expense Yes
/api/audit/detect-anomalies POST Detect anomalies Yes (Auditor)
/api/audit/generate-report POST Generate audit report Yes (Auditor)
/api/reconcile/transactions POST Reconcile transactions Yes
/api/health GET Health check No

Full API documentation available at http://localhost:8000/docs when server is running.

πŸ”’ Security Best Practices

For Production Deployment

  1. Environment Security

    • Use strong, randomly generated keys
    • Store secrets in secure vault (e.g., AWS Secrets Manager, HashiCorp Vault)
    • Enable HTTPS with valid SSL certificates
    • Use PostgreSQL instead of SQLite
  2. Access Control

    • Implement multi-factor authentication (MFA)
    • Use role-based access control (RBAC)
    • Regularly rotate API keys and tokens
    • Monitor failed authentication attempts
  3. Data Protection

    • Encrypt data at rest and in transit
    • Implement data retention policies
    • Regular security audits
    • Secure file upload validation
  4. Network Security

    • Use firewall rules to restrict access
    • Implement rate limiting
    • Enable CORS only for trusted domains
    • Use VPN or private network for sensitive operations
  5. Audit & Compliance

    • Enable comprehensive audit logging
    • Regular review of audit logs
    • Maintain logs for required retention period
    • Implement automated alerting for suspicious activities

Recommended Tools & Libraries

Security & Encryption

  • cryptography: Industry-standard encryption library
  • python-jose: JWT implementation
  • passlib: Password hashing with bcrypt

AI/ML for Finance

  • scikit-learn: Machine learning algorithms
  • pandas: Data manipulation and analysis
  • numpy: Numerical computing

API & Web

  • FastAPI: Modern, fast web framework
  • uvicorn: ASGI server
  • slowapi: Rate limiting

Audit & Logging

  • python-json-logger: Structured logging
  • SQLAlchemy: Database ORM with security features

Data Validation

  • pydantic: Data validation using Python type annotations
  • cerberus: Lightweight data validation

πŸ—οΈ Architecture

vigilant-octo-engine/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ api.py                      # REST API with security
β”‚   β”œβ”€β”€ security.py                 # Encryption, access control
β”‚   β”œβ”€β”€ audit_logging.py            # Audit trail management
β”‚   β”œβ”€β”€ invoice_processing.py       # AI invoice automation
β”‚   β”œβ”€β”€ expense_categorization.py   # ML expense categorization
β”‚   └── anomaly_detection.py        # Fraud detection
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ test_security.py
β”‚   β”œβ”€β”€ test_invoice_processing.py
β”‚   β”œβ”€β”€ test_expense_categorization.py
β”‚   └── test_anomaly_detection.py
β”œβ”€β”€ logs/                           # Audit logs
β”œβ”€β”€ models/                         # AI models cache
β”œβ”€β”€ secure_data/                    # Encrypted data storage
β”œβ”€β”€ requirements.txt                # Dependencies
β”œβ”€β”€ .env.example                    # Configuration template
└── README.md

πŸ–₯️ Frontend (React + Vite)

An accompanying frontend lives in frontend/ for interactive use of the secured API.

Development Setup

cd frontend
npm install
npm run dev

Served at http://localhost:5173 with proxying of /api/* to the backend (configured in vite.config.ts).

Key Frontend Files

  • frontend/package.json – scripts (dev, build, test) and dependencies.
  • frontend/src/types.ts – Typed interfaces mirroring backend responses.
  • frontend/src/apiClient.ts – Thin fetch wrapper; always sends Content-Type: application/json and attaches JWT via Authorization header.
  • frontend/src/AuthContext.tsx – In‑memory auth state (token not persisted to localStorage for security).
  • frontend/src/ProtectedRoute.tsx – Redirects unauthenticated users to /login.
  • Pages: Invoice, Expense, Anomaly, Audit, Dashboard, Login under frontend/src/pages/.
  • Components: Reusable UI in frontend/src/components/ (Navbar, LoadingSpinner, ErrorBoundary).
  • Hooks: frontend/src/hooks/ (useAuth, useApi, usePolling) abstract auth & polling logic.
  • Services: Thin domain wrappers in frontend/src/services/ (e.g. invoiceService.ts).
  • Utils: Formatting helpers in frontend/src/utils/ (formatCurrency, parseDate).
  • Configuration: ESLint (.eslintrc.cjs), Prettier (.prettierrc), EditorConfig (.editorconfig) and env files (.env.development, .env.production).

Security Considerations

  • Tokens are kept only in React state (avoid XSS/localStorage persistence).
  • CORS updated to allow http://localhost:5173 for development only (see src/api.py).
  • Do not add arbitrary originsβ€”review before deployment.

Testing (Frontend)

Vitest + Testing Library for component and client tests:

npm run test

Example tests in frontend/src/__tests__/ validate API client request structure and protected routing. Additional test environment uses jsdom (configured in vite.config.ts).

Building

npm run build

Outputs production assets to frontend/dist/ (serve behind HTTPS; ensure secure headers).

Linting & Formatting

npm run lint     # ESLint (zero warnings policy for CI)
npm run type-check  # TypeScript compile check without emit
npm run format   # Prettier format all changed files

Environment Variables

Frontend uses Vite prefixed vars:

VITE_API_BASE_URL=http://localhost:8000/api   # dev
VITE_API_BASE_URL=/api                        # production (reverse proxy)
VITE_APP_ENV=development|production

Never expose secretsβ€”only non-sensitive config belongs in Vite prefixed variables.

Recommended Hardening (Production)

  • Enable HTTPS & HSTS at reverse proxy layer.
  • Add Content Security Policy (CSP) disallowing inline scripts; move any inline styles to CSS.
  • Use Subresource Integrity (SRI) for third‑party scripts (if any).
  • Prefer ephemeral memory token storage (already implemented) and short JWT lifetimes with silent refresh.
  • Implement backend rate limiting (already via slowapi) and enforce per‑origin CORS.

SharePoint / M365 Integration (Optional Roadmap)

If embedding in SharePoint, wrap built assets in SPFx web part or host as Teams tab:

  • Acquire Azure AD token via MSAL and pass through to backend.
  • Use Graph for user profile enrichment while keeping financial data strictly backend-bound.
  • Store only minimal invoice metadata in SharePoint Lists; keep sensitive payloads encrypted server-side.

Folder Summary

frontend/
β”œβ”€β”€ .eslintrc.cjs
β”œβ”€β”€ .prettierrc
β”œβ”€β”€ .editorconfig
β”œβ”€β”€ .env.development
β”œβ”€β”€ .env.production
β”œβ”€β”€ package.json
β”œβ”€β”€ vite.config.ts
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ apiClient.ts
β”‚   β”œβ”€β”€ AuthContext.tsx
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ Navbar.tsx
β”‚   β”‚   β”œβ”€β”€ LoadingSpinner.tsx
β”‚   β”‚   └── ErrorBoundary.tsx
β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”œβ”€β”€ useAuth.ts
β”‚   β”‚   β”œβ”€β”€ useApi.ts
β”‚   β”‚   └── usePolling.ts
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ invoiceService.ts
β”‚   β”‚   β”œβ”€β”€ expenseService.ts
β”‚   β”‚   β”œβ”€β”€ anomalyService.ts
β”‚   β”‚   β”œβ”€β”€ reconciliationService.ts
β”‚   β”‚   └── auditService.ts
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”œβ”€β”€ formatCurrency.ts
β”‚   β”‚   └── parseDate.ts
β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”œβ”€β”€ Invoice.tsx
β”‚   β”‚   β”œβ”€β”€ Expense.tsx
β”‚   β”‚   β”œβ”€β”€ Anomaly.tsx
β”‚   β”‚   β”œβ”€β”€ Audit.tsx
β”‚   β”‚   β”œβ”€β”€ Dashboard.tsx
β”‚   β”‚   └── Login.tsx
β”‚   β”œβ”€β”€ __tests__/
β”‚   β”‚   β”œβ”€β”€ apiClient.test.ts
β”‚   β”‚   β”œβ”€β”€ ProtectedRoute.test.tsx
β”‚   β”‚   └── services.test.ts
β”‚   β”œβ”€β”€ types.ts
β”‚   β”œβ”€β”€ ProtectedRoute.tsx
β”‚   β”œβ”€β”€ setupTests.ts
β”‚   β”œβ”€β”€ App.tsx
β”‚   └── main.tsx
└── index.html

Extending

  • Add new API endpoint: implement backend route, then create typed wrapper in apiClient.ts and interface in types.ts.
  • Keep mappings 1:1 with backend response fields; prefer explicit interfaces over any.

πŸ§ͺ Testing

Run all tests:

pytest tests/ -v

Run specific test file:

pytest tests/test_security.py -v

Run with coverage:

pytest tests/ --cov=src --cov-report=html

πŸ“Š Use Cases

1. Automated Invoice Processing

  • Extract data from PDF/image invoices
  • Validate invoice information
  • Categorize expenses automatically
  • Suggest GL accounts for posting

2. Expense Management

  • Categorize expenses using AI
  • Identify tax-deductible expenses
  • Detect policy violations
  • Generate spending reports

3. Audit & Compliance

  • Detect duplicate transactions
  • Identify unusual patterns
  • Benford's Law analysis for fraud detection
  • Comprehensive audit trail

4. Financial Reconciliation

  • Match bank transactions with books
  • Identify discrepancies
  • Automated reconciliation suggestions
  • Exception reporting

🀝 Contributing

We welcome contributions! Please follow these guidelines:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

⚠️ Disclaimer

This software is provided as-is for CPA firms to automate financial processes. Users are responsible for:

  • Ensuring compliance with applicable regulations
  • Implementing appropriate security measures
  • Regular security audits and updates
  • Data backup and disaster recovery
  • Consulting with legal and compliance teams

πŸ“ž Support

For questions, issues, or feature requests:

πŸ”„ Roadmap

  • Integration with QuickBooks/Xero
  • OCR for invoice scanning
  • Advanced ML models (deep learning)
  • Mobile app for expense submission
  • Real-time anomaly alerts
  • Multi-currency support
  • Blockchain audit trail
  • Automated tax form generation

Built with ❀️ for CPA firms seeking to automate and secure their financial operations.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published