Skip to main content

JWT library implemented in Rust for Python

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

Rustoken

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 make SECRET-KEY in Rustoken:

from rustoken import secret_key

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

How to make SECRET-KEY on linux:

# 32 bytes (256 bits) base64
openssl rand -base64 32   # outputs a base64 string you can store

# or 64 bytes (512 bits)
openssl rand -base64 64

Usage In Fast-Api:

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:

    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
        ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

rustoken-0.1.4-cp313-cp313-manylinux_2_34_x86_64.whl (810.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

rustoken-0.1.4-cp311-cp311-manylinux_2_34_x86_64.whl (830.4 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

File details

Details for the file rustoken-0.1.4-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rustoken-0.1.4-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 015618871d2def23c51aeb405beb62f53d8b6f86c1278ce8a0b489fd9ede84f5
MD5 b6ff2e76b7198de5c496ff660ca3597c
BLAKE2b-256 8251ce2a68fe41f3ac44fd4ec3301ea9ed57a56fbed07db170434974da773280

See more details on using hashes here.

File details

Details for the file rustoken-0.1.4-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rustoken-0.1.4-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 71bc96e12753199ddaba2422735a09532b787d04ead317a270a92a511e736ff2
MD5 ccd646c685755ca668632c356d1d7041
BLAKE2b-256 5c1ef34060e3cf7f3a8dc65c19e9c8dbba454bf377f0718cbd57ad1a7032df31

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page