def callback(self): try: oauth2helper = oauth2.OAuth2Helper() token = oauth2helper.get_token() user_name = oauth2helper.identify(token) oauth2helper.remember(user_name) oauth2helper.update_token(user_name, token) oauth2helper.redirect_from_callback() except Exception as e: # If the callback is called with an error, we must show the message error_description = toolkit.request.GET.get('error_description') if not error_description: if e.message: error_description = e.message elif hasattr(e, 'description') and e.description: error_description = e.description elif hasattr(e, 'error') and e.error: error_description = e.error else: error_description = type(e).__name__ toolkit.response.status_int = 302 redirect_url = oauth2.get_came_from( toolkit.request.params.get('state')) redirect_url = '/' if redirect_url == constants.INITIAL_PAGE else redirect_url toolkit.response.location = redirect_url helpers.flash_error(error_description)
def __init__(self, name=None): '''Store the OAuth 2 client configuration''' log.debug('Init OAuth2 extension') self.register_url = config.get('ckan.oauth2.register_url', None) self.reset_url = config.get('ckan.oauth2.reset_url', None) self.edit_url = config.get('ckan.oauth2.edit_url', None) self.authorization_header = config.get( 'ckan.oauth2.authorization_header', 'Authorization') self.oauth2helper = oauth2.OAuth2Helper()
def oauth_login(self): log.debug('Oauth login') oauth2helper = oauth2.OAuth2Helper() # Log in attemps are fired when the user is not logged in and they click # on the log in button # Get the page where the user was when the loggin attemp was fired # When the user is not logged in, he/she should be redirected to the dashboard when # the system cannot get the previous page came_from_url = self._get_previous_page(constants.INITIAL_PAGE) oauth2helper.challenge(came_from_url)
def identify(self): log.debug('identify') oauth2helper = oauth2.OAuth2Helper() authorization_header = config.get( 'ckanext.oauth2.authorization_header', 'Authorization') # Create session if it does not exist. Workaround to show flash messages session.save() def _refresh_and_save_token(user_name): new_token = oauth2helper.refresh_token(user_name) if new_token: toolkit.c.usertoken = new_token environ = toolkit.request.environ apikey = toolkit.request.headers.get(authorization_header, '') user_name = None # This API Key is not the one of CKAN, it's the one provided by the OAuth2 Service if apikey: try: token = {'access_token': apikey} user_name = oauth2helper.identify(token) except Exception: pass # If the authentication via API fails, we can still log in the user using session. if user_name is None and 'repoze.who.identity' in environ: user_name = environ['repoze.who.identity']['repoze.who.userid'] log.info('User %s logged using session' % user_name) # If we have been able to log in the user (via API or Session) if user_name: toolkit.c.user = user_name toolkit.c.usertoken = oauth2helper.get_stored_token(user_name) toolkit.c.usertoken_refresh = partial(_refresh_and_save_token, user_name) else: log.warn('The user is not currently logged...')
def __init__(self): self.oauth2helper = oauth2.OAuth2Helper()
def __init__(self, name=None): '''Store the OAuth 2 client configuration''' log.debug('Init OAuth2 extension') self.oauth2helper = oauth2.OAuth2Helper()
def __init__(self, name=None): """Store the OAuth 2 client configuration""" log.debug("Init OAuth2 extension") self.oauth2helper = oauth2.OAuth2Helper()