django-cas-server/cas_server/utils.py

175 lines
5.2 KiB
Python
Raw Normal View History

2015-06-03 15:42:25 +00:00
# ⁻*- coding: utf-8 -*-
2015-05-27 20:10:06 +00:00
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
# more details.
#
# You should have received a copy of the GNU General Public License version 3
# along with this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# (c) 2015 Valentin Samir
2015-05-27 19:56:39 +00:00
"""Some util function for the app"""
from .default_settings import settings
2015-05-29 14:11:10 +00:00
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib import messages
import random
import string
import json
2016-06-24 19:07:19 +00:00
from threading import Thread
2015-12-12 12:51:59 +00:00
from importlib import import_module
2016-06-24 21:37:24 +00:00
from six.moves import BaseHTTPServer
from six.moves.urllib.parse import urlparse, urlunparse, parse_qsl, urlencode
2015-06-21 16:56:16 +00:00
def context(params):
params["settings"] = settings
return params
def JsonResponse(request, data):
data["messages"] = []
for msg in messages.get_messages(request):
data["messages"].append({'message': msg.message, 'level': msg.level_tag})
return HttpResponse(json.dumps(data), content_type="application/json")
def import_attr(path):
"""transform a python module.attr path to the attr"""
if not isinstance(path, str):
2016-05-11 11:06:41 +00:00
return path
if "." not in path:
ValueError("%r should be of the form `module.attr` and we just got `attr`" % path)
module, attr = path.rsplit('.', 1)
try:
return getattr(import_module(module), attr)
except ImportError:
raise ImportError("Module %r not found" % module)
except AttributeError:
raise AttributeError("Module %r has not attribut %r" % (module, attr))
2015-06-12 16:10:52 +00:00
2015-05-29 14:11:10 +00:00
def redirect_params(url_name, params=None):
"""Redirect to `url_name` with `params` as querystring"""
url = reverse(url_name)
2015-06-21 16:56:16 +00:00
params = urlencode(params if params else {})
2015-05-29 14:11:10 +00:00
return HttpResponseRedirect(url + "?%s" % params)
2015-06-12 16:10:52 +00:00
def reverse_params(url_name, params=None, **kwargs):
url = reverse(url_name, **kwargs)
params = urlencode(params if params else {})
return url + "?%s" % params
2015-05-16 21:43:46 +00:00
def update_url(url, params):
2015-05-27 19:56:39 +00:00
"""update params in the `url` query string"""
2015-06-21 16:56:16 +00:00
if not isinstance(url, bytes):
2015-06-12 16:10:52 +00:00
url = url.encode('utf-8')
2015-06-21 16:56:16 +00:00
for key, value in list(params.items()):
if not isinstance(key, bytes):
2015-06-03 15:42:25 +00:00
del params[key]
key = key.encode('utf-8')
2015-06-21 16:56:16 +00:00
if not isinstance(value, bytes):
2015-06-03 15:42:25 +00:00
value = value.encode('utf-8')
params[key] = value
2015-06-21 16:56:16 +00:00
url_parts = list(urlparse(url))
query = dict(parse_qsl(url_parts[4]))
2015-05-16 21:43:46 +00:00
query.update(params)
2015-06-21 16:56:16 +00:00
url_parts[4] = urlencode(query)
2016-06-26 09:16:41 +00:00
for i, url_part in enumerate(url_parts):
if not isinstance(url_part, bytes):
url_parts[i] = url_part.encode('utf-8')
2015-06-21 16:56:16 +00:00
return urlunparse(url_parts).decode('utf-8')
2015-06-12 16:10:52 +00:00
def unpack_nested_exception(error):
"""If exception are stacked, return the first one"""
i = 0
while True:
if error.args[i:]:
if isinstance(error.args[i], Exception):
error = error.args[i]
i = 0
else:
i += 1
else:
break
return error
def _gen_ticket(prefix, lg=settings.CAS_TICKET_LEN):
"""Generate a ticket with prefix `prefix`"""
return '%s-%s' % (
prefix,
''.join(
random.choice(
string.ascii_letters + string.digits
) for _ in range(lg - len(prefix) - 1)
)
)
2015-06-12 16:10:52 +00:00
def gen_lt():
"""Generate a Service Ticket"""
return _gen_ticket(settings.CAS_LOGIN_TICKET_PREFIX, settings.CAS_LT_LEN)
2015-06-12 16:10:52 +00:00
def gen_st():
"""Generate a Service Ticket"""
return _gen_ticket(settings.CAS_SERVICE_TICKET_PREFIX, settings.CAS_ST_LEN)
2015-06-12 16:10:52 +00:00
def gen_pt():
"""Generate a Proxy Ticket"""
return _gen_ticket(settings.CAS_PROXY_TICKET_PREFIX, settings.CAS_PT_LEN)
2015-06-12 16:10:52 +00:00
def gen_pgt():
"""Generate a Proxy Granting Ticket"""
return _gen_ticket(settings.CAS_PROXY_GRANTING_TICKET_PREFIX, settings.CAS_PGT_LEN)
2015-06-12 16:10:52 +00:00
def gen_pgtiou():
"""Generate a Proxy Granting Ticket IOU"""
return _gen_ticket(settings.CAS_PROXY_GRANTING_TICKET_IOU_PREFIX, settings.CAS_PGTIOU_LEN)
def gen_saml_id():
"""Generate an saml id"""
return _gen_ticket('_')
2016-06-24 19:07:19 +00:00
class PGTUrlHandler(BaseHTTPServer.BaseHTTPRequestHandler):
2016-06-24 19:23:33 +00:00
PARAMS = {}
2016-06-24 19:07:19 +00:00
def do_GET(s):
s.send_response(200)
2016-06-24 21:37:24 +00:00
s.send_header(b"Content-type", "text/plain")
2016-06-24 19:07:19 +00:00
s.end_headers()
2016-06-24 21:37:24 +00:00
s.wfile.write(b"ok")
2016-06-24 19:07:19 +00:00
url = urlparse(s.path)
params = dict(parse_qsl(url.query))
PGTUrlHandler.PARAMS.update(params)
2016-06-24 19:23:33 +00:00
2016-06-26 09:16:41 +00:00
def log_message(self, template, *args):
2016-06-24 19:07:19 +00:00
return
@staticmethod
def run():
server_class = BaseHTTPServer.HTTPServer
httpd = server_class(("127.0.0.1", 0), PGTUrlHandler)
2016-06-24 19:23:33 +00:00
(host, port) = httpd.socket.getsockname()
2016-06-24 19:07:19 +00:00
def lauch():
httpd.handle_request()
httpd.server_close()
2016-06-24 19:23:33 +00:00
2016-06-24 19:07:19 +00:00
httpd_thread = Thread(target=lauch)
httpd_thread.daemon = True
httpd_thread.start()
return (httpd_thread, host, port)