Skip to content

Commit 76983a4

Browse files
authored
Merge pull request #323 from matteofuso/oauth-webbrowser-response
Fix OAuth callback server to return a response to the browser
2 parents 8760279 + ccf625c commit 76983a4

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,22 @@ session = Session.Builder() \
7676
.create()
7777
```
7878

79-
#### With auth url callback
79+
#### With auth url callback and changing the content of the success page
8080

8181
```python
8282
from librespot.core import Session
83+
import webbrowser
8384

8485
# This will pass the auth url to the method
8586

8687
def auth_url_callback(url):
87-
print(url)
88+
webbrowser.open(url)
89+
90+
# This is the response sent to the browser once the flow has been completed successfully
91+
success_page = "<html><body><h1>Login Successful</h1><p>You can close this window now.</p><script>setTimeout(() => {window.close()}, 100);</script></body></html>"
8892

8993
session = Session.Builder() \
90-
.oauth(auth_url_callback) \
94+
.oauth(auth_url_callback, success_page) \
9195
.create()
9296
```
9397

librespot/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,7 @@ def stored_file(self,
16381638
pass
16391639
return self
16401640

1641-
def oauth(self, oauth_url_callback) -> Session.Builder:
1641+
def oauth(self, oauth_url_callback, success_page_content = None) -> Session.Builder:
16421642
"""
16431643
Login via OAuth
16441644
@@ -1647,7 +1647,7 @@ def oauth(self, oauth_url_callback) -> Session.Builder:
16471647
"""
16481648
if os.path.isfile(self.conf.stored_credentials_file):
16491649
return self.stored_file(None)
1650-
self.login_credentials = OAuth(MercuryRequests.keymaster_client_id, "http://127.0.0.1:5588/login", oauth_url_callback).flow()
1650+
self.login_credentials = OAuth(MercuryRequests.keymaster_client_id, "http://127.0.0.1:5588/login", oauth_url_callback).set_success_page_content(success_page_content).flow()
16511651
return self
16521652

16531653
def user_pass(self, username: str, password: str) -> Session.Builder:

librespot/oauth.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ class OAuth:
2222
__token = ""
2323
__server = None
2424
__oauth_url_callback = None
25+
__success_page_content = None
2526

2627
def __init__(self, client_id, redirect_url, oauth_url_callback):
2728
self.__client_id = client_id
2829
self.__redirect_url = redirect_url
2930
self.__oauth_url_callback = oauth_url_callback
31+
32+
def set_success_page_content(self, content):
33+
self.__success_page_content = content
34+
return self
3035

3136
def __generate_generate_code_verifier(self):
3237
possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
@@ -76,22 +81,34 @@ def get_credentials(self):
7681
class CallbackServer(HTTPServer):
7782
callback_path = None
7883

79-
def __init__(self, server_address, RequestHandlerClass, callback_path, set_code):
84+
def __init__(self, server_address, RequestHandlerClass, callback_path, set_code, success_page_content):
8085
self.callback_path = callback_path
8186
self.set_code = set_code
87+
self.success_page_content = success_page_content
8288
super().__init__(server_address, RequestHandlerClass)
8389

8490
class CallbackRequestHandler(BaseHTTPRequestHandler):
8591
def do_GET(self):
8692
if(self.path.startswith(self.server.callback_path)):
8793
query = urllib.parse.parse_qs(urlparse(self.path).query)
8894
if not query.__contains__("code"):
95+
self.send_response(400)
96+
self.send_header('Content-type', 'text/html')
97+
self.end_headers()
8998
self.wfile.write(b"Request doesn't contain 'code'")
9099
return
91100
self.server.set_code(query.get("code")[0])
92-
self.wfile.write(b"librespot-python received callback")
101+
self.send_response(200)
102+
self.send_header('Content-type', 'text/html')
103+
self.end_headers()
104+
success_page = self.server.success_page_content or "librespot-python received callback"
105+
self.wfile.write(success_page.encode('utf-8'))
93106
pass
94107

108+
# Suppress logging
109+
def log_message(self, format, *args) -> None:
110+
return
111+
95112
def __start_server(self):
96113
try:
97114
self.__server.handle_request()
@@ -106,7 +123,8 @@ def run_callback_server(self):
106123
(url.hostname, url.port),
107124
self.CallbackRequestHandler,
108125
url.path,
109-
self.set_code
126+
self.set_code,
127+
self.__success_page_content,
110128
)
111129
logging.info("OAuth: Waiting for callback on %s", url.hostname + ":" + str(url.port))
112130
self.__start_server()

0 commit comments

Comments
 (0)