Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# then we'll report the error
if r.status_code == 410:
# Code of 410 means the user has unsubscribed from notifs. This means we
# must remove the device
PushDevice.query.\
filter_by(endpoint=endpoint).\
delete()
db.session.commit()
elif r.status_code // 100 != 2:
try:
rejection_response = r.text
except:
rejection_response = 'Error reading rejection response'
if bugsnag.configuration.api_key is not None:
bugsnag.notify(
Exception("Web Push dispatch error"),
meta_data={
'webpush_request': {
'endpoint': f'{endpoint_url.scheme}://{endpoint_url.netloc}',
'status_code': r.status_code
},
'webpush_response': {
'response': rejection_response
}
}
)
server.logger.error(f'Notification (Web Push) rejected {notification.uuid} -> {endpoint_url.netloc}:\n{rejection_response}')
# that response will be exactly 200 is successful, any other
# response indicates otherwise
if response_status != 200:
try:
reason = json_loads(response_body)['reason']
# If reason is 'BadDeviceToken' this means the user has likely
# unsubscribed so we'll remove their device subscription
if reason == 'BadDeviceToken':
db.session.delete(device)
db.session.commit()
except ValueError:
reason = "unknown"
if bugsnag.configuration.api_key is not None:
bugsnag.notify(
Exception("APNS dispatch error"),
meta_data={"apns_rejection": {"reason": reason}}
)
server.logger.error(f'Notification (APNS) rejected {notification.uuid} -> {device.device_id}:\n{reason}')
return abort(404)
json = request.get_json(silent=True)
if json is None:
bugsnag.notify(
Exception("WebAPN exception"),
meta_data={"webapn_logs": {f"data": request.data}}
)
return ('', 204)
logs = json["logs"]
if server.debug:
print(json_dumps(logs))
if bugsnag.configuration.api_key is not None:
bugsnag.notify(
Exception("WebAPN exception"),
meta_data={"webapn_logs": {f"Log {i}": log for i, log in enumerate(logs)}}
)
return ('', 204)
# users of the provider.
access_token_response = requests.post(
oauth_callback,
data={
'code': code,
'redirect_uri': canonical_host + url_for('auth_login_oauth'),
'client_id': oauth_id,
'client_secret': oauth_secret,
'grant_type': 'authorization_code'
},
headers={
'Accept': 'application/json'
}
).json()
except Exception as error:
if bugsnag.configuration.api_key is not None:
bugsnag.notify(error, meta_data={'oauth':{'provider':provider}})
# Errors mean we couldn't get access key
return render_error('Failed to connect to OAuth provider.'), 403
try:
auth_key = access_token_response['access_token']
except Exception as error:
if bugsnag.configuration.api_key is not None:
bugsnag.notify(error, meta_data={'oauth':{'provider':provider, 'data':access_token_response}})
# Errors mean we couldn't get access key
return render_error('Could not obtain OAuth access token from OAuth provider.'), 403
is_append_flow = auth_opts.get('append', False)
def __init__(self, application, environ, start_response):
self.environ = environ
bugsnag.configure_request(wsgi_environ=self.environ)
try:
if bugsnag.configuration.auto_capture_sessions:
bugsnag.start_session()
self.app = application(environ, start_response)
except Exception as e:
bugsnag.auto_notify(
e,
severity_reason=self.SEVERITY_REASON
)
raise
def prepare(self):
bugsnag.configure().runtime_versions['tornado'] = tornado.version
if bugsnag.configuration.auto_capture_sessions:
bugsnag.start_session()
def __track_session(sender, **extra):
if bugsnag.configuration.auto_capture_sessions:
bugsnag.start_session()
def __track_session(sender, **extra):
if bugsnag.configuration.auto_capture_sessions:
bugsnag.start_session()
# Errors mean we couldn't get access key
return render_error('Could not obtain OAuth access token from OAuth provider.'), 403
is_append_flow = auth_opts.get('append', False)
# If we're client-side we'll stop here UNLESS we are appending
if client_side and not is_append_flow:
return auth_key
try:
# Get identity key, this is something that allows us
# to uniquely identify the user.
# The profile['identification'] is a user-readable string.
oauth_identity, profile = oauth_login(auth_key)
except Exception as error:
if bugsnag.configuration.api_key is not None:
bugsnag.notify(
error,
meta_data={
'oauth': {
'provider': provider
}
}
)
# If we get here that means we could not get profile
# this is our fault since we validated
return render_error('Could not obtain OAuth profile using provider implementation.'), 500
if 'identifier' not in profile:
return render_error('Could not obtain identifier'), 400