Beispiel #1
0
async def create_api_keys(request: Request,
                          key_info: m.AddApiKey,
                          session: Session = Depends(db.session)):
    """
    API KEY 생성
    :param request:
    :param key_info:
    :param session:
    :return:
    """
    user = request.state.user

    api_keys = ApiKeys.filter(session, user_id=user.id,
                              status='active').count()
    if api_keys == MAX_API_KEY:
        raise ex.MaxKeyCountEx()

    alphabet = string.ascii_letters + string.digits
    s_key = ''.join(secrets.choice(alphabet) for _ in range(40))
    uid = None
    while not uid:
        uid_candidate = f"{str(uuid4())[:-12]}{str(uuid4())}"
        uid_check = ApiKeys.get(access_key=uid_candidate)
        if not uid_check:
            uid = uid_candidate

    key_info = key_info.dict()
    new_key = ApiKeys.create(session,
                             auto_commit=True,
                             secret_key=s_key,
                             user_id=user.id,
                             access_key=uid,
                             **key_info)
    return new_key
Beispiel #2
0
async def delete_api_keys(request: Request, key_id: int, access_key: str):
    user = request.state.user
    await check_api_owner(user.id, key_id)
    search_by_key = ApiKeys.filter(access_key=access_key)
    if not search_by_key.first():
        raise ex.NoKeyMatchEx()
    search_by_key.delete(auto_commit=True)
    return MessageOk()
Beispiel #3
0
async def get_api_keys(request: Request):
    """
    API KEY 조회
    :param request:
    :return:
    """
    user = request.state.user
    api_keys = ApiKeys.filter(user_id=user.id).all()
    return api_keys
Beispiel #4
0
async def update_api_keys(request: Request, key_id: int, key_info: m.AddApiKey):
    """
    API KEY User Memo Update
    :param request:
    :param key_id:
    :param key_info:
    :return:
    """
    user = request.state.user
    key_data = ApiKeys.filter(id=key_id)
    if key_data and key_data.first().user_id == user.id:
        return key_data.update(auto_commit=True, **key_info.dict())
    raise ex.NoKeyMatchEx()