Skip to content
This repository was archived by the owner on Sep 15, 2021. It is now read-only.

Commit 7640a81

Browse files
committed
Opt move cookie init to backend and add optional ignore connection error
1 parent aec3401 commit 7640a81

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

django_crowd_auth/backends.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from django_crowd_auth.client import Client
99
from django_crowd_auth import user
10+
from requests.exceptions import ConnectionError
1011

1112

1213
LOGGER = logging.getLogger(__name__)
@@ -19,16 +20,25 @@ class Backend(ModelBackend):
1920
def authenticate(self, request, **credentials):
2021
"""Authenticate an user.
2122
"""
22-
client = Client.from_settings()
23-
remote_addr = request.META['REMOTE_ADDR']
24-
25-
if 'token' in credentials:
26-
session = client.validate_session(
27-
credentials['token'], remote_addr)
28-
elif 'username' in credentials and 'password' in credentials:
29-
session = client.get_session(
30-
credentials['username'], credentials['password'], remote_addr)
31-
else:
23+
24+
try:
25+
# Only try and init cookie config if it is None
26+
client = Client.from_settings()
27+
if client.cookie_config is None:
28+
client.init_cookie_config()
29+
30+
remote_addr = request.META['REMOTE_ADDR']
31+
if 'token' in credentials:
32+
session = client.validate_session(
33+
credentials['token'], remote_addr)
34+
elif 'username' in credentials and 'password' in credentials:
35+
session = client.get_session(
36+
credentials['username'], credentials['password'],
37+
remote_addr)
38+
except ConnectionError as ex:
39+
LOGGER.exception(ex)
40+
if getattr(settings, 'CROWD_RAISE_CONNECTION_ERROR', True):
41+
raise ex
3242
session = None
3343

3444
if session:

django_crowd_auth/client.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ class Client(crowd.CrowdServer):
66
"""Crowd client.
77
"""
88

9-
def get_cookie_config(self):
9+
cookie_config = None
10+
11+
def init_cookie_config(self):
1012
"""Get Crowd's cookie configuration.
1113
"""
1214
url = self.rest_url + '/config/cookie.json'
1315
response = self._get(url)
14-
1516
if response.ok:
16-
return response.json()
17+
Client.cookie_config = self.cookie_config = response.json()
18+
assert 'domain' in self.cookie_config, \
19+
'Missing crowd cookie config property domain'
20+
assert 'name' in self.cookie_config, \
21+
'Missing crowd cookie config property name'
22+
assert 'secure' in self.cookie_config, \
23+
'Missing crowd cookie config property secure'
1724

1825
@classmethod
1926
def from_settings(cls):

django_crowd_auth/middlewares.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313
def sso(get_response):
1414
"""Crowd SSO middleware.
1515
"""
16-
client = Client.from_settings()
17-
cookie_config = client.get_cookie_config()
18-
cookie_name = cookie_config['name']
19-
cookie_domain = cookie_config['domain']
20-
cookie_secure = cookie_config['secure']
21-
LOGGER.debug('Crowd cookie config %r', cookie_config)
2216

2317
def middleware(request):
2418
"""Authenticate users having a Crowd cookie.
2519
"""
20+
if Client.cookie_config is None:
21+
return get_response(request)
22+
23+
cookie_config = Client.cookie_config
24+
2625
crowd_session_last_validation = \
2726
request.session.get('crowd_session_last_validation')
2827

@@ -40,7 +39,7 @@ def middleware(request):
4039
request.user.username)
4140
logout(request)
4241

43-
cookie_token = request.COOKIES.get(cookie_name)
42+
cookie_token = request.COOKIES.get(cookie_config.get('name'))
4443

4544
if not request.user.is_authenticated and cookie_token:
4645
LOGGER.debug('Trying to auth from cookie %s', cookie_token)
@@ -60,13 +59,15 @@ def middleware(request):
6059
cookie_token = request.session['crowd_session_token']
6160

6261
response.set_cookie(
63-
key=cookie_name, value=cookie_token,
62+
key=cookie_config.get('name'), value=cookie_token,
6463
max_age=None, expires=crowd_session_expiry, path='/',
65-
domain=cookie_domain, secure=cookie_secure, httponly=True)
64+
domain=cookie_config.get('domain'),
65+
secure=cookie_config.get('secure'), httponly=True)
6666

6767
else:
6868
response.delete_cookie(
69-
key=cookie_name, path='/', domain=cookie_domain)
69+
key=cookie_config.get('name'), path='/',
70+
domain=cookie_config.get('domain'))
7071

7172
return response
7273

0 commit comments

Comments
 (0)