apikee 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
apikee-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,114 @@
1
+ Metadata-Version: 2.1
2
+ Name: apikee
3
+ Version: 1.0.0
4
+ Summary: ApiKee - A lightweight API key validation library for FastAPI
5
+ Home-page: https://github.com/apikee-dev/apikee-python
6
+ Author: usmhic
7
+ License: MIT
8
+ Requires-Python: >=3.7
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: fastapi>=0.68.0
11
+ Requires-Dist: aiohttp>=3.8.0
12
+
13
+ # ApiKee Python
14
+
15
+ ApiKee is a lightweight and easy-to-use library for managing and securing your FastAPI endpoints. Whether you want to validate API keys locally or via a server, ApiKee ensures smooth integration with minimal configuration.
16
+
17
+ ---
18
+
19
+ ## **Table of Contents**
20
+
21
+ 1. [Concept Overview](#concept-overview)
22
+ 2. [Installation](#installation)
23
+ 3. [Usage](#usage)
24
+ - [Local Validation](#1-local-validation)
25
+ - [Server Validation](#2-server-validation)
26
+ 4. [Server Validation Options](#server-validation-options)
27
+ - [Build Your Own Server](#1-build-your-own-server)
28
+ - [Use ApiKee Premium Server](#2-use-apikee-premium-server)
29
+ 5. [Contributing](#contributing)
30
+ 6. [License](#license)
31
+
32
+ ---
33
+
34
+ ## **Concept Overview**
35
+
36
+ ApiKee provides two modes of operation:
37
+ 1. **Local Validation**: Validate API keys locally by comparing them to a predefined key.
38
+ 2. **Server Validation**: Send API keys to a server for validation and handle responses.
39
+
40
+ ---
41
+
42
+ ## **Installation**
43
+
44
+ Install ApiKee using pip:
45
+ ```bash
46
+ pip install apikee
47
+ ```
48
+
49
+ ---
50
+
51
+ ## **Usage**
52
+
53
+ ### **1. Local Validation**
54
+ ```python
55
+ from fastapi import FastAPI, Request
56
+ from apikee.config import config
57
+ from apikee.decorators import apikey
58
+
59
+ app = FastAPI()
60
+
61
+ # Set your local API key
62
+ config.local_key = "my-local-api-key"
63
+
64
+ @app.get("/local-protected")
65
+ @apikey()
66
+ async def local_protected_endpoint(request: Request):
67
+ return {"message": "Local validation successful"}
68
+ ```
69
+
70
+ ### **2. Server Validation**
71
+ ```python
72
+ from fastapi import FastAPI, Request
73
+ from apikee.config import config
74
+ from apikee.decorators import apikey
75
+
76
+ app = FastAPI()
77
+
78
+ # Configure server validation
79
+ config.server_key = "my-server-key"
80
+ config.server_url = "https://premium.apikee.com"
81
+ config.project_id = "my-project-id"
82
+ config.environment = "staging"
83
+
84
+ @app.get("/server-protected")
85
+ @apikey(endpoint_id="endpoint-id")
86
+ async def server_protected_endpoint(request: Request):
87
+ return {"message": "Server validation successful"}
88
+ ```
89
+
90
+ ---
91
+
92
+ ## **Server Validation Options**
93
+
94
+ ### **1. Build Your Own Server**
95
+ You can create your own validation server by following the specifications provided in the [ApiKee API Documentation](https://api.yourdocumentation.com).
96
+
97
+ ### **2. Use ApiKee Premium Server**
98
+ Use the pre-built ApiKee server available at [apikee.com](https://apikee.com) to handle validation, analytics, and centralized key management.
99
+
100
+ ---
101
+
102
+ ## **Contributing**
103
+
104
+ Contributions are welcome! Follow the standard fork-branch-PR model to contribute. For detailed instructions, check the [GitHub repository](https://github.com/your-repo-link).
105
+
106
+ ---
107
+
108
+ ## **License**
109
+
110
+ This library is open-source and released under the MIT License.
111
+ - **Note**: The open-source license applies only to the library.
112
+ - For server validation, you must either:
113
+ - Build your own server respecting the ApiKee specifications.
114
+ - Use the paid ApiKee server at [apikee.com](https://apikee.com).
apikee-1.0.0/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # ApiKee Python
2
+
3
+ ApiKee is a lightweight and easy-to-use library for managing and securing your FastAPI endpoints. Whether you want to validate API keys locally or via a server, ApiKee ensures smooth integration with minimal configuration.
4
+
5
+ ---
6
+
7
+ ## **Table of Contents**
8
+
9
+ 1. [Concept Overview](#concept-overview)
10
+ 2. [Installation](#installation)
11
+ 3. [Usage](#usage)
12
+ - [Local Validation](#1-local-validation)
13
+ - [Server Validation](#2-server-validation)
14
+ 4. [Server Validation Options](#server-validation-options)
15
+ - [Build Your Own Server](#1-build-your-own-server)
16
+ - [Use ApiKee Premium Server](#2-use-apikee-premium-server)
17
+ 5. [Contributing](#contributing)
18
+ 6. [License](#license)
19
+
20
+ ---
21
+
22
+ ## **Concept Overview**
23
+
24
+ ApiKee provides two modes of operation:
25
+ 1. **Local Validation**: Validate API keys locally by comparing them to a predefined key.
26
+ 2. **Server Validation**: Send API keys to a server for validation and handle responses.
27
+
28
+ ---
29
+
30
+ ## **Installation**
31
+
32
+ Install ApiKee using pip:
33
+ ```bash
34
+ pip install apikee
35
+ ```
36
+
37
+ ---
38
+
39
+ ## **Usage**
40
+
41
+ ### **1. Local Validation**
42
+ ```python
43
+ from fastapi import FastAPI, Request
44
+ from apikee.config import config
45
+ from apikee.decorators import apikey
46
+
47
+ app = FastAPI()
48
+
49
+ # Set your local API key
50
+ config.local_key = "my-local-api-key"
51
+
52
+ @app.get("/local-protected")
53
+ @apikey()
54
+ async def local_protected_endpoint(request: Request):
55
+ return {"message": "Local validation successful"}
56
+ ```
57
+
58
+ ### **2. Server Validation**
59
+ ```python
60
+ from fastapi import FastAPI, Request
61
+ from apikee.config import config
62
+ from apikee.decorators import apikey
63
+
64
+ app = FastAPI()
65
+
66
+ # Configure server validation
67
+ config.server_key = "my-server-key"
68
+ config.server_url = "https://premium.apikee.com"
69
+ config.project_id = "my-project-id"
70
+ config.environment = "staging"
71
+
72
+ @app.get("/server-protected")
73
+ @apikey(endpoint_id="endpoint-id")
74
+ async def server_protected_endpoint(request: Request):
75
+ return {"message": "Server validation successful"}
76
+ ```
77
+
78
+ ---
79
+
80
+ ## **Server Validation Options**
81
+
82
+ ### **1. Build Your Own Server**
83
+ You can create your own validation server by following the specifications provided in the [ApiKee API Documentation](https://api.yourdocumentation.com).
84
+
85
+ ### **2. Use ApiKee Premium Server**
86
+ Use the pre-built ApiKee server available at [apikee.com](https://apikee.com) to handle validation, analytics, and centralized key management.
87
+
88
+ ---
89
+
90
+ ## **Contributing**
91
+
92
+ Contributions are welcome! Follow the standard fork-branch-PR model to contribute. For detailed instructions, check the [GitHub repository](https://github.com/your-repo-link).
93
+
94
+ ---
95
+
96
+ ## **License**
97
+
98
+ This library is open-source and released under the MIT License.
99
+ - **Note**: The open-source license applies only to the library.
100
+ - For server validation, you must either:
101
+ - Build your own server respecting the ApiKee specifications.
102
+ - Use the paid ApiKee server at [apikee.com](https://apikee.com).
@@ -0,0 +1,10 @@
1
+ from .config import config
2
+ from .decorators import apikey
3
+ from .middleware import SwaggerProtectedMiddleware
4
+
5
+ def init_app(app):
6
+ """
7
+ Initialize the FastAPI application with ApiKee configurations.
8
+ This automatically adds middleware to protect Swagger endpoints.
9
+ """
10
+ app.add_middleware(SwaggerProtectedMiddleware)
@@ -0,0 +1,13 @@
1
+ from typing import Optional
2
+
3
+ class ApiKeeConfig:
4
+ def __init__(self, local_key: Optional[str] = None, server_key: Optional[str] = None,
5
+ server_url: Optional[str] = None, project_id: Optional[str] = None, environment: Optional[str] = None):
6
+ self.local_key = local_key
7
+ self.server_key = server_key
8
+ self.server_url = server_url
9
+ self.project_id = project_id
10
+ self.environment = environment
11
+
12
+ # Global configuration instance
13
+ config = ApiKeeConfig()
@@ -0,0 +1,29 @@
1
+ from fastapi import HTTPException, Request
2
+ from functools import wraps
3
+ from .config import config
4
+ from .server_client import validate_with_server
5
+
6
+ def apikey(endpoint_id: str = None):
7
+ """
8
+ Decorator for API key validation.
9
+ - If `endpoint_id` is provided, server validation is used.
10
+ - If `endpoint_id` is not provided, local validation is used.
11
+ """
12
+ def decorator(func):
13
+ @wraps(func)
14
+ async def wrapper(request: Request, *args, **kwargs):
15
+ api_key = request.headers.get("X-Api-Key")
16
+ if not api_key:
17
+ raise HTTPException(status_code=403, detail="API key missing")
18
+
19
+ if endpoint_id: # Server validation
20
+ is_valid, message = await validate_with_server(api_key, endpoint_id)
21
+ if not is_valid:
22
+ raise HTTPException(status_code=403, detail=message)
23
+ else: # Local validation
24
+ if not config.local_key or api_key != config.local_key:
25
+ raise HTTPException(status_code=403, detail="Invalid API key")
26
+
27
+ return await func(request, *args, **kwargs)
28
+ return wrapper
29
+ return decorator
@@ -0,0 +1,2 @@
1
+ class ApiKeeError(Exception):
2
+ pass
@@ -0,0 +1,18 @@
1
+ from fastapi import HTTPException, Request
2
+ from starlette.middleware.base import BaseHTTPMiddleware
3
+ from .config import config
4
+
5
+ class SwaggerProtectedMiddleware(BaseHTTPMiddleware):
6
+ """
7
+ Middleware to protect Swagger and Redoc documentation endpoints.
8
+ """
9
+
10
+ def __init__(self, app):
11
+ super().__init__(app)
12
+
13
+ async def dispatch(self, request: Request, call_next):
14
+ if request.url.path in ["/docs", "/redoc", "/openapi.json"]:
15
+ api_key = request.headers.get("X-Api-Key")
16
+ if not api_key or api_key != config.local_key:
17
+ raise HTTPException(status_code=403, detail="Access to documentation is restricted")
18
+ return await call_next(request)
@@ -0,0 +1,22 @@
1
+ import aiohttp
2
+ from .config import config
3
+
4
+ async def validate_with_server(api_key: str, endpoint_id: str) -> (bool, str):
5
+ if not config.server_url or not config.server_key:
6
+ return False, "Server configuration is incomplete"
7
+
8
+ payload = {
9
+ "api_key": api_key,
10
+ "endpoint_id": endpoint_id,
11
+ "project_id": config.project_id,
12
+ "environment": config.environment,
13
+ }
14
+
15
+ headers = {"Authorization": f"Bearer {config.server_key}"}
16
+
17
+ async with aiohttp.ClientSession() as session:
18
+ async with session.post(f"{config.server_url}/validate", json=payload, headers=headers) as response:
19
+ if response.status == 200:
20
+ data = await response.json()
21
+ return data.get("isValid", False), data.get("message", "Unknown error")
22
+ return False, f"Server error: {response.status}"
@@ -0,0 +1,114 @@
1
+ Metadata-Version: 2.1
2
+ Name: apikee
3
+ Version: 1.0.0
4
+ Summary: ApiKee - A lightweight API key validation library for FastAPI
5
+ Home-page: https://github.com/apikee-dev/apikee-python
6
+ Author: usmhic
7
+ License: MIT
8
+ Requires-Python: >=3.7
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: fastapi>=0.68.0
11
+ Requires-Dist: aiohttp>=3.8.0
12
+
13
+ # ApiKee Python
14
+
15
+ ApiKee is a lightweight and easy-to-use library for managing and securing your FastAPI endpoints. Whether you want to validate API keys locally or via a server, ApiKee ensures smooth integration with minimal configuration.
16
+
17
+ ---
18
+
19
+ ## **Table of Contents**
20
+
21
+ 1. [Concept Overview](#concept-overview)
22
+ 2. [Installation](#installation)
23
+ 3. [Usage](#usage)
24
+ - [Local Validation](#1-local-validation)
25
+ - [Server Validation](#2-server-validation)
26
+ 4. [Server Validation Options](#server-validation-options)
27
+ - [Build Your Own Server](#1-build-your-own-server)
28
+ - [Use ApiKee Premium Server](#2-use-apikee-premium-server)
29
+ 5. [Contributing](#contributing)
30
+ 6. [License](#license)
31
+
32
+ ---
33
+
34
+ ## **Concept Overview**
35
+
36
+ ApiKee provides two modes of operation:
37
+ 1. **Local Validation**: Validate API keys locally by comparing them to a predefined key.
38
+ 2. **Server Validation**: Send API keys to a server for validation and handle responses.
39
+
40
+ ---
41
+
42
+ ## **Installation**
43
+
44
+ Install ApiKee using pip:
45
+ ```bash
46
+ pip install apikee
47
+ ```
48
+
49
+ ---
50
+
51
+ ## **Usage**
52
+
53
+ ### **1. Local Validation**
54
+ ```python
55
+ from fastapi import FastAPI, Request
56
+ from apikee.config import config
57
+ from apikee.decorators import apikey
58
+
59
+ app = FastAPI()
60
+
61
+ # Set your local API key
62
+ config.local_key = "my-local-api-key"
63
+
64
+ @app.get("/local-protected")
65
+ @apikey()
66
+ async def local_protected_endpoint(request: Request):
67
+ return {"message": "Local validation successful"}
68
+ ```
69
+
70
+ ### **2. Server Validation**
71
+ ```python
72
+ from fastapi import FastAPI, Request
73
+ from apikee.config import config
74
+ from apikee.decorators import apikey
75
+
76
+ app = FastAPI()
77
+
78
+ # Configure server validation
79
+ config.server_key = "my-server-key"
80
+ config.server_url = "https://premium.apikee.com"
81
+ config.project_id = "my-project-id"
82
+ config.environment = "staging"
83
+
84
+ @app.get("/server-protected")
85
+ @apikey(endpoint_id="endpoint-id")
86
+ async def server_protected_endpoint(request: Request):
87
+ return {"message": "Server validation successful"}
88
+ ```
89
+
90
+ ---
91
+
92
+ ## **Server Validation Options**
93
+
94
+ ### **1. Build Your Own Server**
95
+ You can create your own validation server by following the specifications provided in the [ApiKee API Documentation](https://api.yourdocumentation.com).
96
+
97
+ ### **2. Use ApiKee Premium Server**
98
+ Use the pre-built ApiKee server available at [apikee.com](https://apikee.com) to handle validation, analytics, and centralized key management.
99
+
100
+ ---
101
+
102
+ ## **Contributing**
103
+
104
+ Contributions are welcome! Follow the standard fork-branch-PR model to contribute. For detailed instructions, check the [GitHub repository](https://github.com/your-repo-link).
105
+
106
+ ---
107
+
108
+ ## **License**
109
+
110
+ This library is open-source and released under the MIT License.
111
+ - **Note**: The open-source license applies only to the library.
112
+ - For server validation, you must either:
113
+ - Build your own server respecting the ApiKee specifications.
114
+ - Use the paid ApiKee server at [apikee.com](https://apikee.com).
@@ -0,0 +1,13 @@
1
+ README.md
2
+ setup.py
3
+ apikee/__init__.py
4
+ apikee/config.py
5
+ apikee/decorators.py
6
+ apikee/exceptions.py
7
+ apikee/middleware.py
8
+ apikee/server_client.py
9
+ apikee.egg-info/PKG-INFO
10
+ apikee.egg-info/SOURCES.txt
11
+ apikee.egg-info/dependency_links.txt
12
+ apikee.egg-info/requires.txt
13
+ apikee.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ fastapi>=0.68.0
2
+ aiohttp>=3.8.0
@@ -0,0 +1 @@
1
+ apikee
apikee-1.0.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
apikee-1.0.0/setup.py ADDED
@@ -0,0 +1,19 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name="apikee",
5
+ version="1.0.0",
6
+ author="usmhic",
7
+ description="ApiKee - A lightweight API key validation library for FastAPI",
8
+ packages=find_packages(),
9
+ install_requires=[
10
+ "fastapi>=0.68.0",
11
+ "aiohttp>=3.8.0",
12
+ ],
13
+ python_requires=">=3.7",
14
+ include_package_data=True,
15
+ url="https://github.com/apikee-dev/apikee-python",
16
+ license="MIT",
17
+ long_description=open("README.md").read(),
18
+ long_description_content_type="text/markdown",
19
+ )