Skip to content

Commit

Permalink
Merge pull request #63 from rlatn1234/auth-impl
Browse files Browse the repository at this point in the history
계정정보 입력기능 추가
  • Loading branch information
roeniss authored Feb 9, 2024
2 parents 43c6551 + bf61a59 commit 197f767
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ soupsieve==2.5
tomli==2.0.1
urllib3==1.26.18
webencodings==0.5.1
tomli-w==1.0.0
56 changes: 52 additions & 4 deletions src/dhapi/router/credentials_provider.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,66 @@
import os
import logging
import tomli
import tomli_w


logger = logging.getLogger(__name__)


def get_credentials(profile_name):
try:
credentials = _read_credentials_file(profile_name)
except FileNotFoundError:
print("❌ ~/.dhapi/credentials 파일을 찾을 수 없습니다. 파일을 생성하고 프로필을 추가하시겠습니까? [Y/n] ", end="")
answer = input().strip().lower()
if answer in ["y", "yes", ""]:
print("📝 입력된 프로필 이름을 사용하시겠습니까? [Y/n]", end="")
answer = input().strip().lower()
if answer in ["y", "yes", ""]:
_add_credentials(profile_name)
else:
print("📝 프로필 이름을 입력하세요: ", end="")
profile_name = input().strip()
_add_credentials(profile_name)
else:
raise FileNotFoundError("~/.dhapi/credentials 파일을 찾을 수 없습니다.")

credentials = _read_credentials_file(profile_name)

if credentials is None:
print(f"❌'{profile_name}' 프로필을 찾지 못했습니다. 추가하시겠습니까? [Y/n] ", end="")
answer = input().strip().lower()
if answer in ["y", "yes", ""]:
_add_credentials(profile_name)
credentials = _read_credentials_file(profile_name)
return credentials
raise ValueError(f"~/.dhapi/credentials 파일에서 '{profile_name}' 프로필을 찾지 못했습니다.")

return credentials


def _read_credentials_file(profile_name):
with open(os.path.expanduser("~/.dhapi/credentials"), "r", encoding="UTF-8") as f:
file = f.read()
config = tomli.loads(file)
credentials = config.get(profile_name)
return credentials


def _add_credentials(profile_name):
print("📝 사용자 ID를 입력하세요: ", end="")
user_id = input().strip()
print("📝 사용자 비밀번호를 입력하세요: ", end="")
user_pw = input().strip()

doc = {profile_name: {"username": user_id, "password": user_pw}}

with open(os.path.expanduser("~/.dhapi/credentials"), "r", encoding="UTF-8") as f:
file = f.read()
config = tomli.loads(file)

credentials = config.get(profile_name)
if credentials is None:
raise ValueError(f"~/.dhapi/credentials 파일에서 '{profile_name}' 프로필을 찾지 못했습니다.")
return config.get(profile_name)
doc.update(config)

with open(os.path.expanduser("~/.dhapi/credentials"), "wb") as f:
tomli_w.dump(doc, f)
f.close()

0 comments on commit 197f767

Please sign in to comment.