Metadata-Version: 2.4
Name: verify-username-ad
Version: 0.1.0
Summary: Validate usernames against Active Directory (OSN Enterprise Users, osn.wa.gov)
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: pywin32; sys_platform == "win32"
Requires-Dist: ldap3>=2.9
Provides-Extra: dev
Requires-Dist: python-dotenv; extra == "dev"
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pyinstaller; extra == "dev"

# Verify Username (Active Directory)

Check if a username exists in **Active Directory** under **OSN Enterprise Users** (domain **osn.wa.gov**). The tool can use Windows ADSI (when run on a domain-joined PC) or LDAP with a configurable server and credentials.

## Quick start on the target machine (domain-joined Windows)

1. **Option A – Transfer a zip (no packaging)**  
   Copy the project folder (or a zip of it) to the target machine. Then:

   ```powershell
   cd verify_username_ad
   python -m venv .venv
   .venv\Scripts\Activate.ps1
   pip install -r requirements.txt
   pip install -e .
   verify-username-ad jdoe
   ```

   Or without installing the package:

   ```powershell
   pip install -r requirements.txt
   python run_check.py jdoe
   ```

2. **Option B – Single executable**  
   On a Windows machine (can be your dev machine), build the exe:

   ```powershell
   pip install pyinstaller
   python build_exe.py
   ```

   Copy `dist\verify_username_ad.exe` to the target machine and run:

   ```powershell
   .\verify_username_ad.exe jdoe
   ```

## Usage

```text
# Check one or more usernames
verify-username-ad jdoe
verify-username-ad jdoe asmith testuser

# From stdin (one username per line)
echo jdoe | verify-username-ad
type usernames.txt | verify-username-ad

# Quiet: only print invalid usernames to stderr; exit 1 if any invalid
verify-username-ad -q jdoe invalid_user

# Verbose: show error reason when invalid
verify-username-ad -v jdoe
```

## Configuration

- **ADSI (default on Windows)**  
  No config. Uses the logged-in user’s context. Run on a **domain-joined** Windows machine.

- **LDAP (any machine that can reach the DC)**  
  Set environment variables (or use `--use-ldap` and `--server`):

  - `AD_SERVER_URI` – e.g. `ldap://osn.wa.gov` or `ldap://dc01.osn.wa.gov`
  - `AD_BASE_DN` – default: `OU=OSN Enterprise Users,DC=osn,DC=wa,DC=gov`
  - `AD_BIND_USER` – e.g. `user@osn.wa.gov` or `osn\user`
  - `AD_BIND_PASSWORD` – password for bind user

  Example:

  ```powershell
  $env:AD_SERVER_URI = "ldap://osn.wa.gov"
  $env:AD_BIND_USER = "svc_reader@osn.wa.gov"
  $env:AD_BIND_PASSWORD = "***"
  verify-username-ad jdoe
  ```

## Testing without AD (mock mode)

You can test the flow **without** a domain or AD access:

1. **Mock with built-in list** (valid: `jdoe`, `asmith`, `testuser`):

   ```powershell
   $env:VERIFY_USERNAME_MOCK = "1"
   python run_check.py jdoe
   python run_check.py unknown_user
   ```

2. **Mock with a file** (one username per line = “valid”):

   ```powershell
   $env:VERIFY_USERNAME_MOCK = "1"
   $env:VERIFY_USERNAME_MOCK_FILE = "C:\path\to\valid_usernames.txt"
   python run_check.py jdoe
   ```

3. **CLI flag** (same as env):

   ```powershell
   python run_check.py --mock jdoe
   ```

## Easier ways to test without copying files

- **Shared folder / network drive**  
  Put the project on a share; on the target machine run from that path (e.g. `python run_check.py jdoe`). No need to “package” or copy manually each time.

- **Remote execution**  
  If you have PowerShell Remoting or SSH to the target PC, run the script there (e.g. `Invoke-Command -ComputerName TargetPC -ScriptBlock { ... }`). You still need the code on that machine (e.g. via share or one-time copy).

- **Mock mode on your machine**  
  Use `VERIFY_USERNAME_MOCK=1` and optionally `VERIFY_USERNAME_MOCK_FILE` to test logic and CLI locally; only run real AD checks on the domain-joined machine.

## Project layout

```text
verify_username_ad/
  src/verify_username_ad/
    ad_check.py    # ADSI + ldap3
    mock_check.py  # Mock for testing
    config.py      # Env/config
    cli.py         # CLI entry
  run_check.py     # Run without installing package
  requirements.txt
  build_exe.py     # PyInstaller build script
  README.md
```

## Requirements

- Python 3.9+
- Windows + domain-joined (for ADSI), or any OS with network access to the DC (for LDAP)
- `pywin32` (Windows, for ADSI), `ldap3` (for LDAP)
