def get_access_token(request): code = request.GET['code'] clientid = get_config_value(request, "client_id") clientsecret = get_config_value(request, "client_secret") redirecturi = get_config_value(request, "redirect_uri") sparedirecturi = get_config_value(request, "spa_redirect_uri") authuri = get_config_value(request, "auth_uri") try: payload = { 'grant_type': 'authorization_code', 'client_id': clientid, 'client_secret': clientsecret, 'code': code, 'redirect_uri': redirecturi } headers = {"Accept": "application/json"} resp = requests.post(authuri + '/oauth/token', data=payload, headers=headers) resp.raise_for_status() respjson = resp.json() token = respjson['id_token'] return HTTPFound(location=sparedirecturi + token) except Exception as e: logger.exception(e) return None
def get_search(request): authorization_code = request.POST['authorization_code'] try: payload = { 'client_id': 'bc6a0e70dc379a6313ae', 'client_secret': '99ca1391ae9b8bc0c0a71628629e43ab32c17def', 'code': authorization_code } headers = {"Accept": "application/json"} resp = requests.post('https://github.com/login/oauth/access_token', data=payload, headers=headers) resp.raise_for_status() respjson = resp.json() return respjson except Exception as e: logger.exception(e) return None
def attachments_ping(request): """Heartbeat view for the attachments backend. :returns: ``True`` if succeeds to write and delete, ``False`` otherwise. """ # Do nothing if server is readonly. if asbool(request.registry.settings.get('readonly', False)): return True status = False attachment = request.attachment try: location = attachment.save_file(StringIO(HEARTBEAT_CONTENT), HEARTBEAT_FILENAME, replace=True) attachment.delete(location) status = True except Exception as e: logger.exception(e) return status
def timestamp(self): """Return the current collection timestamp. :rtype: int """ try: return self.model.timestamp() except storage_exceptions.BackendError as e: is_readonly = self.request.registry.settings['readonly'] if not is_readonly: raise e # If the instance is configured to be readonly, and if the # collection is empty, the backend will try to bump the timestamp. # It fails if the configured db user has not write privileges. logger.exception(e) error_msg = ("Collection timestamp cannot be written. " "Records endpoint must be hit at least once from a " "writable instance.") raise http_error(HTTPServiceUnavailable(), errno=ERRORS.BACKEND, message=error_msg)
def wrapped(*args, **kwargs): try: return func(*args, **kwargs) except redis.RedisError as e: logger.exception(e) raise exceptions.BackendError(original=e)