pytorch-lightning

Форк
0
61 строка · 2.0 Кб
1
# Copyright The Lightning AI team.
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
from typing import Dict
16

17
from lightning_cloud.openapi import ApiClient, AuthServiceApi, V1LoginRequest
18

19
from lightning.app.utilities.login import Auth
20

21

22
# This class joins common things for reading logs,
23
# initialization and getting API token
24
class _AuthTokenGetter:
25
    def __init__(self, api_client: ApiClient):
26
        self.api_client = api_client
27
        self._auth = Auth()
28
        self._auth.authenticate()
29
        self._auth_service = AuthServiceApi(api_client)
30

31
    def _get_api_token(self) -> str:
32
        token_resp = self._auth_service.auth_service_login(
33
            body=V1LoginRequest(
34
                username=self._auth.username,
35
                api_key=self._auth.api_key,
36
            )
37
        )
38
        return token_resp.token
39

40

41
def _credential_string_to_basic_auth_params(credential_string: str) -> Dict[str, str]:
42
    """Returns the name/ID pair for each given Secret name.
43

44
    Raises a `ValueError` if any of the given Secret names do not exist.
45

46
    """
47
    if credential_string.count(":") != 1:
48
        raise ValueError(
49
            "Credential string must follow the format username:password; "
50
            + f"the provided one ('{credential_string}') does not."
51
        )
52

53
    username, password = credential_string.split(":")
54

55
    if not username:
56
        raise ValueError("Username cannot be empty.")
57

58
    if not password:
59
        raise ValueError("Password cannot be empty.")
60

61
    return {"username": username, "password": password}
62

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.