Skip to content

Commit

Permalink
splic auth middlewares and backends
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvictor committed Apr 7, 2024
1 parent 73fa0c3 commit 06b1cf5
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aws_packages"
version = "0.0.8"
version = "0.0.9"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
Empty file.
File renamed without changes.
27 changes: 27 additions & 0 deletions aws_packages/auth/icp/middlewares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Auth middleware."""

from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response
from aws_lambda_powertools.event_handler.exceptions import UnauthorizedError
from aws_lambda_powertools.event_handler.middlewares import NextMiddleware

from aws_packages.auth.exceptions import JWTAuthError
from aws_packages.auth.icp.auth_backend import icp_auth_backend
from aws_packages.auth.models import AuthorizationRequest


def icp_login_required(
app: APIGatewayRestResolver, next_middleware: NextMiddleware
) -> Response:
"""Login required middleware
:param app: application instance
:param next_middleware: next middleware
:return: Response object after the middleware has been applied
"""

request = AuthorizationRequest(**app.current_event.headers)
try:
icp_auth_backend.authenticate_with_token(request.token)
except JWTAuthError:
raise UnauthorizedError(f"Unauthorized")
return next_middleware(app)
6 changes: 6 additions & 0 deletions aws_packages/auth/icp/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from aws_packages.auth.icp.auth_backend import icp_auth_backend
from aws_packages.auth.views import process_authenticate_request


def process_icp_authenticate_request(request_body):
return process_authenticate_request(request_body, icp_auth_backend)
22 changes: 2 additions & 20 deletions aws_packages/auth/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from aws_lambda_powertools.event_handler.exceptions import UnauthorizedError
from aws_lambda_powertools.event_handler.middlewares import NextMiddleware

from aws_packages.auth.auth_backend_icp import icp_auth_backend
from aws_packages.auth.exceptions import JWTAuthError
from aws_packages.auth.models import AuthorizationRequest

Expand All @@ -21,25 +20,8 @@ def icp_login_required(

request = AuthorizationRequest(**app.current_event.headers)
try:
icp_auth_backend.authenticate_with_token(request.token)
except JWTAuthError:
raise UnauthorizedError(f"Unauthorized")
return next_middleware(app)


def solana_login_required(
app: APIGatewayRestResolver, next_middleware: NextMiddleware
) -> Response:
"""Login required middleware
:param app: application instance
:param next_middleware: next middleware
:return: Response object after the middleware has been applied
"""

request = AuthorizationRequest(**app.current_event.headers)
try:
icp_auth_backend.authenticate_with_token(request.token)
# TODO: generic backend
generic_backend.authenticate_with_token(request.token)
except JWTAuthError:
raise UnauthorizedError(f"Unauthorized")
return next_middleware(app)
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"""

import os

from aws_packages.auth.auth_backend_base import AuthBackendBase
from aws_packages.auth.models import AuthenticationRequest, User
from aws_packages.auth.tokens import AccessToken
Expand Down Expand Up @@ -38,7 +36,7 @@ def get_access_token(self, user: User) -> str:
)

if __name__ == "__main__":
icp_auth_backend = SolanaAuthBackend(
_auth_backend = SolanaAuthBackend(
url="https://ic0.app",
)

Expand Down
27 changes: 27 additions & 0 deletions aws_packages/auth/solana/middlewares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Auth middleware."""

from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response
from aws_lambda_powertools.event_handler.exceptions import UnauthorizedError
from aws_lambda_powertools.event_handler.middlewares import NextMiddleware

from aws_packages.auth.exceptions import JWTAuthError
from aws_packages.auth.models import AuthorizationRequest
from aws_packages.auth.solana.auth_backend import solana_auth_backend


def solana_login_required(
app: APIGatewayRestResolver, next_middleware: NextMiddleware
) -> Response:
"""Login required middleware
:param app: application instance
:param next_middleware: next middleware
:return: Response object after the middleware has been applied
"""

request = AuthorizationRequest(**app.current_event.headers)
try:
solana_auth_backend.authenticate_with_token(request.token)
except JWTAuthError:
raise UnauthorizedError(f"Unauthorized")
return next_middleware(app)
6 changes: 6 additions & 0 deletions aws_packages/auth/solana/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from aws_packages.auth.solana.auth_backend import solana_auth_backend
from aws_packages.auth.views import process_authenticate_request


def process_icp_authenticate_request(request_body):
return process_authenticate_request(request_body, solana_auth_backend)
12 changes: 1 addition & 11 deletions aws_packages/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from aws_lambda_powertools.event_handler import Response, content_types
from aws_lambda_powertools.event_handler.exceptions import BadRequestError

from aws_packages.auth.auth_backend_icp import icp_auth_backend
from aws_packages.auth.auth_backend_solana import solana_auth_backend
from aws_packages.auth.models import AuthenticationRequest, LoginResponse, User
from aws_packages.auth.models import AuthenticationRequest, LoginResponse


def process_authenticate_request(request_body, auth_backend):
Expand All @@ -31,11 +29,3 @@ def process_authenticate_request(request_body, auth_backend):
access_token=auth_backend.get_access_token(auth_user)
).as_dict(),
)


def process_icp_authenticate_request(request_body):
return process_authenticate_request(request_body, icp_auth_backend)


def process_solana_authenticate_request(request_body):
return process_authenticate_request(request_body, solana_auth_backend)

0 comments on commit 06b1cf5

Please sign in to comment.