Skip to main content

AWS Lambda REST ToolBox

Project description

Lambdalizator

AWS Lambda REST Toolbox inspired by Flask. Created and Open Sourced thanks to LocalBini.com by pdyba and songofroland.

Python 3.8+ Black

Authentication

Lambdalizator can automatically read the value of the Authentication HTTP header and transform it into the User object that is available as a part of the request. The Authentication header must contain a JWT token that will be verified using one of the configured public keys (see Configuration below). The User object will have properties corresponding to the key-value pairs from the token's data.

To enable authentication provide a value for either ALLOWED_PUBLIC_KEYS or ALLOWED_AUDIENCES environment variables.

Configuration

Lambdalizator can be configured using the following environment variables:

  • ALLOWED_PUBLIC_KEYS - a list of public keys that can be used for decoding auth tokens send in the Authentication and Authorization headers. If you are using Cognito, you can use public keys from: https://cognito-idp.{your aws region}.amazonaws.com/{your pool id}/.well-known/jwks.json.
  • ALLOWED_AUDIENCES - a list of audiences that will be used for verifying the JWTs send in the Authentication and Authorization headers. It should be a comma-separated list of strings, e.g. aud1,aud2. If not set, any audience will be considered valid.
  • ALLOWED_ISS - allowed issuer of JWT - Security feature. If not set, issuer will not be checked.
  • AUTH_REMOVE_PREFIXES - if enabled, all fields starting with a prefix (like cognito:) in the auth token will have the prefix removed. Defaults to False (set as "0" or "1").
  • PRINT_TRACEBACK - more verbose errors. Defaults to False (set as "0" or "1").
  • LOGGING_LEVEL - log level used in the application. Defaults to INFO.
  • EXPIRATION_KEY - for defining the expiration key used in authorization default "exp" according to JWT standard.

Hello World Example:

1. Define resource

# simple_resource.py

from lbz.router import add_route
from lbz.response import Response
from lbz.resource import Resource

class HelloWorld(Resource):

    @add_route("/", method="GET")
    def list(self):
        return Response({"message": "HelloWorld"})

2. Define handler

# simple_resource_handler.py

from lbz.exceptions import LambdaFWException

from simple_resource import HelloWorld


def handle(event, context):
    try:
        exp = HelloWorld(event)
        resp = exp()
        return resp
    except Exception as err:
        return LambdaFWException().to_dict()

3. Create dev Server 🖥️

# simple_resource_dev.py

from lbz.dev.server import MyDevServer

from simple_resource.simple_resource import HelloWorld

if __name__ == '__main__':
    server = MyDevServer(acls=HelloWorld, port=8001)
    server.run()

4. Don't forget to unit test ⌨️

# python -m unittest simple_resource_test.py
import unittest
from lbz.dev.test import Client

from simple_resource import HelloWorld


class PublicTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.client = Client(resource=HelloWorld)

    def test_filter_queries_all_active_when_no_params(self):
        data = self.client.get("/").to_dict()["body"]
        self.assertEqual(data, '{"message":"HelloWorld"}')

5. Authenticate it 💂

# simple_auth/simple_resource.py

from lbz.router import add_route
from lbz.response import Response
from lbz.resource import Resource
from lbz.authz import add_authz, authorize, set_authz


@set_authz
class HelloWorld(Resource):
    _name = "helloworld"

    @authorize
    @add_authz()
    @add_route("/", method="GET")
    def list(self, restrictions=None):
        return Response({"message": f"Hello, {self.request.user.username} !"})

Documentation

WIP

Changelog

Release Changelogs

Contribution

We are always happy to have new contributions. We have marked issues good for anyone looking to get started Please take a look at our Contribution guidelines.

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

lbz-0.3.5.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

lbz-0.3.5-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file lbz-0.3.5.tar.gz.

File metadata

  • Download URL: lbz-0.3.5.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.1

File hashes

Hashes for lbz-0.3.5.tar.gz
Algorithm Hash digest
SHA256 210deb5411d497bbd356e4b4d9ebb214abce557c46373ad4a3797d055257721e
MD5 59b4664f387c3c3bd9e485b6d309a091
BLAKE2b-256 4218bf8ba2f94ce2ca88081cfed57aa8b0b6be8aca8d1ea0906b790e1afedb7f

See more details on using hashes here.

File details

Details for the file lbz-0.3.5-py3-none-any.whl.

File metadata

  • Download URL: lbz-0.3.5-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.1

File hashes

Hashes for lbz-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 72d2384e47a51e6e268442df6cd7c9211eebd615c7d6af94dc8db1651beab2b1
MD5 3f0ed78d97c01f3d205fad42b651a83f
BLAKE2b-256 e93f8d9dad03495f83de9a2269356945a2feb1b5015dd573163140dbed1accc9

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