auth.py

Overview

The auth.py file provides custom authentication classes designed to be used with the Python requests library. These classes facilitate the inclusion of authorization tokens in HTTP request headers, enabling secure interaction with different APIs in the InfiniFlow ecosystem.

Specifically, this file defines two authentication handlers:

Both classes inherit from requests.auth.AuthBase, making them compatible with requests's authentication mechanism.

Classes

RAGFlowHttpApiAuth

Description

This class attaches a Bearer token to the Authorization header of outgoing HTTP requests. The token is prefixed with the keyword "Bearer " as per OAuth 2.0 bearer token usage conventions.

Constructor

def __init__(self, token: str):
auth = RAGFlowHttpApiAuth(token="your_bearer_token_here")
response = requests.get("https://api.example.com/data", auth=auth)

Methods

def __call__(self, r: requests.PreparedRequest) -> requests.PreparedRequest:

RAGFlowWebApiAuth

Description

This class attaches a raw token string directly to the Authorization header without any prefix. It is intended for web APIs that expect the token as-is in the header.

Constructor

def __init__(self, token: str):
auth = RAGFlowWebApiAuth(token="raw_token_string_here")
response = requests.get("https://webapi.example.com/resource", auth=auth)

Methods

def __call__(self, r: requests.PreparedRequest) -> requests.PreparedRequest:

Implementation Details


Interaction With Other System Components


Example Usage

import requests
from auth import RAGFlowHttpApiAuth, RAGFlowWebApiAuth

# Using RAGFlowHttpApiAuth for a REST API
http_auth = RAGFlowHttpApiAuth(token="abc123bearertoken")
response = requests.get("https://api.infiflow.com/v1/data", auth=http_auth)
print(response.status_code, response.json())

# Using RAGFlowWebApiAuth for a Web API
web_auth = RAGFlowWebApiAuth(token="rawtoken123")
response = requests.get("https://web.infiflow.com/resource", auth=web_auth)
print(response.status_code, response.content)

Mermaid Class Diagram

classDiagram
    class RAGFlowHttpApiAuth {
        -_token: str
        +__init__(token: str)
        +__call__(r: PreparedRequest) PreparedRequest
    }
    class RAGFlowWebApiAuth {
        -_token: str
        +__init__(token: str)
        +__call__(r: PreparedRequest) PreparedRequest
    }
    RAGFlowHttpApiAuth --|> AuthBase
    RAGFlowWebApiAuth --|> AuthBase

Summary

The auth.py file encapsulates two streamlined authentication handlers for the InfiniFlow ecosystem that integrate seamlessly with requests. By abstracting token handling into dedicated classes, it improves code maintainability, security, and clarity in API interactions. The distinction between Bearer and raw token formats ensures compatibility with diverse API authentication schemes.