#!/usr/bin/python
# Debug
##import os
##print "Content-type: text/html\n"
##import sys
##sys.stderr = sys.stdout
# Copyright 2011 Jon Rifkin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-----------------------------------------------------------------------
# Usage
#-----------------------------------------------------------------------
#
# Purpose
# Authenticate users against a CAS server from your python cgi scripts.
#
# Using in your script
#
# import pycas
# status, id, cookie = pycas.login(CAS_SERVER,THIS_SCRIPT)
#
# Required Parameters
#
# - CAS_SERVER : the url of your CAS server
# (for example, https://login.yoursite.edu).
# - THIS_SCRIPT: the url of the calling python cgi script.
#
# Returned Values
#
# - status: return code, 0 for success.
# - id : the user name returned by cas.
# - cookie: when non-blank, send this cookie to the client's
# browser so it can authenticate for the rest of the
# session.
#
# Optional Parmaters:
# - lifetime: lifetime of the cookie in seconds, enforced by pycas.
# Default is 0, meaning unlimited lifetime.
# - path: Authentication cookie applies for all urls under 'path'.
# Defaults to "/" (all urls).
# - protocol: CAS protocol version. Default is 2. Can be set to 1.
# - secure: Default is 1, which authenticates for https connections only.
# - opt: set to 'renew' or 'gateway' for these CAS options.
#
# Examples:
# status, id, cookie = pycas.login(CAS_SERVER,THIS_SCRIPT,protocol=1,secure=0)
# status, id, cookie = pycas.login(CAS_SERVER,THIS_SCRIPT,path="/cgi-bin/accts")
#
# Status Codes are listed below.
#
#-----------------------------------------------------------------------
# Constants
#-----------------------------------------------------------------------
#
# Secret used to produce hash. This can be any string. Hackers
# who know this string can forge this script's authentication cookie.
SECRET = "7f16162998eb7efafb1498f75190a937"
# Name field for pycas cookie
PYCAS_NAME = "pycas"
# CAS Staus Codes: returned to calling program by login() function.
CAS_OK = 0 # CAS authentication successful.
CAS_COOKIE_EXPIRED = 1 # PYCAS cookie exceeded its lifetime.
CAS_COOKIE_INVALID = 2 # PYCAS cookie is invalid (probably corrupted).
CAS_TICKET_INVALID = 3 # CAS server ticket invalid.
CAS_GATEWAY = 4 # CAS server returned without ticket while in gateway mode.
# Status codes returned internally by function get_cookie_status().
COOKIE_AUTH = 0 # PYCAS cookie is valid.
COOKIE_NONE = 1 # No PYCAS cookie found.
COOKIE_GATEWAY = 2 # PYCAS gateway cookie found.
COOKIE_INVALID = 3 # Invalid PYCAS cookie found.
# Status codes returned internally by function get_ticket_status().
TICKET_OK = 0 # Valid CAS server ticket found.
TICKET_NONE = 1 # No CAS server ticket found.
TICKET_INVALID = 2 # Invalid CAS server ticket found.
CAS_MSG = (
"CAS authentication successful.",
"PYCAS cookie exceeded its lifetime.",
"PYCAS cookie is invalid (probably corrupted).",
"CAS server ticket invalid.",
"CAS server returned without ticket while in gateway mode.",
)
###Optional log file for debugging
###LOG_FILE="/tmp/cas.log"
#-----------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------
import os
import cgi
from hashlib import md5
import time
import urllib
import urlparse
#-----------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------
# For debugging.
def writelog(msg):
f = open(LOG_FILE,"a")
timestr = time.strftime("%Y-%m-%d %H:%M:%S ");
f.write(timestr + msg + "\n");
f.close()
# Used for parsing xml. Search str for first occurance of
#
Parameters sent from browser
pycas.py
"""
# Print browser parameters from pycas.login
if cgi.FieldStorage().has_key("ticket"):
ticket = cgi.FieldStorage()["ticket"].value
else:
ticket = ""
in_cookie = os.getenv("HTTP_COOKIE")
print """
Ticket %s
Cookie %s
Parameters returned from pycas.login()
status | %s - %s |
id | %s |
cookie | %s |