Metadata-Version: 2.4
Name: rustoken
Version: 0.1.5
Requires-Dist: typer>=0.10.0
Requires-Dist: click>=8.0
Summary: JWT library implemented in Rust for Python
Author-email: Ehsan Amiri <ehsan.amiri.prog@example.com>
Requires-Python: >=3.7
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://ehs22n.github.io/rustoken/
Project-URL: Repository, https://github.com/ehs22n/rustoken
Project-URL: Documentation, https://ehs22n.github.io/rustoken/
Project-URL: Issues, https://github.com/ehs22n/rustoken/issues

# Rustoken

<img src="https://raw.githubusercontent.com/ehs22n/rustoken/refs/heads/main/logo.png" width=100>

**Rustoken** is a `Python library` written in `Rust` that can create and decode JWT tokens.
This is the first version of the library and it currently supports only the `HS256` algorithm.


---

How to install:


```python
pip install rustoken
```

make sure if its installed:

```bash
$ rstoken version
```

help command:

```python
$ rstoken --help
```


How to make SECRET-KEY in Rustoken:
```python
from rustoken import secret_key

secret_key = secret_key() # save it to .env file
print(secret_key)
```

---

How to make SECRET-KEY on CLI:
```bash
$ rstoken secret
```

---

Noraml Usage:

```python
from rustoken import RustToken

jwt = RustToken("MY-SECRET-KEY") # better to use .env file
access = jwt.create_token(new_user.id, 24 * 60 * 60) # 1 day
refresh = jwt.create_token(new_user.id, 24 * 60 * 60 * 7) # 7 day exp
```
---

Usage In Fast-Api:

```python
from rustoken import RustToken

from fastapi import FastAPI, Depends
from sqlalchemy.orm import Session
from . import models, schemas, database

app = FastAPI()


models.Base.metadata.create_all(bind=database.engine)

def get_db():
    db = database.SessionLocal()
    try:
        yield db
    finally:
        db.close()


@app.post("/make-user/", response_model=schemas.UserResponse)
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    new_user = models.User(username=user.username, email=user.email)
    db.add(new_user)
    db.commit()
    db.refresh(new_user)

    #<------------------------------------------------------->
    jwt = RustToken("MY-SECRET-KEY") # better to use .env file
    access = jwt.create_token(new_user.id, 24 * 60 * 60) # 1 day
    refresh = jwt.create_token(new_user.id, 24 * 60 * 60 * 7) # 7 day exp
    return {"refresh_token": refresh, "access_token": access}

```

---

> NOTE: The expiration time is in seconds

---

How to decode Tokens:
```python
    from rustoken import RustToken

    jwt = RustToken("MY-SECRET-KEY") # better to use .env file

    token = "eyJ0eXAiOiJKV1QiLCJhbGciO..."
    try:
        jwt.decode(token)
    except ValueError:
        # if token is expired it return a ValueError
        ...

```

