forked from ITISFoundation/osparc-simcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Refactor API-keys service (ITISFoundation#6843)
Co-authored-by: odeimaiz <[email protected]>
- Loading branch information
1 parent
6d1ee29
commit 34374b4
Showing
33 changed files
with
1,150 additions
and
663 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Depends, status | ||
from models_library.api_schemas_webserver.auth import ( | ||
ApiKeyCreateRequest, | ||
ApiKeyCreateResponse, | ||
ApiKeyGet, | ||
) | ||
from models_library.generics import Envelope | ||
from models_library.rest_error import EnvelopedError | ||
from simcore_service_webserver._meta import API_VTAG | ||
from simcore_service_webserver.api_keys._exceptions_handlers import _TO_HTTP_ERROR_MAP | ||
from simcore_service_webserver.api_keys._rest import ApiKeysPathParams | ||
|
||
router = APIRouter( | ||
prefix=f"/{API_VTAG}", | ||
tags=["auth"], | ||
responses={ | ||
i.status_code: {"model": EnvelopedError} for i in _TO_HTTP_ERROR_MAP.values() | ||
}, | ||
) | ||
|
||
|
||
@router.post( | ||
"/auth/api-keys", | ||
operation_id="create_api_key", | ||
status_code=status.HTTP_201_CREATED, | ||
response_model=Envelope[ApiKeyCreateResponse], | ||
) | ||
async def create_api_key(_body: ApiKeyCreateRequest): | ||
"""creates API keys to access public API""" | ||
|
||
|
||
@router.get( | ||
"/auth/api-keys", | ||
operation_id="list_api_keys", | ||
response_model=Envelope[list[ApiKeyGet]], | ||
status_code=status.HTTP_200_OK, | ||
) | ||
async def list_api_keys(): | ||
"""lists API keys by this user""" | ||
|
||
|
||
@router.get( | ||
"/auth/api-keys/{api_key_id}", | ||
operation_id="get_api_key", | ||
response_model=Envelope[ApiKeyGet], | ||
status_code=status.HTTP_200_OK, | ||
) | ||
async def get_api_key(_path: Annotated[ApiKeysPathParams, Depends()]): | ||
"""returns the API Key with the given ID""" | ||
|
||
|
||
@router.delete( | ||
"/auth/api-keys/{api_key_id}", | ||
operation_id="delete_api_key", | ||
status_code=status.HTTP_204_NO_CONTENT, | ||
) | ||
async def delete_api_key(_path: Annotated[ApiKeysPathParams, Depends()]): | ||
"""deletes the API key with the given ID""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
# | ||
# core --- | ||
"_auth", | ||
"_auth_api_keys", | ||
"_groups", | ||
"_tags", | ||
"_tags_groups", # after _tags | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
35 changes: 35 additions & 0 deletions
35
packages/models-library/src/models_library/rpc/webserver/auth/api_keys.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import datetime as dt | ||
from typing import Annotated | ||
|
||
from models_library.basic_types import IDStr | ||
from pydantic import BaseModel, ConfigDict, Field | ||
|
||
|
||
class ApiKeyCreate(BaseModel): | ||
display_name: Annotated[str, Field(..., min_length=3)] | ||
expiration: dt.timedelta | None = None | ||
|
||
model_config = ConfigDict( | ||
from_attributes=True, | ||
) | ||
|
||
|
||
class ApiKeyGet(BaseModel): | ||
id: IDStr | ||
display_name: Annotated[str, Field(..., min_length=3)] | ||
api_key: str | None = None | ||
api_secret: str | None = None | ||
|
||
model_config = ConfigDict( | ||
from_attributes=True, | ||
json_schema_extra={ | ||
"examples": [ | ||
{ | ||
"id": "42", | ||
"display_name": "test-api-forever", | ||
"api_key": "key", | ||
"api_secret": "secret", | ||
}, | ||
] | ||
}, | ||
) |
Empty file.
Empty file.
66 changes: 66 additions & 0 deletions
66
packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/webserver/auth/api_keys.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import logging | ||
|
||
from models_library.api_schemas_webserver import WEBSERVER_RPC_NAMESPACE | ||
from models_library.basic_types import IDStr | ||
from models_library.rabbitmq_basic_types import RPCMethodName | ||
from models_library.rpc.webserver.auth.api_keys import ApiKeyCreate, ApiKeyGet | ||
from pydantic import TypeAdapter | ||
from servicelib.logging_utils import log_decorator | ||
from servicelib.rabbitmq import RabbitMQRPCClient | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
@log_decorator(_logger, level=logging.DEBUG) | ||
async def create_api_key( | ||
rabbitmq_rpc_client: RabbitMQRPCClient, | ||
*, | ||
user_id: str, | ||
product_name: str, | ||
api_key: ApiKeyCreate, | ||
) -> ApiKeyGet: | ||
result: ApiKeyGet = await rabbitmq_rpc_client.request( | ||
WEBSERVER_RPC_NAMESPACE, | ||
TypeAdapter(RPCMethodName).validate_python("create_api_key"), | ||
user_id=user_id, | ||
product_name=product_name, | ||
api_key=api_key, | ||
) | ||
assert isinstance(result, ApiKeyGet) | ||
return result | ||
|
||
|
||
@log_decorator(_logger, level=logging.DEBUG) | ||
async def get_api_key( | ||
rabbitmq_rpc_client: RabbitMQRPCClient, | ||
*, | ||
user_id: str, | ||
product_name: str, | ||
api_key_id: IDStr, | ||
) -> ApiKeyGet: | ||
result: ApiKeyGet = await rabbitmq_rpc_client.request( | ||
WEBSERVER_RPC_NAMESPACE, | ||
TypeAdapter(RPCMethodName).validate_python("get_api_key"), | ||
user_id=user_id, | ||
product_name=product_name, | ||
api_key_id=api_key_id, | ||
) | ||
assert isinstance(result, ApiKeyGet) | ||
return result | ||
|
||
|
||
async def delete_api_key( | ||
rabbitmq_rpc_client: RabbitMQRPCClient, | ||
*, | ||
user_id: str, | ||
product_name: str, | ||
api_key_id: IDStr, | ||
) -> None: | ||
result = await rabbitmq_rpc_client.request( | ||
WEBSERVER_RPC_NAMESPACE, | ||
TypeAdapter(RPCMethodName).validate_python("delete_api_key"), | ||
user_id=user_id, | ||
product_name=product_name, | ||
api_key_id=api_key_id, | ||
) | ||
assert result is None |
Oops, something went wrong.