Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor oauth classes #499

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions ytmusicapi/auth/oauth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .base import OAuthToken
from .credentials import OAuthCredentials
from .refreshing import RefreshingToken
from .token import OAuthToken, RefreshingToken

__all__ = ["OAuthCredentials", "RefreshingToken", "OAuthToken"]
__all__ = ["OAuthCredentials", "OAuthToken", "RefreshingToken"]
148 changes: 0 additions & 148 deletions ytmusicapi/auth/oauth/base.py

This file was deleted.

47 changes: 24 additions & 23 deletions ytmusicapi/auth/oauth/credentials.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import webbrowser
from typing import Dict, Optional
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Dict, Mapping, Optional

import requests

Expand All @@ -12,10 +13,29 @@
OAUTH_USER_AGENT,
)

from .base import Credentials, OAuthToken
from .exceptions import BadOAuthClient, UnauthorizedOAuthClient
from .models import AuthCodeDict, BaseTokenDict, RefreshableTokenDict
from .refreshing import RefreshingToken


@dataclass
class Credentials(ABC):
"""Base class representation of YouTubeMusicAPI OAuth Credentials"""

client_id: str
client_secret: str

@abstractmethod
def get_code(self) -> Mapping:
"""Method for obtaining a new user auth code. First step of token creation."""

@abstractmethod
def token_from_code(self, device_code: str) -> RefreshableTokenDict:
"""Method for verifying user auth code and conversion into a FullTokenDict."""

@abstractmethod
def refresh_token(self, refresh_token: str) -> BaseTokenDict:
"""Method for requesting a new access token for a given refresh_token.
Token must have been created by the same OAuth client."""


class OAuthCredentials(Credentials):
Expand Down Expand Up @@ -90,25 +110,6 @@ def token_from_code(self, device_code: str) -> RefreshableTokenDict:
)
return response.json()

def prompt_for_token(self, open_browser: bool = False, to_file: Optional[str] = None) -> RefreshingToken:
"""
Method for CLI token creation via user inputs.

:param open_browser: Optional. Open browser to OAuth consent url automatically. (Default = False).
:param to_file: Optional. Path to store/sync json version of resulting token. (Default = None).
"""

code = self.get_code()
url = f"{code['verification_url']}?user_code={code['user_code']}"
if open_browser:
webbrowser.open(url)
input(f"Go to {url}, finish the login flow and press Enter when done, Ctrl-C to abort")
raw_token = self.token_from_code(code["device_code"])
ref_token = RefreshingToken(OAuthToken(**raw_token), credentials=self)
if to_file:
ref_token.local_cache = to_file
return ref_token

def refresh_token(self, refresh_token: str) -> BaseTokenDict:
"""
Method for requesting a new access token for a given refresh_token.
Expand Down
87 changes: 0 additions & 87 deletions ytmusicapi/auth/oauth/refreshing.py

This file was deleted.

Loading