pep8ness
This commit is contained in:
parent
27642283cc
commit
ecd2af964f
|
@ -1,16 +1,15 @@
|
|||
import uuid
|
||||
import requests
|
||||
import logging
|
||||
import base64
|
||||
import json
|
||||
|
||||
|
||||
from requests_oauthlib import OAuth2Session
|
||||
|
||||
|
||||
from django.views.generic import TemplateView, View, FormView
|
||||
from django.views.generic import View, FormView
|
||||
from django.views.generic.detail import SingleObjectMixin
|
||||
from django.http import HttpResponse, HttpResponseRedirect, Http404
|
||||
from django.http import HttpResponseRedirect, Http404
|
||||
from django.contrib.auth import authenticate, REDIRECT_FIELD_NAME
|
||||
from django.contrib.auth.decorators import user_passes_test
|
||||
from django.contrib import messages
|
||||
|
@ -31,7 +30,7 @@ from . import app_settings, models, utils
|
|||
|
||||
|
||||
SET_PASSWORD_FORM_CLASS = a2_utils.import_module_or_class(
|
||||
a2_app_settings.A2_REGISTRATION_SET_PASSWORD_FORM_CLASS)
|
||||
a2_app_settings.A2_REGISTRATION_SET_PASSWORD_FORM_CLASS)
|
||||
|
||||
|
||||
def user_has_fcaccount(user):
|
||||
|
@ -96,9 +95,10 @@ def resolve_access_token(authorization_code, redirect_uri, logger):
|
|||
}
|
||||
logger.debug('data %s', data)
|
||||
try:
|
||||
response = requests.post(app_settings.token_url, data=data,
|
||||
verify=app_settings.verify_certificate,
|
||||
allow_redirects=False, timeout=3)
|
||||
response = requests.post(
|
||||
app_settings.token_url, data=data,
|
||||
verify=app_settings.verify_certificate,
|
||||
allow_redirects=False, timeout=3)
|
||||
if response.status_code == 400:
|
||||
try:
|
||||
data = response.json()
|
||||
|
@ -117,8 +117,8 @@ def resolve_access_token(authorization_code, redirect_uri, logger):
|
|||
logger.debug('token resolved : {}'.format(response))
|
||||
return response
|
||||
except ValueError:
|
||||
logger.warning("no JSON object can be decoded from "
|
||||
"the data received from {} : '{}'".format(
|
||||
logger.warning(
|
||||
"no JSON object can be decoded from the data received from {} : '{}'".format(
|
||||
app_settings.token_url, response.content))
|
||||
|
||||
|
||||
|
@ -184,7 +184,8 @@ class FcOAuthSessionViewMixin(LoggerMixin):
|
|||
old_next_url = self.redirect_to(request, *args, **kwargs)
|
||||
here = '{0}?{1}'.format(
|
||||
request.path, urlencode({REDIRECT_FIELD_NAME: old_next_url}))
|
||||
there = '{0}{2}{1}'.format(next_url, urlencode({REDIRECT_FIELD_NAME: here}),
|
||||
there = '{0}{2}{1}'.format(
|
||||
next_url, urlencode({REDIRECT_FIELD_NAME: here}),
|
||||
'&' if '?' in next_url else '?')
|
||||
return self.redirect(request, next_url=there, *args, **kwargs)
|
||||
|
||||
|
@ -193,26 +194,23 @@ class FcOAuthSessionViewMixin(LoggerMixin):
|
|||
|
||||
def get_ressource(self, url, verify):
|
||||
try:
|
||||
data = self.oauth_session().get(url, verify=verify,
|
||||
allow_redirects=False, timeout=3)
|
||||
data = self.oauth_session().get(url, verify=verify, allow_redirects=False, timeout=3)
|
||||
data.raise_for_status()
|
||||
except requests.exceptions.RequestException as e:
|
||||
self.logger.error(u'unable to retrieve ressource from {} '
|
||||
'due to {}'.format(url, e))
|
||||
self.logger.error(u'unable to retrieve ressource from {} due to {}'.format(url, e))
|
||||
else:
|
||||
try:
|
||||
data = data.json()
|
||||
self.logger.debug('ressource resolved : {}'.format(data))
|
||||
return data
|
||||
except ValueError:
|
||||
self.logger.warning("no JSON object can be decoded from "
|
||||
"the data received from {} : '{}'".format(
|
||||
self.logger.warning(
|
||||
"no JSON object can be decoded from the data received from {} : '{}'".format(
|
||||
url, data.content))
|
||||
|
||||
|
||||
def get_user_info(self):
|
||||
return self.get_ressource(app_settings.userinfo_url + '?schema=openid',
|
||||
app_settings.verify_certificate)
|
||||
app_settings.verify_certificate)
|
||||
|
||||
def get_data(self, scopes=[]):
|
||||
data = dict()
|
||||
|
@ -248,7 +246,7 @@ class FcOAuthSessionViewMixin(LoggerMixin):
|
|||
# The token request may fail, 'error' is then required.
|
||||
# A bad client secret results in error equals to invalid_request
|
||||
# for FC and invalid_client for oidc_provider.
|
||||
if 'error' in self.token :
|
||||
if 'error' in self.token:
|
||||
msg = 'token request failed : {}'.format(self.token)
|
||||
self.logger.warning(msg)
|
||||
messages.warning(request, _('Unable to connect to FranceConnect.'))
|
||||
|
@ -319,8 +317,9 @@ class LoginOrLinkView(PopupViewMixin, FcOAuthSessionViewMixin, View):
|
|||
if fc_account.user is not request.user:
|
||||
msg = 'Attempt to link FC account {} already linked with user {}'
|
||||
self.logger.info(msg.format(self.sub, fc_account.user))
|
||||
messages.info(request, _('The FranceConnect account {} is '
|
||||
'already linked with another account.').format(fc_account))
|
||||
messages.info(request,
|
||||
_('The FranceConnect account {} is already'
|
||||
' linked with another account.').format(fc_account))
|
||||
return self.redirect(request)
|
||||
except models.FcAccount.DoesNotExist:
|
||||
pass
|
||||
|
@ -336,11 +335,13 @@ class LoginOrLinkView(PopupViewMixin, FcOAuthSessionViewMixin, View):
|
|||
self.update_user_info()
|
||||
data = utils.get_mapped_attributes_flat(request)
|
||||
if 'email' in data:
|
||||
messages.info(request, _('Your FranceConnect account {} with email {} has '
|
||||
'been linked.').format(self.fc_account, data['email']))
|
||||
messages.info(request,
|
||||
_('Your FranceConnect account {} with '
|
||||
'email {} has been linked.').format(self.fc_account,
|
||||
data['email']))
|
||||
else:
|
||||
messages.info(request, _('Your FranceConnect account {} has '
|
||||
'been linked.').format(self.fc_account))
|
||||
messages.info(request, _('Your FranceConnect account {} '
|
||||
'has been linked.').format(self.fc_account))
|
||||
else:
|
||||
self.fc_account.token = json_token
|
||||
self.fc_account.save()
|
||||
|
@ -365,7 +366,8 @@ class LoginOrLinkView(PopupViewMixin, FcOAuthSessionViewMixin, View):
|
|||
if app_settings.show_button_quick_account_creation:
|
||||
return self.redirect_and_come_back(request, settings.LOGIN_URL)
|
||||
else:
|
||||
return self.redirect_and_come_back(request, '{0}?nofc=1'.format(settings.LOGIN_URL))
|
||||
return self.redirect_and_come_back(request,
|
||||
'{0}?nofc=1'.format(settings.LOGIN_URL))
|
||||
|
||||
|
||||
class RegistrationView(LoggerMixin, View):
|
||||
|
|
Loading…
Reference in New Issue