|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +from abc import ABC, abstractmethod |
| 17 | +from typing import Type |
| 18 | + |
| 19 | +from veadk.auth.base_auth import BaseAuth |
| 20 | + |
| 21 | + |
| 22 | +class BaseVeAuth(ABC, BaseAuth): |
| 23 | + volcengine_access_key: str |
| 24 | + """Volcengine Access Key""" |
| 25 | + |
| 26 | + volcengine_secret_key: str |
| 27 | + """Volcengine Secret Key""" |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + access_key: str | None = None, |
| 32 | + secret_key: str | None = None, |
| 33 | + ) -> None: |
| 34 | + super().__init__() |
| 35 | + |
| 36 | + final_ak = access_key or os.getenv("VOLCENGINE_ACCESS_KEY") |
| 37 | + final_sk = secret_key or os.getenv("VOLCENGINE_SECRET_KEY") |
| 38 | + |
| 39 | + assert final_ak, "Volcengine access key cannot be empty." |
| 40 | + assert final_sk, "Volcengine secret key cannot be empty." |
| 41 | + |
| 42 | + self.access_key = final_ak |
| 43 | + self.secret_key = final_sk |
| 44 | + |
| 45 | + self._token: str = "" |
| 46 | + |
| 47 | + @abstractmethod |
| 48 | + def _fetch_token(self) -> None: ... |
| 49 | + |
| 50 | + @property |
| 51 | + def token(self) -> str: ... |
| 52 | + |
| 53 | + |
| 54 | +def veauth(auth_token_name: str, auth_cls: Type[BaseVeAuth]): |
| 55 | + def decorator(cls: Type): |
| 56 | + # api_key -> _api_key |
| 57 | + # for cache |
| 58 | + private_auth_token_name = f"_{auth_token_name}" |
| 59 | + setattr(cls, private_auth_token_name, "") |
| 60 | + |
| 61 | + # init a auth cls for fetching token |
| 62 | + auth_cls_instance = "_auth_cls_instance" |
| 63 | + setattr(cls, auth_cls_instance, auth_cls()) |
| 64 | + |
| 65 | + def getattribute(self, name: str): |
| 66 | + if name != auth_token_name: |
| 67 | + return object.__getattribute__(self, name) |
| 68 | + if name == auth_token_name: |
| 69 | + token = object.__getattribute__(self, name) |
| 70 | + |
| 71 | + if token: |
| 72 | + return token |
| 73 | + elif not token and not getattr(cls, private_auth_token_name): |
| 74 | + token = getattr(cls, auth_cls_instance).token |
| 75 | + setattr(cls, private_auth_token_name, token) |
| 76 | + return token |
| 77 | + elif not token and getattr(cls, private_auth_token_name): |
| 78 | + return getattr(cls, private_auth_token_name) |
| 79 | + return token |
| 80 | + |
| 81 | + setattr(cls, "__getattribute__", getattribute) |
| 82 | + return cls |
| 83 | + |
| 84 | + return decorator |
0 commit comments