basecamp-mcp-server 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
auth_manager.py ADDED
@@ -0,0 +1,70 @@
1
+ """
2
+ Auth manager for Basecamp MCP server.
3
+ Handles token refresh logic automatically.
4
+ """
5
+
6
+ import logging
7
+ import os
8
+ import token_storage
9
+ from basecamp_oauth import BasecampOAuth
10
+ from datetime import datetime
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+ def ensure_authenticated():
15
+ """
16
+ Checks if the current token is valid and refreshes it if necessary.
17
+ Returns:
18
+ bool: True if authenticated (or successfully refreshed), False otherwise.
19
+ """
20
+ token_data = token_storage.get_token()
21
+
22
+ if not token_data or not token_data.get('access_token'):
23
+ logger.error("No token data found. Initial authentication required.")
24
+ return False
25
+
26
+ if not token_storage.is_token_expired():
27
+ logger.debug("Token is still valid.")
28
+ return True
29
+
30
+ # Token is expired, try to refresh
31
+ refresh_token = token_data.get('refresh_token')
32
+ if not refresh_token:
33
+ logger.error("Token expired and no refresh token available.")
34
+ return False
35
+
36
+ logger.info("Token expired. Attempting automatic refresh...")
37
+
38
+ try:
39
+ oauth = BasecampOAuth()
40
+ new_token_data = oauth.refresh_token(refresh_token)
41
+
42
+ # Basecamp refresh response usually contains access_token, expires_in.
43
+ # It may or may not contain a new refresh_token.
44
+ new_access_token = new_token_data.get('access_token')
45
+ new_refresh_token = new_token_data.get('refresh_token') or refresh_token
46
+ expires_in = new_token_data.get('expires_in')
47
+
48
+ # Maintain the account_id we already had
49
+ account_id = token_data.get('account_id')
50
+
51
+ token_storage.store_token(
52
+ access_token=new_access_token,
53
+ refresh_token=new_refresh_token,
54
+ expires_in=expires_in,
55
+ account_id=account_id
56
+ )
57
+
58
+ logger.info("Successfully refreshed and stored new tokens.")
59
+ return True
60
+ except Exception as e:
61
+ logger.error(f"Failed to refresh token: {e}")
62
+ return False
63
+
64
+ if __name__ == "__main__":
65
+ # Can be run as a standalone script to manually force a refresh check
66
+ logging.basicConfig(level=logging.INFO)
67
+ if ensure_authenticated():
68
+ print("Authenticated!")
69
+ else:
70
+ print("Authentication failed.")