/usr/lib/python2.7/dist-packages/MLBviewer/milbLogin.py is in mlbviewer 2015.sf.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | #!/usr/bin/env python
# mlbviewer is free software; you can redistribute it and/or modify
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, Version 2.
#
# mlbviewer 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 for more details.
#
# For a copy of the GNU General Public License, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA
import urllib
import urllib2
import re
import time
import datetime
import cookielib
import os
import sys
from mlbLog import MLBLog
# DEBUG VARIABLES
# Cookie debug writes cookie contents to cookielog
COOKIE_DEBUG=True
# If this is set to True, all cookie morsels are written to cookie file
# else if morsels are marked as discard, then they are not written to file
IGNORE_DISCARD=True
# DO NOT EDIT BELOW HERE
AUTHDIR = '.mlb'
COOKIEFILE = os.path.join(os.environ['HOME'], AUTHDIR, 'milbcookie')
SESSIONKEY = os.path.join(os.environ['HOME'], AUTHDIR, 'milbsessionkey')
LOGFILE = os.path.join(os.environ['HOME'], AUTHDIR, 'cookielog')
USERAGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'
class Error(Exception):
pass
class MLBNoCookieFileError(Error):
pass
class MLBAuthError(Error):
pass
class MiLBSession:
def __init__(self,user,passwd,debug=False):
self.user = user
if self.user is None:
# if user= is commented out, cfg.get() returns None, normalize this
self.user = ""
self.passwd = passwd
self.auth = True
self.logged_in = None
self.cookie_jar = None
self.cookies = {}
self.debug = debug
if COOKIE_DEBUG:
self.debug = True
self.log = MLBLog(LOGFILE)
try:
self.session_key = self.readSessionKey()
if self.debug:
self.log.write("LOGIN> Read session key from file: " + str(self.session_key))
except:
self.session_key = None
def readSessionKey(self):
sk = open(SESSIONKEY,"r")
self.session_key = sk.read()
sk.close()
return session_key
def writeSessionKey(self,session_key):
if self.debug:
self.log.write('Writing session-key to file: ' + str(self.session_key) + '\n')
sk = open(SESSIONKEY,"w")
sk.write(session_key)
sk.close()
return session_key
def extractCookies(self):
for c in self.cookie_jar:
self.cookies[c.name] = c.value
self.printCookies()
def printCookies(self):
if self.debug:
self.log.write('Printing relevant cookie morsels...\n')
for name in self.cookies.keys():
if name in ('fprt', 'ftmu', 'ipid'):
self.log.write(str(name) + ' = ' + str(self.cookies[name]))
self.log.write('\n')
def readCookieFile(self):
self.cookie_jar = cookielib.LWPCookieJar()
if self.cookie_jar != None:
if os.path.isfile(COOKIEFILE):
self.cookie_jar.load(COOKIEFILE,ignore_discard=IGNORE_DISCARD)
if self.debug:
self.log.write('readCookieFile:\n')
self.extractCookies()
else:
raise MLBNoCookieFileError
else:
self.error_str = "Couldn't open cookie jar"
raise Exception,self.error_str
def login(self):
try:
self.readCookieFile()
except MLBNoCookieFileError:
#pass
if self.debug:
self.log.write("LOGIN> No cookie file")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie_jar))
urllib2.install_opener(opener)
# First visit the login page and get the session cookie
login_url = 'https://secure.milb.com/enterworkflow.do?flowId=registration.profile'
txheaders = {'User-agent' : USERAGENT}
data = None
req = urllib2.Request(login_url,data,txheaders)
# we might have cookie info by now??
if self.user=="":
return
try:
handle = urllib2.urlopen(req)
except:
self.error_str = 'Error occurred in HTTP request to login page'
raise Exception, self.error_str
try:
if self.debug:
self.log.write('pre-login:\n')
self.extractCookies()
except Exception,detail:
raise Exception,detail
#if self.debug:
# self.log.write('Did we receive a cookie from the wizard?\n')
# for index, cookie in enumerate(self.cookie_jar):
# print >> self.log, index, ' : ' , cookie
self.cookie_jar.save(COOKIEFILE,ignore_discard=IGNORE_DISCARD)
rdata = handle.read()
# now authenticate
auth_values = {'uri' : '/account/login_register.jsp',
'registrationAction' : 'identify',
'emailAddress' : self.user,
'password' : self.passwd
}
success_pat = re.compile(r'Account Management - Profile | MiLB.com Account |')
auth_data = urllib.urlencode(auth_values)
auth_url = 'https://secure.milb.com/authenticate.do'
req = urllib2.Request(auth_url,auth_data,txheaders)
try:
handle = urllib2.urlopen(req)
self.cookie_jar.save(COOKIEFILE,ignore_discard=IGNORE_DISCARD)
if self.debug:
self.log.write('post-login: (this gets saved to file)\n')
self.extractCookies()
except:
self.error_str = 'Error occurred in HTTP request to auth page'
raise Exception, self.error_str
auth_page = handle.read()
#if self.debug:
# self.log.write('Did we receive a cookie from authenticate?\n')
# for index, cookie in enumerate(self.cookie_jar):
# print >> self.log, index, ' : ' , cookie
self.cookie_jar.save(COOKIEFILE,ignore_discard=IGNORE_DISCARD)
try:
loggedin = re.search(success_pat, auth_page).groups()
self.log.write('Logged in successfully!\n')
self.logged_in = True
except:
self.error_str = 'Login was unsuccessful.'
self.log.write(auth_page)
os.remove(COOKIEFILE)
raise MLBAuthError, self.error_str
#if self.debug:
# self.log.write("DEBUG>>> writing login page")
# self.log.write(auth_page)
# END login()
def getSessionData(self):
# This is the workhorse routine.
# 1. Login
# 2. Get the url from the workflow page
# 3. Logout
# 4. Return the raw workflow response page
# The hope is that this sequence will always be the same and leave
# it to url() to determine if an error occurs. This way, hopefully,
# error or no, we'll always log out.
if self.cookie_jar is None:
if self.logged_in is None:
login_count = 0
while not self.logged_in:
if self.user=="":
break
try:
self.login()
except:
if login_count < 3:
login_count += 1
time.sleep(1)
else:
raise
#raise Exception,self.error_str
# clear any login unsuccessful messages from previous failures
if login_count > 0:
self.error_str = "Not logged in."
wf_url = 'http://www.milb.com/index.jsp?flowId=media.media'
# Open the workflow url...
# Get the session key morsel
referer_str = ''
txheaders = {'User-agent' : USERAGENT,
'Referer' : referer_str }
req = urllib2.Request(url=wf_url,headers=txheaders,data=None)
try:
handle = urllib2.urlopen(req)
if self.debug:
self.log.write('getSessionData:\n')
self.extractCookies()
except Exception,detail:
self.error_str = 'Not logged in'
raise Exception, self.error_str
url_data = handle.read()
#if self.debug:
# if self.auth:
# self.log.write('Did we receive a cookie from workflow?\n')
# for index, cookie in enumerate(self.cookie_jar):
# print >> self.log, index, ' : ' , cookie
if self.auth:
self.cookie_jar.save(COOKIEFILE,ignore_discard=IGNORE_DISCARD)
#if self.debug:
# self.log.write("DEBUG>>> writing workflow page")
# self.log.write(url_data)
return url_data
def logout(self):
"""Logs out from the mlb.com session. Meant to prevent
multiple login errors."""
LOGOUT_URL="https://secure.mlb.com/enterworkflow.do?flowId=registration.logout&c_id=mlb"
txheaders = {'User-agent' : USERAGENT,
'Referer' : 'http://mlb.mlb.com/index.jsp'}
data = None
req = urllib2.Request(LOGOUT_URL,data,txheaders)
handle = urllib2.urlopen(req)
logout_info = handle.read()
handle.close()
pattern = re.compile(r'You are now logged out.')
if not re.search(pattern,logout_info):
self.error_str = "Logout was unsuccessful. Check " + LOGFILE
self.log.write(logout_info)
raise MLBAuthError, self.error_str
else:
self.log.write('Logged out successfully!\n')
self.logged_in = None
if self.debug:
self.log.write("DEBUG>>> writing logout page")
self.log.write(logout_info)
# clear session cookies since they're no longer valid
self.log.write('Clearing session cookies\n')
self.cookie_jar.clear_cookie_jar()
# session is bogus now - force a new login each time
self.cookie_jar = None
# END logout
|