Metadata-Version: 2.4
Name: rnet-oauth
Version: 1.0.3
Summary: RNet OAuth Python client library
Author: rNet Ai
License-Expression: MIT
Project-URL: Homepage, https://rnetai.org
Project-URL: Repository, https://github.com/rNetAi/rnet-oauth-python
Project-URL: Issues, https://github.com/rNetAi/rnet-oauth-python/issues
Keywords: oauth2,rnet,authentication
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Operating System :: OS Independent
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Dynamic: license-file

# RNet OAuth Python Library

A Python backend library for integrating **RNet OAuth** and **AI Provider** services. This library allows users to authenticate via RNet and pay for AI model token costs directly using their RNet account.

## Features

- **OAuth2 PKCE Support**: Secure authorization code flow with automatic code verifier and challenge generation.
- **Token Management**: Exchange authorization codes for tokens and refresh expired tokens.
- **UserInfo Endpoint**: Fetch the authenticated user's RNet profile with an access token.
- **AI Integration**: Easy methods to chat with AI models using standard or streaming responses.

## Installation

```bash
pip install rnet-oauth
```

## Quick Start

### 1. Initialize the Clients
```python
from rnet_oauth import RNetOAuthClient, ModelClient

auth = RNetOAuthClient(
    client_id='client-id',
    client_secret='client-secret',
    redirect_uri='redirect-uri'
)
ai = ModelClient("gemini-2.5-flash-lite")
```

### 2. Generate Authorization URL (OAuth2 PKCE)
```python
# 1. Generate PKCE
pkce = auth.generate_pkce()
verifier = pkce['verifier']
challenge = pkce['challenge']

# 2. Get Authorization URL
# challenge: PKCE code challenge (optional)
# state: An optional string to maintain state between the request and callback (recommended for security)
auth_url = auth.get_authorization_url(challenge, state='optional-state')
```

### 3. Exchange Code for Tokens
```python
# 3. Exchange code for tokens
tokens = auth.exchange_code_for_token(code, verifier)
access_token = tokens['access_token']
```

### 4. Get User Info
```python
user_info = auth.get_user_info(access_token)
print(user_info['email'])
print(user_info['name'])
```

The UserInfo response comes from RNet's `/userinfo` endpoint and may include:
`sub`, `email`, `email_verified`, `name`, `preferred_username`, `user_id`, `role`, and `status`.

### 5. Chat with AI
```python
response = ai.chat({
    "contents": [
        {
            "role": "user",
            "parts": [{"text": "Hello!"}]
        }
    ]
}, access_token)
```

### 6. Streaming AI Response
```python
for chunk in ai.chat_stream({
    "contents": [
        {
            "role": "user",
            "parts": [{"text": "Hello!"}]
        }
    ]
}, access_token):
    print(chunk)
```

## License
MIT
