-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented cross-terminal memos sharing feature (#17)
* add signup login refresh_token password_update push pull supoort * set https path and add small test
- Loading branch information
Showing
11 changed files
with
474 additions
and
7 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import json | ||
from typing import Optional | ||
|
||
import requests | ||
from logzero import logger | ||
from prompt_toolkit import prompt | ||
|
||
from pmemo.api.config import APIConfig, Tokens | ||
|
||
|
||
def input_email() -> str: | ||
return prompt("Email:") | ||
|
||
|
||
def input_password() -> str: | ||
return prompt("Password:", is_password=True) | ||
|
||
|
||
class APIAuthenticator: | ||
def __init__(self) -> None: | ||
self._config = APIConfig() | ||
|
||
def _input_user_info(self) -> tuple[str, str]: | ||
email = input_email() | ||
password = input_password() | ||
return email, password | ||
|
||
def signup(self) -> Tokens: | ||
email, password = self._input_user_info() | ||
res = requests.post( | ||
self._config.signup, data=json.dumps({"email": email, "password": password}) | ||
) | ||
tokens = res.json() | ||
if res.status_code == requests.codes.ok: | ||
logger.info("Signup Success") | ||
else: | ||
logger.error("Signup Failed") | ||
return Tokens( | ||
token=tokens.get("idToken", ""), | ||
refresh_token=tokens.get("refreshToken", ""), | ||
) | ||
|
||
def _refresh_token(self, refresh_token: str) -> Tokens: | ||
res = requests.post( | ||
self._config.refresh_token, | ||
data=json.dumps({"refresh_token": refresh_token.strip('"')}), | ||
) | ||
tokens = res.json() | ||
if res.status_code == requests.codes.ok: | ||
logger.info("Refresh Token Success") | ||
else: | ||
logger.error("Refresh Token Failed") | ||
return Tokens( | ||
token=tokens.get("id_token", ""), | ||
refresh_token=tokens.get("refresh_token", refresh_token), | ||
) | ||
|
||
def login(self, refresh_token: Optional[str]) -> Tokens: | ||
if refresh_token is not None: | ||
return self._refresh_token(refresh_token) | ||
email, password = self._input_user_info() | ||
res = requests.post( | ||
self._config.login, | ||
data=json.dumps({"email": email, "password": password}), | ||
) | ||
tokens = res.json() | ||
if res.status_code == requests.codes.ok: | ||
logger.info("Login Success") | ||
else: | ||
logger.error("Login Failed") | ||
return Tokens( | ||
token=tokens.get("idToken", ""), | ||
refresh_token=tokens.get("refreshToken", ""), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import json | ||
|
||
import requests | ||
from cryptography.fernet import Fernet | ||
from logzero import logger | ||
|
||
from pmemo.api.config import APIConfig, Tokens | ||
|
||
|
||
class APIClient: | ||
def __init__(self, tokens: Tokens, encryption_key: bytes) -> None: | ||
self._config = APIConfig() | ||
self._tokens = tokens | ||
self._fernet = Fernet(encryption_key) | ||
|
||
def store_memo(self, file_name: bytes, content: bytes) -> None: | ||
# Note: Identical file names should yield same encryption | ||
IV = b"\xb3\x03\xbdVn\xeejKH\xc4\x0c\x83\xa7\xba_\x8e" | ||
encrypted_file_name = self._fernet._encrypt_from_parts(file_name, 0, IV).decode( | ||
"utf-8" | ||
) | ||
encrypted_content = self._fernet.encrypt(content).decode("utf-8") | ||
res = requests.post( | ||
self._config.memos, | ||
headers={"Authorization": f"Bearer {self._tokens.token}"}, | ||
data=json.dumps( | ||
dict(file_name=encrypted_file_name, content=encrypted_content) | ||
), | ||
) | ||
if res.status_code == requests.codes.ok: | ||
logger.info("Memo stored successfully: %s", file_name.decode("utf-8")) | ||
else: | ||
logger.error("Failed to store memo") | ||
if self._tokens.token: | ||
logger.error("Maybe login again to refresh the token") | ||
|
||
def get_memos(self) -> list[str]: | ||
res = requests.get( | ||
self._config.memos, | ||
headers={"Authorization": f"Bearer {self._tokens.token}"}, | ||
) | ||
if res.status_code != requests.codes.ok: | ||
logger.error("Failed to get memos") | ||
if self._tokens.token: | ||
logger.error("Maybe login again to refresh the token") | ||
return [] | ||
decrypted_contents = [] | ||
for memo in res.json(): | ||
decrypted_contents.append( | ||
self._fernet.decrypt(memo["content"].encode()).decode("utf-8") | ||
) | ||
return decrypted_contents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import os | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class APIConfig(BaseModel, frozen=True): | ||
domain: str = "https://pmemo.asugawara.com" | ||
version: str = "v1" | ||
|
||
@property | ||
def signup(self) -> str: | ||
return os.path.join(self.domain, self.version, "signup") | ||
|
||
@property | ||
def login(self) -> str: | ||
return os.path.join(self.domain, self.version, "login") | ||
|
||
@property | ||
def refresh_token(self) -> str: | ||
return os.path.join(self.domain, self.version, "refresh_token") | ||
|
||
@property | ||
def password_update(self) -> str: | ||
return os.path.join(self.domain, self.version, "password_update") | ||
|
||
@property | ||
def memos(self) -> str: | ||
return os.path.join(self.domain, self.version, "memos") | ||
|
||
|
||
class Tokens(BaseModel, frozen=True): | ||
token: str = "" | ||
refresh_token: str = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.