1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
20 from blenderkit
import tasks_queue
, utils
, paths
, search
, categories
, oauth
, ui
, ui_panels
28 bk_logger
= logging
.getLogger('blenderkit')
30 from bpy
.props
import (
34 CLIENT_ID
= "IdFRwa3SGA8eMpzhRVFMg5Ts8sPK93xBjif93x0F"
35 PORTS
= [62485, 65425, 55428, 49452]
37 active_authenticator
= None
40 def login_thread(signup
=False):
41 global active_authenticator
42 r_url
= paths
.get_oauth_landing_url()
43 url
= paths
.get_bkit_url()
44 authenticator
= oauth
.SimpleOAuthAuthenticator(server_url
=url
, client_id
=CLIENT_ID
, ports
=PORTS
)
45 # we store authenticator globally to be able to ping the server if connection fails.
46 active_authenticator
= authenticator
47 thread
= threading
.Thread(target
=login
, args
=([signup
, url
, r_url
, authenticator
]), daemon
=True)
51 def login(signup
, url
, r_url
, authenticator
):
52 auth_token
, refresh_token
, oauth_response
= authenticator
.get_new_token(register
=signup
, redirect_url
=r_url
)
53 bk_logger
.debug('tokens retrieved')
54 tasks_queue
.add_task((write_tokens
, (auth_token
, refresh_token
, oauth_response
)))
57 def refresh_token_thread():
58 preferences
= bpy
.context
.preferences
.addons
['blenderkit'].preferences
59 if len(preferences
.api_key_refresh
) > 0 and preferences
.refresh_in_progress
== False:
60 preferences
.refresh_in_progress
= True
61 url
= paths
.get_bkit_url()
62 thread
= threading
.Thread(target
=refresh_token
, args
=([preferences
.api_key_refresh
, url
]), daemon
=True)
65 ui
.add_report('Already Refreshing token, will be ready soon. If this fails, please login again in Login panel.')
68 def refresh_token(api_key_refresh
, url
):
69 authenticator
= oauth
.SimpleOAuthAuthenticator(server_url
=url
, client_id
=CLIENT_ID
, ports
=PORTS
)
70 auth_token
, refresh_token
, oauth_response
= authenticator
.get_refreshed_token(api_key_refresh
)
71 if auth_token
is not None and refresh_token
is not None:
72 tasks_queue
.add_task((write_tokens
, (auth_token
, refresh_token
, oauth_response
)))
73 return auth_token
, refresh_token
, oauth_response
76 def write_tokens(auth_token
, refresh_token
, oauth_response
):
77 bk_logger
.debug('writing tokens')
78 preferences
= bpy
.context
.preferences
.addons
['blenderkit'].preferences
79 preferences
.api_key_refresh
= refresh_token
80 preferences
.api_key
= auth_token
81 preferences
.api_key_timeout
= time
.time() + oauth_response
['expires_in']
82 preferences
.api_key_life
= oauth_response
['expires_in']
83 preferences
.login_attempt
= False
84 preferences
.refresh_in_progress
= False
85 props
= utils
.get_search_props()
88 ui
.add_report('BlenderKit Re-Login success')
90 categories
.fetch_categories_thread(auth_token
, force
= False)
93 class RegisterLoginOnline(bpy
.types
.Operator
):
94 """Login online on BlenderKit webpage"""
96 bl_idname
= "wm.blenderkit_login"
97 bl_label
= "BlenderKit login/signup"
98 bl_options
= {'REGISTER', 'UNDO'}
100 signup
: BoolProperty(
101 name
="create a new account",
102 description
="True for register, otherwise login",
104 options
={'SKIP_SAVE'}
107 message
: bpy
.props
.StringProperty(
110 default
="You were logged out from BlenderKit.\n Clicking OK takes you to web login. ")
113 def poll(cls
, context
):
116 def draw(self
, context
):
118 utils
.label_multiline(layout
, text
=self
.message
, width
= 300)
120 def execute(self
, context
):
121 preferences
= bpy
.context
.preferences
.addons
['blenderkit'].preferences
122 preferences
.login_attempt
= True
123 login_thread(self
.signup
)
126 def invoke(self
, context
, event
):
127 wm
= bpy
.context
.window_manager
128 preferences
= bpy
.context
.preferences
.addons
['blenderkit'].preferences
129 preferences
.api_key_refresh
= ''
130 preferences
.api_key
= ''
131 return wm
.invoke_props_dialog(self
)
134 class Logout(bpy
.types
.Operator
):
135 """Logout from BlenderKit immediately"""
137 bl_idname
= "wm.blenderkit_logout"
138 bl_label
= "BlenderKit logout"
139 bl_options
= {'REGISTER', 'UNDO'}
142 def poll(cls
, context
):
145 def execute(self
, context
):
146 preferences
= bpy
.context
.preferences
.addons
['blenderkit'].preferences
147 preferences
.login_attempt
= False
148 preferences
.api_key_refresh
= ''
149 preferences
.api_key
= ''
150 if bpy
.context
.window_manager
.get('bkit profile'):
151 del (bpy
.context
.window_manager
['bkit profile'])
155 class CancelLoginOnline(bpy
.types
.Operator
):
156 """Cancel login attempt"""
158 bl_idname
= "wm.blenderkit_login_cancel"
159 bl_label
= "BlenderKit login cancel"
160 bl_options
= {'REGISTER', 'UNDO'}
163 def poll(cls
, context
):
166 def execute(self
, context
):
167 global active_authenticator
168 preferences
= bpy
.context
.preferences
.addons
['blenderkit'].preferences
169 preferences
.login_attempt
= False
171 if active_authenticator
is not None:
172 requests
.get(active_authenticator
.redirect_uri
)
173 active_authenticator
= None
174 except Exception as e
:
175 print('stopped login attempt')
189 bpy
.utils
.register_class(c
)
194 bpy
.utils
.unregister_class(c
)