Skip to main content

Usecase agnostic implementation of AWS4 signing schema.

Project description

Usecase agnostic implementation of AWS4 Sig v4

This implementation aims to be usecase agnostic. As such it accepts the component pieces of a request rather than a full opinionated request object like httpx.Request.

https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html

Usage

Validation

from aws4 import generate_challenge, validate_challenge

payload = "<extract content from request>"

challenge = generate_challenge(
    method=request.method,
    url=request.url,
    headers=request.headers,
    content=payload.decode("utf-8"),
)

secret_access_key = <load secret key using the challenge.access_key_id>

validate_challenge(challenge, secret_key.secret_access_key)

Signing

An example of an httpx AWS4 request signing. In this example the Authorization header is injected into request.headers

from datetime import datetime, timezone

import aws4


service = "s3"
region = "us-east-1"
access_key_id = "my-access-key-id"
secret_access_key = "my-secret-access-key"

def http_aws4_auth(request: httpx.Request):
    dt = datetime.now(tz=timezone.utc)
    request.headers["x-amz-date"] = aws4.to_amz_date(dt)
    request.headers["host"] = request.url.netloc.decode("utf-8")

    body = request.content.decode("utf-8")
    if body:
        request.headers["Content-Length"] = str(len(body))

    aws4.sign_request(
        service,
        request.method,
        request.url,
        region,
        request.headers,
        body,
        access_key_id,
        secret_access_key,
        dt,
    )

with httpx.Client() as client:
    r = client.request(
        url="http://localhost",
        auth=auth,
    )

Extra credit

Thanks to @ozzzzz and @ivanmisic for work on the initial httpx/fastapi implementations this was extracted from.

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

auth_aws4-0.1.8.tar.gz (55.1 kB view details)

Uploaded Source

Built Distribution

auth_aws4-0.1.8-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file auth_aws4-0.1.8.tar.gz.

File metadata

  • Download URL: auth_aws4-0.1.8.tar.gz
  • Upload date:
  • Size: 55.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for auth_aws4-0.1.8.tar.gz
Algorithm Hash digest
SHA256 1b5545210f9ef1da7290981e67b932b9530c50c1efbcf640fac0cf4c63976de1
MD5 fcdecbbb06e8612a09b9e072008b97ae
BLAKE2b-256 c13800468b3a98acb3e90e4b6a403613710c07d74f2d262f21afc9686ce3a898

See more details on using hashes here.

File details

Details for the file auth_aws4-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: auth_aws4-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for auth_aws4-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 039429b8d1b64bddbf311cc733388149823036f66cbdc6db7dee921417922f3e
MD5 cb2887db31e435754799ceaaab829dea
BLAKE2b-256 f4145d7512f6dd5b6ea2a8087b70922018523214d4cca33ca51035722e8f8343

See more details on using hashes here.

Supported by

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