Skip to content

Commit

Permalink
develop csrf debug false work #23
Browse files Browse the repository at this point in the history
  • Loading branch information
philipsahli committed Mar 31, 2018
1 parent 981ab90 commit 209b0eb
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 23 deletions.
8 changes: 6 additions & 2 deletions tumbo/aaa/cas/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from core.models import Base
from core.utils import read_jwt
import django.contrib.sessions.backends.cache

User = get_user_model()

Expand All @@ -20,10 +21,11 @@
def cas_login(function):
def wrapper(request, *args, **kwargs):
# logger.debug("authenticate %s" % request.user)
user=request.user
user = request.user

# if logged in
if request.user.is_authenticated():
#import pdb; pdb.set_trace()
logger.info("user.is_authenticated with user %s" % request.user.username)
logger.info("user has internalid: %s" % request.user.authprofile.internalid)
return function(request, *args, **kwargs)
Expand All @@ -49,7 +51,8 @@ def wrapper(request, *args, **kwargs):
cas_ticketverify=reverse('cas-ticketverify')
cas_ticketverify+="?ticket=%s&service=%s" % (ticket, service_full)
host = urlparse(request.build_absolute_uri()).netloc
response = requests.get("https://%s%s" % (host, cas_ticketverify))
# TODO: normally with https
response = requests.get("http://%s%s" % (host, cas_ticketverify))
logger.info("Response from verify: " + str(response.status_code))
logger.info("Response from verify: " + response.text)

Expand All @@ -63,6 +66,7 @@ def wrapper(request, *args, **kwargs):
auth_login(request, user)

request.session['cookie_path'] = "/userland/%s/%s" % (base.user.username, base.name)
logger.info("Setting cookie_path to: " % request.session['cookie_path'])
request.session.cycle_key()

# user is logged in successfully, redirect to service URL
Expand Down
3 changes: 3 additions & 0 deletions tumbo/aaa/cas/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from aaa.cas.models import Ticket
from core.models import AuthProfile


logger = logging.getLogger(__name__)
Expand All @@ -14,6 +15,8 @@ def create_ticket(backend, user, response, *args, **kwargs):
logger.info("create_ticket pipeline for user %s started" % user.username)

# workaround for creating internalid
auth, created = AuthProfile.objects.get_or_create(user=user)
user.authprofile = auth
user.authprofile.internalid = user.authprofile.internalid
user.authprofile.save()

Expand Down
2 changes: 2 additions & 0 deletions tumbo/aaa/cas/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.shortcuts import redirect, render
from django.http import HttpResponse
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt

from core.utils import create_jwt
from core.models import Base
Expand All @@ -22,6 +23,7 @@

logger = logging.getLogger(__name__)

@csrf_exempt
def loginpage(request):
"""
If a user wants to login, he opens the url named `cas-login`, which renders the cas_loginpage.html.
Expand Down
11 changes: 7 additions & 4 deletions tumbo/aaa/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
from django.conf import settings

def _is_member(user, group):
print user, group
print user.groups.filter(name=group).exists()
return user.groups.filter(name=group).exists()

def restrict_user(backend, user, response, *args, **kwargs):
print backend
if user.is_superuser: return

group = getattr(settings, "SOCIAL_AUTH_USER_GROUP", None)
if group:
if not _is_member(user, group):
return HttpResponse("Login forbidden.")
#group = getattr(settings, "SOCIAL_AUTH_USER_GROUP", None)
#if group:
# if not _is_member(user, group):
# return HttpResponse("Login forbidden.")

def redirect_with_ticket_to_service(backend, user, response, *args, **kwargs):
print backend, user, response, args, str(kwargs)
response = redirect(service+"?ticket=aaa")
26 changes: 18 additions & 8 deletions tumbo/aaa/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def _setup(self):

def test_login_to_console(self):
self.client1.login(username='user1', password='pass')
self.client1.logout()
response = self.client1.get(reverse('console'))
self.assertEqual(200, response.status_code)

Expand All @@ -48,18 +49,13 @@ def test_step1_login_to_cas_with_service_redirects_to_service_with_ticket(self):
self._setup()
response = self.client1.post(self.cas_login_url, {'username':'user1', 'password': 'pass', 'service': self.userland_home})
self.assertEqual(302, response.status_code)
try:
self.assertTrue(("?ticket=" in response['Location']))
except Exception, e:
logger.error(response['Location'])
#print response['Location']
raise e
self.assertTrue(("http://testserver/userland/user1/base1/static/index.html?ticket=" in response['Location']))
return response['Location']

def test_step2_service_calls_cas_url_to_verify_ticket(self):
#self._setup()
url = self.test_step1_login_to_cas_with_service_redirects_to_service_with_ticket()
qs = urlparse(url).query
self.url = self.test_step1_login_to_cas_with_service_redirects_to_service_with_ticket()
qs = urlparse(self.url).query
self.cas_ticketverify+="?%s&service=%s" % (qs, self.userland_home)
self.client1.logout()
self.response = self.client1.get(self.cas_ticketverify)
Expand All @@ -69,3 +65,17 @@ def test_step2_verify_ticket_returns_readable_token(self):
self.test_step2_service_calls_cas_url_to_verify_ticket()
username, data = read_jwt(self.response.content, settings.SECRET_KEY)
User.objects.get(username=username)

def test_call_service_with_ticket(self):
url = self.test_step1_login_to_cas_with_service_redirects_to_service_with_ticket()
print url

self.response = self.client1.get(url)
print self.response._headers
# expect 404 because worker is not running
self.assertEqual(404, self.response.status_code)
#self.assertContains("asdf", self.response.content)

# if successfull, we receive a Set-Cookie Header
#import pdb; pdb.set_trace()

4 changes: 4 additions & 0 deletions tumbo/core/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def setUp(self, distribute_mock):
setting.value = "setting2_value"
setting.save()

#self.client1 = Client(enforce_csrf_checks=True) # logged in with objects
#self.client2 = Client(enforce_csrf_checks=True) # logged in without objects
#self.client3 = Client(enforce_csrf_checks=True) # not logged in

self.client1 = Client() # logged in with objects
self.client2 = Client() # logged in without objects
self.client3 = Client() # not logged in
Expand Down
16 changes: 8 additions & 8 deletions tumbo/tumbo/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@
'token': os.environ.get('DIGITALOCEAN_CONFIG', None),
'zone': os.environ.get('DIGITALOCEAN_ZONE', None)
},
'core.plugins.datastore': {
'ENGINE': "django.db.backends.postgresql_psycopg2",
'HOST': "127.0.0.1",
'PORT': "15432",
'NAME': "store",
'USER': "store",
'PASSWORD': "store123"
}
#'core.plugins.datastore': {
# 'ENGINE': "django.db.backends.postgresql_psycopg2",
# 'HOST': "127.0.0.1",
# 'PORT': "15432",
# 'NAME': "store",
# 'USER': "store",
# 'PASSWORD': "store123"
#}
}

TUMBO_SCHEDULE_JOBSTORE = "sqlite:////tmp/jobstore.db"
Expand Down
3 changes: 2 additions & 1 deletion tumbo/tumbo/dev_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@

TUMBO_SCHEDULE_JOBSTORE = "sqlite:////tmp/jobstore.db"

REDIS_METRICS['PASSWORD'] = os.environ.get('CACHE_ENV_REDIS_PASS', None)
if os.environ.get('CACHE_ENV_REDIS_PASS', None):
REDIS_METRICS['PASSWORD'] = os.environ.get('CACHE_ENV_REDIS_PASS')

#TEMPLATE_LOADERS += (
# 'core.loader.DevLocalRepositoryPathLoader',
Expand Down
1 change: 1 addition & 0 deletions tumbo/ui/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def home(request):
def profile(request):
"""Home view, displays login mechanism"""
auth, created = AuthProfile.objects.get_or_create(user=request.user)
print auth, created
if not request.user.is_authenticated():
raise Exception("Not Logged in")

Expand Down

0 comments on commit 209b0eb

Please sign in to comment.