semvec/pypi-packages huggingface dataset:
ninja-keys 1.0.1
API Keys for Django Ninja
License: MIT
Requires Python: >=3.9
Keywords: api, authentication, authorization, django, django-ninja, ninja, security

# ninja-keys

[![PyPI version](https://badge.fury.io/py/ninja-keys.svg)](https://badge.fury.io/py/ninja-keys)
[![PyPI Supported Python Versions](https://img.shields.io/pypi/pyversions/ninja-keys.svg)](https://pypi.python.org/pypi/ninja-keys/)
[![tests](https://github.com/feliperalmeida/ninja-keys/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/feliperalmeida/ninja-keys/actions/workflows/tests.yml)
[![codecov](https://codecov.io/github/feliperalmeida/ninja-keys/graph/badge.svg?token=F8O6BPUYPH)](https://codecov.io/github/feliperalmeida/ninja-keys)

API Keys for Django Ninja. 🔐

Based on [djangorestframework-api-key](https://github.com/florimondmanca/djangorestframework-api-key).

## Introduction

This package provides a simple and secure way to add API keys to your Django Ninja APIs.

You can easily customize API keys to your needs, such as tying them to specific users or organizations, setting
expiration dates, and more.

## Quickstart

Install the package using pip:

```bash
pip install ninja-keys
```

Add `ninja_keys` to your `INSTALLED_APPS` in your Django project's settings file:

```python
INSTALLED_APPS = [
    ...
    "ninja_keys",
]
```

Run the migrations:

```bash
python manage.py migrate
```

## Usage

### API Key creation

API keys can be created via the Django admin interface (/admin) or programmatically via the `.create_key()` method on
`APIKey` objects. By doing this, you'll have only one-time access to the key. Once it's hashed and stored, you cannot
access the raw key anymore.

```python
from ninja_keys.models import APIKey

api_key, key = APIKey.objects.create_key(name="my-remote-service")
# Proceed with `api_key` and `key`... The `key` value is the raw key that should be provided to the client.
```

### API Key authentication

API keys can be used to authenticate requests to your API. To do this, you'll need to add the `ApiKeyAuth` class to
your API's `auth` attribute. It can be added globally or per view/router.

```python
from ninja_keys.auth import ApiKeyAuth

# global
api = NinjaAPI(auth=ApiKeyAuth())
```

```python
from ninja_keys.auth import ApiKeyAuth


# Per view/router
@api.get("/protected", auth=ApiKeyAuth())
def protected(request):
    return "Hello, protected!"
```

#### Requests with API keys

To make requests with an API key, you'll need to send the key in the `X-API-Key` header.

```bash
# Using curl
curl -X GET -H "X-API-Key: my-api-key" http://localhost:8000/api/protected

# Using HTTPie
http http://localhost:8000/api/protected X-API-Key:"my-api-key"
```

By default, the API key is required to be sent in the `X-API-Key` header. You can change this behavior by overriding
the `param_name` attribute on the `ApiKeyAuth` class.

```python
class MyApiKeyAuth(ApiKeyAuth):
    param_name = "Api-Key"
```
vcrypto 4.2.1
Utility to easily store passwords/secrets
License: MIT
Requires Python: >=3.8

# **vcrypto - Secure Secrets Management in Python**
vcrypto is a simple and effective utility for securely storing passwords and secrets in Python. It leverages **Fernet encryption** from the [cryptography](https://cryptography.io/en/latest/) module instead of reinventing the wheel.

## **🔒 Why vcrypto?**
Managing secrets in Python, especially in collaborative projects, can be cumbersome. Our approach simplifies it:

👉 **One master private password** (ignored in Git)\
👉 **A structured dictionary file with encrypted secrets**\
👉 **Easily shareable** - only the master password needs to be exchanged once\
👉 **Version-controlled secrets** - since only encrypted values are stored

This package makes it easy to **store and retrieve encrypted secrets** in **JSON** or **YAML** formats.

---

## **📌 Features**
- 🔐 **AES-128 Encryption** (Fernet) for secure storage.
- 📂 **JSON/YAML Support** for structured secret storage.
- 🛠️ **Environment Variable Support** for flexible key management.
- 🔑 **Simple API** for saving and retrieving secrets.

---

## **📝 Installation**
Install via **pip**:
```sh
pip install vcrypto
```

---

## **🚀 Quickstart**
### **1️⃣ Initialize vcrypto**
Before using `vcrypto`, initialize it with:
```python
from vcrypto import init_vcrypto

init_vcrypto(secrets_file="secrets.yaml", environ_var_name="MY_ENV_VAR")
```

> [!TIP]
> You only need to initialize it **once**.

This ensures `vcrypto` knows where to store secrets and how to retrieve the master password.

### **2️⃣ Store and Retrieve Secrets**
#### **Save a Secret**
```python
from vcrypto import save_secret

save_secret("SECRET_TOKEN", "super_secret_value")
```

#### **Retrieve a Secret**
```python
from vcrypto import read_secret

token = read_secret("SECRET_TOKEN")
print(token)  # Output: super_secret_value
```

---

## **⚙️ Customization**
The `init_vcrypto` function allows customization:

| Parameter                 | Description |
|---------------------------|-------------|
| `secrets_file`            | Path to the encrypted secrets file (JSON/YAML) |
| `filename_master_password`| Path to the master password file (optional, defaults to `master.password`) |
| `environ_var_name`        | Name of an environment variable to retrieve the master password |

**Example:**
```python
init_vcrypto(secrets_file="config/secrets.yaml", filename_master_password="config/master.secret")
```
This setup stores **both** the `master.password` and `secrets.yaml` in the `config/` folder.

---

## **🛠️ Development**
This project uses [`uv`](https://github.com/astral-sh/uv) for dependency management.

### **Install dependencies**
```sh
pip install uv
uv venv
uv install
```

---

## **💜 License**
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).

---

## **👤 Author**
- **[Arnau Villoro](https://villoro.com)**

---