pytorch-lightning
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
15from typing import Dict
16
17from lightning_cloud.openapi import ApiClient, AuthServiceApi, V1LoginRequest
18
19from lightning.app.utilities.login import Auth
20
21
22# This class joins common things for reading logs,
23# initialization and getting API token
24class _AuthTokenGetter:
25def __init__(self, api_client: ApiClient):
26self.api_client = api_client
27self._auth = Auth()
28self._auth.authenticate()
29self._auth_service = AuthServiceApi(api_client)
30
31def _get_api_token(self) -> str:
32token_resp = self._auth_service.auth_service_login(
33body=V1LoginRequest(
34username=self._auth.username,
35api_key=self._auth.api_key,
36)
37)
38return token_resp.token
39
40
41def _credential_string_to_basic_auth_params(credential_string: str) -> Dict[str, str]:
42"""Returns the name/ID pair for each given Secret name.
43
44Raises a `ValueError` if any of the given Secret names do not exist.
45
46"""
47if credential_string.count(":") != 1:
48raise ValueError(
49"Credential string must follow the format username:password; "
50+ f"the provided one ('{credential_string}') does not."
51)
52
53username, password = credential_string.split(":")
54
55if not username:
56raise ValueError("Username cannot be empty.")
57
58if not password:
59raise ValueError("Password cannot be empty.")
60
61return {"username": username, "password": password}
62