Metadata-Version: 2.4
Name: mushroom-magic-campus-auth
Version: 0.1.0
Summary: Magic Campus auth provider for MushroomAgent server auth.
Project-URL: Homepage, https://github.com/kiwilightyear/mushroom-magic-campus-auth
Project-URL: Repository, https://github.com/kiwilightyear/mushroom-magic-campus-auth
Project-URL: Issues, https://github.com/kiwilightyear/mushroom-magic-campus-auth/issues
Author: Kiwilightyear
License-Expression: MIT
License-File: LICENSE
Keywords: auth-provider,authentication,magic-campus,mushroom-agent
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Security
Requires-Python: >=3.11
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.8; extra == 'dev'
Requires-Dist: twine>=5.1; extra == 'dev'
Description-Content-Type: text/markdown

# mushroom-magic-campus-auth

Magic Campus authentication provider for MushroomAgent server auth.

It validates an incoming token by calling Magic Campus:

```text
GET /api/v1/accounts/getUserInfo
Authorization: Bearer <AccessToken>
```

Expected response:

```json
{
  "code": 200,
  "msg": "ok",
  "data": {
    "userId": 123,
    "nickname": "Alice",
    "avatarUrl": "https://example.test/avatar.png",
    "phone": "13800000000",
    "email": "alice@example.test"
  }
}
```

## Install For Local Development

From this directory:

```bash
pip install -e .
```

Or install it into the same Python environment that runs `mushroom-agent`.

After publishing to PyPI:

```bash
pip install mushroom-magic-campus-auth
```

## MushroomAgent Config

Add this provider to `~/.mushroom_agent/config.yaml`:

```yaml
server:
  auth:
    providers:
      - type: import
        path: mushroom_magic_campus_auth:create_provider
        config:
          base_url: "https://magic-campus.example.com"
          timeout_seconds: 3

      # Optional fallback to existing MushroomAgent accesskeys.
      - type: accesskey
```

If you do not include `type: accesskey`, only Magic Campus tokens will be accepted.

## Token Sources

MushroomAgent extracts token candidates from:

- `Authorization: Bearer <token>`
- query parameters: `accesskey`, `token`, `auth_token`
- cookie: `mushroom_server_auth`

This means the provider works for:

- HTTP API
- WebUI login
- `/ws/v1/chat`
- `/ws/v1/node`

## Config Options

```yaml
config:
  base_url: "https://magic-campus.example.com"       # required
  user_info_path: "/api/v1/accounts/getUserInfo"     # optional
  timeout_seconds: 3                                 # optional
  expected_code: 200                                 # optional
  issue_cookie: false                                # optional
  headers:                                           # optional extra headers
    X-App-Id: "mushroom-agent"
```

`issue_cookie: true` asks MushroomAgent to set the accepted token as the auth cookie after WebUI login.

## Try With curl

```bash
curl -X POST "http://127.0.0.1:7860/i/chat/api/messages" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <MagicCampusAccessToken>" \
  -d '{"text":"hello"}'
```

## Development

```bash
pip install -e ".[dev]"
pytest
```

## Build And Publish To PyPI

Install build tools:

```bash
pip install -e ".[dev]"
```

Build source and wheel distributions:

```bash
python -m build
```

Validate package metadata:

```bash
twine check dist/*
```

Upload to TestPyPI first:

```bash
twine upload --repository testpypi dist/*
```

Upload to PyPI:

```bash
twine upload dist/*
```

Use a PyPI API token when prompted for the password. The username is usually `__token__`.
