Created
October 31, 2013 07:06
-
-
Save iOS0x00/7245395 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import urllib | |
from tornado.web import RequestHandler | |
#CAS setting | |
CAS_SETTINGS = { | |
#replace this with your cas server url | |
'cas_server' : 'http://cas_server', | |
#replace this with your website url | |
'service_url' : 'http://service_url/deal_with_st', | |
#CAS protocol version, 1.0 or 2.0? default is 2.0. | |
'version' : 2 | |
} | |
urlpattern = ( | |
( r'/login/?', tornadocas.LoginHandler ), | |
( r'/deal_with_st/?', tornadocas.DealWithSTHandler ), | |
) | |
''' | |
Just redirect to cas server to got the server ticket. | |
''' | |
class LoginHandler( RequestHandler ): | |
def get( self ): | |
#redirect to cas server | |
redirect_url = CAS_SETTINGS[ 'cas_server' ] + '/login?service=' + CAS_SETTINGS[ 'service_url' ] | |
self.redirect( redirect_url ) | |
''' | |
Validate the SERVER TICKET, return None if failed, otherwise userid. | |
''' | |
class DealWithSTHandler( RequestHandler ): | |
def get( self ): | |
#what you finally get | |
userid = None | |
try: | |
server_ticket = self.get_argument( 'ticket' ) | |
except Exception, e: | |
print 'there is not server ticket in request argumets!' | |
raise HTTPError( 404 ) | |
#validate the ST | |
validate_suffix = '/validate' if CAS_SETTINGS[ 'version' ] == 1 else '/proxyValidate' | |
validate_url = CAS_SETTINGS[ 'cas_server'] + validate_suffix + '?service=' + urllib.quote( CAS_SETTINGS[ 'service_url' ] ) + '&ticket=' + urllib.quote( server_ticket ) | |
response = urllib.urlopen( validate_url ).read() | |
pattern = r'<cas:user>(.*)</cas:user>' | |
match = re.search( pattern, response ) | |
if match: | |
userid = match.groups()[ 0 ] | |
if not userid: | |
print 'validate failed!' | |
raise HTTPError( 404 ) | |
self.deal_with_userid( userid ) | |
def deal_with_userid( self, userid ): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment