Skip to content

Commit

Permalink
Merge branch 'auth' into 'master'
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonVanAssche committed Nov 17, 2024
2 parents 830a3bc + 22ba26d commit 9c0835e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.spec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%define _version 0.3.1
%define _version 0.3.2
%define _release 1

Name: jamjar
Expand Down
2 changes: 1 addition & 1 deletion src/jamjar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
syncing, exporting, and managing playlists in the JamJar database.
"""

__version__ = "0.3.1"
__version__ = "0.3.2"

import json
import os
Expand Down
18 changes: 18 additions & 0 deletions src/jamjar/cli/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,12 @@ def refresh_token(self):
response = requests.post(self.token_url, data=body, timeout=10)
response.raise_for_status()
new_token_info = response.json()
new_token_info["refresh_token"] = token_info["refresh_token"]
new_token_info["expires_at"] = datetime.now().timestamp() + new_token_info["expires_in"]

self.verify_token(new_token_info)
self.save_token(new_token_info)

return new_token_info

except requests.exceptions.RequestException as e:
Expand All @@ -155,6 +159,20 @@ def refresh_token(self):
print(f"Error refreshing token: {e}")
raise

def verify_token(self, token_info):
"""Verifies the validity of the access token."""
try:
headers = {"Authorization": f"Bearer {token_info['access_token']}"}
response = requests.get("https://api.spotify.com/v1/me", headers=headers, timeout=10)
if response.status_code != 200:
raise RuntimeError("Token verification failed. Please log in again.")
except requests.exceptions.RequestException as e:
print(f"Error verifying token: {e}")
raise
except Exception as e:
print(f"Error processing token verification response: {e}")
raise

def save_token(self, token_info):
"""Saves token info to a file."""
try:
Expand Down

0 comments on commit 9c0835e

Please sign in to comment.