add apikey

This commit is contained in:
2025-03-27 01:14:18 -03:00
parent 7021833476
commit 8118dfd403
14 changed files with 114 additions and 69 deletions

View File

@@ -1,3 +1,4 @@
{"id": {"S": "apikey"}, "sk": {"S": "32504457-f133-4c00-936b-6aa712ca9f40"}, "tenant": {"M": {"id": {"S": "*"}, "name": {"S": "default"}}}}
{"id": {"S": "5OxmMjL-ujoR5IMGegQz"}, "sk": {"S": "0"}, "update_date": {"S": "2024-02-08T16:42:33.776409-03:00"}, "create_date": {"S": "2019-03-25T00:00:00-03:00"}, "email_verified": {"BOOL": true}, "cognito:sub": {"S": "58efed8d-d276-41a8-8502-4ab8b5a6415e"}, "cpf": {"S": "07879819908"}, "email": {"S": "sergio@somosbeta.com.br"}, "name": {"S": "S\u00e9rgio Rafael de Siqueira"}, "last_login": {"S": "2024-02-08T20:53:45.818126-03:00"}, "tenant:org_id": {"L": [{"S": "cJtK9SsnJhKPyxESe7g3DG"}]}}
{"id": {"S": "5OxmMjL-ujoR5IMGegQz"}, "sk": {"S": "cognito"}, "create_date": {"S": "2025-03-03T17:12:26.443507-03:00"}, "sub": {"S": "58efed8d-d276-41a8-8502-4ab8b5a6415e"}}
{"id": {"S": "5OxmMjL-ujoR5IMGegQz"}, "sk": {"S": "emails#sergio@somosbeta.com.br"}, "email_verified": {"BOOL": true}, "update_date": {"S": "2024-02-08T16:42:33.776409-03:00"}, "create_date": {"S": "2019-03-25T00:00:00-03:00"}, "email_primary": {"BOOL": true}, "mx_record_exists": {"BOOL": true}, "update_date": {"S": "2023-11-09T12:13:04.308986-03:00"}}

View File

@@ -1,10 +1,12 @@
from layercake.dynamodb import DynamoDBCollection, DynamoDBPersistenceLayer
import auth as app
from auth import _parse_bearer_token
from .conftest import LambdaContext
def test_bearer_jwt(lambda_context: LambdaContext, dynamodb_seeds):
def test_bearer_jwt(lambda_context: LambdaContext):
# You should mock the Cognito user to pass the test
app.get_user = lambda *args, **kwargs: {
'sub': '58efed8d-d276-41a8-8502-4ab8b5a6415e',
@@ -29,6 +31,31 @@ def test_bearer_jwt(lambda_context: LambdaContext, dynamodb_seeds):
}
def test_bearer_apikey(
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
lambda_context: LambdaContext,
):
app.collect = DynamoDBCollection(dynamodb_persistence_layer)
event = {
'headers': {
'authorization': 'Bearer sk-32504457-f133-4c00-936b-6aa712ca9f40',
}
}
# This data was added from seeds
assert app.lambda_handler(event, lambda_context) == {
'isAuthorized': True,
'context': {'tenant': {'name': 'default', 'id': '*'}},
}
# This data was added from seeds
assert app.lambda_handler(
{'headers': {'authorization': 'Bearer sk-abc'}},
lambda_context,
) == {'isAuthorized': False}
def test_parse_bearer_token_api_key():
bearer = _parse_bearer_token(
'Bearer sk-35433970-6857-4062-bb43-f71683b2f68e',