def menbib_oauth_finish(**kwargs): user = get_current_user() if not user: raise HTTPError(http.FORBIDDEN) node = Node.load(session.data.get('menbib_auth_nid')) result = finish_auth() user.add_addon('menbib') user.save() user_settings = user.get_addon('menbib') user_settings.owner = user user_settings.access_token = result.access_token user_settings.refresh_token = result.refresh_token user_settings.token_type = result.token_type user_settings.expires_in = result.expires_in user_settings.save() flash('Successfully authorized Mendeley', 'success') if node: del session.data['menbib_auth_nid'] if node.has_addon('menbib'): node_addon = node.get_addon('menbib') node_addon.set_user_auth(user_settings) node_addon.save() return redirect(node.web_url_for('node_setting')) return redirect(web_url_for('user_addons'))
def menbib_oauth_start(**kwargs): user = get_current_user() nid = kwargs.get('pid') or kwargs.get('nid') if nid: session.data['menbib_auth_nid'] = nid if not user: raise HTTPError(http.FORBIDDEN) if user.has_addon('menbib') and user.get_addon('menbib').has_auth: flash('You have already authorized Mendeley for this account', 'warning') return redirect(web_url_for('user_addons')) return redirect(get_auth_flow())
def mendeley_oauth_start(*args, **kwargs): user = get_current_user() nid = kwargs.get('nid') or kwargs.get('pid') node = models.Node.load(nid) if nid else None # Fail if node provided and user not contributor if node and not node.is_contributor(user): raise HTTPError(http.FORBIDDEN) user.add_addon('mendeley') mendeley_user = user.get_addon('mendeley') if node: mendeley_node = node.get_addon('mendeley') mendeley_node.user_settings = mendeley_user # Add webhook if mendeley_node.user and mendeley_node.repo: mendeley_node.add_hook() mendeley_node.save() authorization_url, state = oauth_start_url(user, node) mendeley_user.oauth_state = state mendeley_user.save() return redirect(authorization_url)
def mendeley_oauth_callback(*args, **kwargs): user = models.User.load(request.args.get('state')) node = models.Node.load(kwargs.get('nid')) if user is None: raise HTTPError(http.NOT_FOUND) if kwargs.get('nid') and not node: raise HTTPError(http.NOT_FOUND) mendeley_user = user.get_addon('mendeley') if mendeley_user is None: raise HTTPError(http.BAD_REQUEST) if mendeley_user.oauth_state != request.args.get('state'): raise HTTPError(http.BAD_REQUEST) mendeley_node = node.get_addon('mendeley') if node else None code = request.args.get('code') if code is None: raise HTTPError(http.BAD_REQUEST) token = oauth_get_token(code,user) mendeley_user.oauth_state = None mendeley_user.oauth_access_token = token['access_token'] mendeley_user.oauth_refresh_token = token['refresh_token'] mendeley_user.oauth_token_type = token['token_type'] mendeley_user.oauth_token_expires = token['expires_in'] mendeley_user.oauth_token = token connect = Mendeley.from_settings(mendeley_user) user = connect.user() user_name = user['main']['url'].rsplit('/', 2)[1] mendeley_user.mendeley_user = user_name mendeley_user.save() if mendeley_node: mendeley_node.user_settings = mendeley_user mendeley_node.save() if node: return redirect(os.path.join(node.url, 'settings')) return redirect('/settings/')