Skip to main content

Anzar is a lightweight authentication and authorization framework that runs as a separate microservice

Project description

Anzar SDK Documentation

Server

from anzar_openapi import Role
from flask import g
from flask import Flask, jsonify
from anzar.server import require_auth, require_role, AnzarAuth

app = Flask(__name__)

requireAuth = AnzarAuth(
    secret = process.env.SECRET,
    audience = process.env.AUDIENCE
    issuerBaseURL = process.env.ISSUER
)

@app.route("/protected")
@require_auth(requireAuth)
def me():
    return jsonify(g.user_id)


@app.route("/admin")
@requires_role(requireAuth, role=Role.USER)
def admin():
    return jsonify(f"admin panel, {g.user_id}")


if __name__ == "__main__":
    app.run(debug=True)

Client

Install The Python SDK

In a python project run the following command to install the anzar package.

uv

$ uv add anzar

pip

# in a virtual env run
$ pip install anzar

Create Anzar Auth Instance

in your main entry file (main.py for example), import anzar.yml and parse it as a YAML file

def load_config(path: str) -> AnzarConfig:
    import yaml

    try:
        with open(path, "r") as f:
            data = yaml.safe_load(f)
        return AnzarConfig(**data)
    except Exception as e:
        import sys

        sys.exit("check your configuration file: anzar.yml")

import Anzar Auth and create your auth instance

# Initialize once at application startup
# The SDK will communicate with your Anzar container at the configured api_url
from anzar import Anzar

config = load_config("anzar.yml")
anzar = Anzar(config, options=None)

Note "Configuring Token Persistence" To keep users logged in across restarts, choose a token storage adapter.

```python
from anzar.adapters import RedisStorage
from anzar.types import SdkOptions 

anzar = Anzar(config, SdkOptions(storage=RedisStorage()))
```

Basic Usage

Anzar provides authentication support for email and password.

📝 Note: Other methods of authentication will be implemented later

Sign Up

To sign up a user you need to call the method register with the user's information.

from anzar.types import ApiException, AuthResponse, ErrorCode

try:
    data: AuthResponse = anzar.Auth.register({
      "username": "username",
      "email": "user@example.com",
      "password": "password"
    })
except ApiException as e:
    if e.data is None:
        return

    if e.data.code == ErrorCode.InvalidCredentials:
        show_error("Invalid email or password.")
    elif e.data.code == ErrorCode.AccountNotVerified:
        show_error("Please verify your email before logging in.")

📝 Note: By default, the users are automatically signed in after they successfully sign up. Disabling this behavior will be implemented later

Sign In

To sign in a user you need to call the method login.

from anzar.types import ApiException, AuthResponse, ErrorCode

try:
    data: AuthResponse = anzar.Auth.login({
      "email": "user@example.com",
      "password": "password"
    })
except ApiException as e:
    if e.data is None:
        return

    if e.data.code == ErrorCode.InvalidCredentials:
        show_error("Invalid email or password.")
    elif e.data.code == ErrorCode.AccountNotVerified:
        show_error("Please verify your email before logging in.")

Project details


Download files

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

Source Distribution

anzar-0.4.3.tar.gz (48.4 kB view details)

Uploaded Source

Built Distribution

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

anzar-0.4.3-py3-none-any.whl (142.0 kB view details)

Uploaded Python 3

File details

Details for the file anzar-0.4.3.tar.gz.

File metadata

  • Download URL: anzar-0.4.3.tar.gz
  • Upload date:
  • Size: 48.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for anzar-0.4.3.tar.gz
Algorithm Hash digest
SHA256 38c3e64e41c07eae236afa19314326082ef903a1a4909c4afb1fdd0e9db7c327
MD5 147003779a2c1d99ca43336d35717396
BLAKE2b-256 d2a2a21a483cfc71016ca1d2242ede1e26f37ca10d84b480a038dd448e70d961

See more details on using hashes here.

File details

Details for the file anzar-0.4.3-py3-none-any.whl.

File metadata

  • Download URL: anzar-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 142.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for anzar-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 132cbcf14c3c121034def26ec862f6331383727b893a43e1c4cce160f2b8ddb9
MD5 2e825ac00b9f01cdaf6c8575943775fd
BLAKE2b-256 6deed4219a8f97027c63fa45bca3d732fc043163e18916d9a0dad3a0b2054443

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