Skip to main content

Provides a simple OAuth server deployed as an AWS Lambda function, intended to support development with Localstack

Project description

Simple OAuth Server

A simple OAuth server deployable to AWS Lambda, designed for development and testing environments. This server offers two primary services:

  1. Authorization - Clients can provide their credentials and obtain a bearer token.
  2. Validation - Validates bearer tokens for authorizing AWS API Gateway requests.

This service is targeted at developers who need a mock OAuth server for testing and development.

Prerequisites

The deployment uses Pulumi for AWS Lambda, and components are deployed using the Pulumi CLI. You can deploy the server as part of a larger Pulumi deployment or separately.

Requirements

  • AWS account credentials
  • Pulumi CLI installed
  • Python environment

Set Up

Step 1: Deployment Script

You can deploy the OAuth server using Pulumi. Below is an example script that starts the OAuth server with the test configuration provided. You need a configuration that defines test clients with their credentials and permissions. This configuration is expected to be in YAML format. When starting a OAuth server configation can be either passed as string inline like the example below or using a file name.

# __main__.py

import simple_oauth_server

test_users = """
clients:
  client1:
    client_secret: "client1-secret"
    audience: "test-api"
    sub: "client1-subject"
    scope: "read:data"
    permissions:
      - "read:data"
  
  client2:
    client_secret: "client2-secret"
    audience: "test-api"
    sub: "client2-subject"
    scope: "write:data"
    permissions:
      - "write:data"
"""

oauth_server = simple_oauth_server.start("oauth", config=test_users)

Step 3: Run Pulumi Deployment

To deploy the server:

pulumi up

Pulumi will use the provided configuration and start the OAuth service on AWS Lambda.

Usage

Authorization

To obtain a bearer token, clients must provide their client_id, client_secret, and the target audience (API they want to access). Here's an example of how to request an authorization token:

Example Request:

curl --request POST \
  --url https://your-oauth-server/authorize \
  --header 'Content-Type: application/json' \
  --data '{
    "client_id": "client1",
    "client_secret": "client1-secret",
    "audience": "test-api",
    "grant_type": "client_credentials"
  }'

Example Response:

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "token_type": "Bearer",
  "expires_in": 86400
}

This token can be used to authenticate subsequent API requests.

Validation

The validation service can be integrated with AWS API Gateways as a authorizer to validate incoming requests using the bearer token.

Example AWS API Gateway Integration:

  1. Set up a Lambda authorizer in AWS API Gateway.
  2. Use the token_validator.py Lambda function to validate tokens.
  3. Configure API Gateway routes to use the Lambda authorizer.

Example Request:

curl --request POST \
  --url https://your-api-gateway-endpoint/test-api/greet \
  --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'

The token_validator.py function verifies the JWT token, ensuring the request is authenticated before allowing access to your API routes.

Example Token Validator (Lambda):

import os
import jwt

def validate_token(token):
    public_key = open('public_key.pem').read()  # Load public key from file
    return jwt.decode(token, public_key, algorithms=["RS256"], audience="test-api")

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

simple_oauth_server-0.1.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

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

simple_oauth_server-0.1.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file simple_oauth_server-0.1.0.tar.gz.

File metadata

  • Download URL: simple_oauth_server-0.1.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for simple_oauth_server-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9bc7770fb775376a8ab9bc13b602521aababb00077522d16025b891dbe7ea66b
MD5 932e869fbb985068c5636c5cf4adcefc
BLAKE2b-256 7d5f9c39806fb2a0870069563c0675c15897631a60a7a02cb3d85d7176a2e70f

See more details on using hashes here.

File details

Details for the file simple_oauth_server-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_oauth_server-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4330f4b1a8038f0c0a77d42ffd5efb97baca63c576bebdf4a521af3fbf8aab40
MD5 343f7a0efc573d6631fac2f9aefead4f
BLAKE2b-256 39733da7b1dde12793b738e747ba91b4b6f4ace94f3793fdff1600db8cea6af5

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