def thredds(request): encoded_secret_key = settings.ESGF_SECRET_KEY return_query_name = settings.ESGF_RETURN_QUERY_NAME session_cookie_name = settings.ESGF_SESSION_COOKIE_NAME if request.is_secure(): scheme = 'https' else: scheme = 'http' authenticate_url = '{}://{}{}'.format(scheme, request.get_host(), reverse('home')) redirect = '{}://{}{}{}'.format( scheme, request.get_host(), reverse('thredds'), 'fileServer/cmip5_css02_data/cmip5/output1/CMCC/CMCC-CM/historical/day/atmos/day/r1i1p1/clt/1/clt_day_CMCC-CM_historical_r1i1p1_20050101-20051231.nc' ) session_cookie = request.COOKIES.get(session_cookie_name) cookie = None if session_cookie: secret_key = encoded_secret_key.decode('base64') cookie = SecureCookie.parse_ticket(secret_key, session_cookie, None, None) return render( request, 'auth/thredds.j2', { 'authenticate_url': authenticate_url, 'return_query_name': return_query_name, 'encoded_secret_key': encoded_secret_key, 'quoted_encoded_secret_key': quote(encoded_secret_key), 'session_cookie_name': session_cookie_name, 'redirect': redirect, 'session_cookie': session_cookie, 'decrypted_session_cookie': cookie })
def check_cookie(): key = 'yUGFos3E/MbDiN4q1YjjTE/gmO7SLxeetAxxAgPUaB0=' key = key.decode('base64') cookie_value = 'c39193baf69307eb13967af1c6a3e9fdbbf4b468eefdb0a543ba2a28aaa74ae4f452e8482dd4b7bf465013369f92decd-8d98649db5e1ef67e287474442645cb6-25e8ec8bd024ae0dd56078403fe099e4d9e948bdf51661d848fee5e8cdb0afc1' cookie = SecureCookie.parse_ticket(key, cookie_value, None, None) print(cookie)
def test02_check_cookie(self): secret = os.urandom(32) cookie = SecureCookie(secret, 'pjk', '127.0.0.1') cookie_val = cookie.cookie_value() session = { 'authkit.cookie.user': None, 'authkit.cookie.user_data': None } ticket = SecureCookie.parse_ticket(secret, cookie_val, '127.0.0.1', session) self.assertEqual('pjk', ticket[1], 'Error parsing user id') log.info('ticket: %r', ticket)
def test02_check_cookie(self): secret = os.urandom(32) cookie = SecureCookie(secret, 'pjk', '127.0.0.1') cookie_val = cookie.cookie_value() session = { 'authkit.cookie.user': None, 'authkit.cookie.user_data': None } ticket = SecureCookie.parse_ticket(secret, cookie_val, '127.0.0.1', session) self.assertEqual('pjk', ticket[1], 'Error parsing user id') log.info('ticket: %r', ticket)
def _parse_cookie_value(self, cookie_value): """ Parses user information from an encrypted cookie """ shared_secret = codecs.decode(settings.SECURITY_SHAREDSECRET.encode(), "base64") try: parsed_cookie_items = SecureCookie.parse_ticket( shared_secret, cookie_value, None, None) username = parsed_cookie_items[self.USERNAME_INDEX] return { "username": username, "groups": [], } except BadTicket as e: LOG.warning("Error decoding cookie.") raise CookieParsingError(e) except VerificationError as e: LOG.warning("Cookie signature verification error.") raise CookieParsingError(e) except IndexError as e: LOG.warning("Index not in cookie.") raise CookieParsingError(e)