diff options
author | Vilem Duha <vilem.duha@gmail.com> | 2019-05-31 01:14:33 +0300 |
---|---|---|
committer | Vilem Duha <vilem.duha@gmail.com> | 2019-06-01 19:49:19 +0300 |
commit | 07d74a9b2e341c2eea77404b25d45791e3c46296 (patch) | |
tree | 5dad2159cb1c4850fd5ae44d8fafdb982e869544 /blenderkit | |
parent | 7f6405130af3e693a9d806d3ebd4cd414a59b245 (diff) |
BlenderKit: Further login and Oauth tweaks
Diffstat (limited to 'blenderkit')
-rw-r--r-- | blenderkit/bkit_oauth.py | 10 | ||||
-rw-r--r-- | blenderkit/oauth.py | 34 | ||||
-rw-r--r-- | blenderkit/paths.py | 6 | ||||
-rw-r--r-- | blenderkit/ui_panels.py | 15 |
4 files changed, 38 insertions, 27 deletions
diff --git a/blenderkit/bkit_oauth.py b/blenderkit/bkit_oauth.py index b68e982e..28ca2d89 100644 --- a/blenderkit/bkit_oauth.py +++ b/blenderkit/bkit_oauth.py @@ -46,14 +46,10 @@ def login_thread(signup=False): def login(signup): - if signup: - r_url = paths.BLENDERKIT_SIGNUP_URL - else: - r_url = paths.BLENDERKIT_LOGIN_URL + r_url = paths.get_oauth_landing_url() - authenticator = oauth.SimpleOAuthAuthenticator(server_url=paths.get_bkit_url(), client_id=CLIENT_ID, ports=PORTS, - redirect_url=r_url) - auth_token, refresh_token = authenticator.get_new_token() + authenticator = oauth.SimpleOAuthAuthenticator(server_url=paths.get_bkit_url(), client_id=CLIENT_ID, ports=PORTS) + auth_token, refresh_token = authenticator.get_new_token(register = signup, redirect_url=r_url) utils.p('tokens retrieved') tasks_queue.add_task((write_tokens, (auth_token, refresh_token))) diff --git a/blenderkit/oauth.py b/blenderkit/oauth.py index e2846dab..220dbed4 100644 --- a/blenderkit/oauth.py +++ b/blenderkit/oauth.py @@ -20,17 +20,16 @@ import json import webbrowser from http.server import BaseHTTPRequestHandler, HTTPServer -from urllib.parse import parse_qs, urlparse +from urllib.parse import parse_qs, quote as urlquote, urlparse import requests class SimpleOAuthAuthenticator(object): - def __init__(self, server_url, client_id, ports, redirect_url): + def __init__(self, server_url, client_id, ports): self.server_url = server_url self.client_id = client_id self.ports = ports - self.redirect_url = redirect_url def _get_tokens(self, authorization_code=None, refresh_token=None, grant_type="authorization_code"): data = { @@ -54,7 +53,7 @@ class SimpleOAuthAuthenticator(object): access_token = json.loads(response.content)['access_token'] return access_token, refresh_token - def get_new_token(self): + def get_new_token(self, register=True, redirect_url=None): class HTTPServerHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) @@ -63,14 +62,14 @@ class SimpleOAuthAuthenticator(object): if 'code' in self.path: self.auth_code = self.path.split('=')[1] # Display to the user that they no longer need the browser window - self.wfile.write(bytes( - '<html>' - '<head><meta http-equiv="refresh" content="0;url=%(redirect_url)s"></head>' - '<script> window.location.href="%(redirect_url)s"; </script>' - '<h1>You may now close this window.</h1>' - '</html>' % {'redirect_url': self.redirect_url}, - 'utf-8', - )) + if redirect_url: + redirect_string = ( + '<head><meta http-equiv="refresh" content="0;url=%(redirect_url)s"></head>' + '<script> window.location.href="%(redirect_url)s"; </script>' % {'redirect_url': redirect_url} + ) + else: + redirect_string = "" + self.wfile.write(bytes('<html>%s<h1>You may now close this window.</h1></html>' % redirect_string, 'utf-8')) qs = parse_qs(urlparse(self.path).query) self.server.authorization_code = qs['code'][0] @@ -80,10 +79,15 @@ class SimpleOAuthAuthenticator(object): except OSError: continue break - webbrowser.open_new( - "%s/o/authorize?client_id=%s&state=random_state_string&response_type=code&" - "redirect_uri=http://localhost:%s/consumer/exchange/" % (self.server_url, self.client_id, port), + authorize_url = ( + "/o/authorize?client_id=%s&state=random_state_string&response_type=code&" + "redirect_uri=http://localhost:%s/consumer/exchange/" % (self.client_id, port) ) + if register: + authorize_url = "%s/accounts/register/?next=%s" % (self.server_url, urlquote(authorize_url)) + else: + authorize_url = "%s%s" % (self.server_url, authorize_url) + webbrowser.open_new(authorize_url) httpServer.handle_request() authorization_code = httpServer.authorization_code diff --git a/blenderkit/paths.py b/blenderkit/paths.py index cb460ff1..6103ce8a 100644 --- a/blenderkit/paths.py +++ b/blenderkit/paths.py @@ -29,11 +29,12 @@ BLENDERKIT_MANUAL = "https://youtu.be/1hVgcQhIAo8" BLENDERKIT_MODEL_UPLOAD_INSTRUCTIONS_URL = "https://www.blenderkit.com/docs/upload/" BLENDERKIT_MATERIAL_UPLOAD_INSTRUCTIONS_URL = "https://www.blenderkit.com/docs/uploading-material/" BLENDERKIT_LOGIN_URL = "https://www.blenderkit.com/accounts/login" +BLENDERKIT_OAUTH_LANDING_URL = "/oauth-landing/" BLENDERKIT_SIGNUP_URL = "https://www.blenderkit.com/accounts/register" BLENDERKIT_ADDON_URL = "https://www.blenderkit.com/api/v1/assets/6923b215-7df0-46f3-95ae-a2b5ff44ddd5/" BLENDERKIT_ADDON_FILE_URL = "https://www.blenderkit.com/get-blenderkit/" -_presets = os.path.join(bpy.utils.user_resource('SCRIPTS'), "presets") BLENDERKIT_SETTINGS_FILENAME = os.path.join(_presets, "bkit.json") +_presets = os.path.join(bpy.utils.user_resource('SCRIPTS'), "presets") def get_bkit_url(): @@ -52,6 +53,9 @@ def get_bkit_url(): def get_api_url(): return get_bkit_url() + BLENDERKIT_API +def get_oauth_landing_url(): + return get_bkit_url() + BLENDERKIT_OAUTH_LANDING_URL + def default_global_dict(): from os.path import expanduser diff --git a/blenderkit/ui_panels.py b/blenderkit/ui_panels.py index b74e3bbe..197f208f 100644 --- a/blenderkit/ui_panels.py +++ b/blenderkit/ui_panels.py @@ -544,10 +544,17 @@ def draw_panel_brush_ratings(self, context): def draw_login_buttons(layout): - layout.operator("wm.blenderkit_login", text="Login", - icon='URL').signup = False - layout.operator("wm.blenderkit_login", text="Sign up", - icon='URL').signup = True + user_preferences = bpy.context.preferences.addons['blenderkit'].preferences + + if user_preferences.login_attempt: + layout.label(text='Login through browser') + layout.label(text='in progress.') + layout.operator("wm.blenderkit_login_cancel", text="Cancel", icon='CANCEL') + else: + layout.operator("wm.blenderkit_login", text="Login", + icon='URL').signup = False + layout.operator("wm.blenderkit_login", text="Sign up", + icon='URL').signup = True class VIEW3D_PT_blenderkit_unified(Panel): |