Changeset 1035
- Timestamp:
- 26/05/06 14:53:19 (15 years ago)
- Location:
- TI12-security/trunk/python
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
TI12-security/trunk/python/NDG/SecurityCGI.py
r1022 r1035 59 59 returnURI=None, 60 60 trustedHostInfo=None, 61 cookieLifetimeHrs= 12,61 cookieLifetimeHrs=8, 62 62 wsDebug=False, 63 63 **cgiFieldStorageKwArgs): … … 70 70 list of login URI for trusted hosts 71 71 returnURI: the address to redirect back to following a 72 redirect 72 redirect 73 73 to the user's home site to obtain their 74 74 credentials … … 101 101 102 102 # Work out expiry time offset from the time this script is run 103 self.dtCookieExpiry = datetime.utcnow() 104 self.dtCookieExpiry +=timedelta(seconds=self.cookieLifetimeHrs*60*60)103 self.dtCookieExpiry = datetime.utcnow() + \ 104 timedelta(seconds=self.cookieLifetimeHrs*60*60) 105 105 106 106 self.__wsDebug = False 107 self._ _authorisationMethod = None107 self._authorisationMethod = None 108 108 109 109 cgi.FieldStorage.__init__(self, **cgiFieldStorageKwArgs) … … 121 121 # Receive credentials back from home site and set a new cookie at 122 122 # remote site 123 if 'expires' in self: 124 encodedExpiry = self['expires'].value 125 else: 126 encodedExpiry = None 127 123 128 self.receiveCredsResponse(self['NDG-ID1'].value, 124 129 self['NDG-ID2'].value, 130 encodedExpiry=encodedExpiry, 125 131 **kwargs) 126 132 127 133 elif 'authenticate' in self: 128 # User has logged on at home site and a cookie is now set - 134 # User has logged on at home site and a cookie is now set - 129 135 # next step is processCredsRequest() below 130 136 sessCookie = self.authenticate() 131 137 132 138 if 'returnURI' in self: 133 # Authentication is required following a redirect from 139 # Authentication is required following a redirect from 134 140 # another site - redirect back to the remote site returning 135 141 # the cookie information … … 158 164 pageTitle='', 159 165 headTags='', 160 delayTime= 3,161 redirectMsg=' Redirecting'):166 delayTime=0, 167 redirectMsg=''): 162 168 """Request credentials from a user's home site 163 169 … … 174 180 requestURI = self['requestURI'].value 175 181 176 output ="""Content-type: text/html182 print """Content-type: text/html 177 183 178 184 <html> … … 187 193 </html>""" % \ 188 194 (pageTitle, delayTime, requestURI, self.returnURI, headTags, redirectMsg) 189 sys.stderr.write(output) 190 print output 191 192 195 196 193 197 #_________________________________________________________________________ 194 198 def receiveCredsResponse(self, 195 199 sessID, 196 200 sessMgrURI, 197 pageTitle='', 198 hdrTxt='', 199 bodyTxt=''): 200 """Remote site receives returned credentials and creates a new cookie 201 encodedExpiry=None, 202 **showCredsReceivedKwArgs): 203 """Remote site receives returned credentials and creates a new cookie 201 204 for its domain""" 202 203 sessCookie = UserSession.createSecurityCookie(sessID, 204 sessMgrURI, 205 dtExpiry=self.dtCookieExpiry) 206 205 sessCookie = self.createCookie(sessID, sessMgrURI, encodedExpiry) 206 self.showCredsReceived(sessCookie) 207 208 #_________________________________________________________________________ 209 def showCredsReceived(sessCookie, pageTitle='', hdrTxt='', bodyTxt=''): 210 """Called from receiveCredsResponse() once a cookie has been created. 211 Makes a page to set the cookie and display to the user that they have 212 been authenticated. Derived class should override this method as 213 required""" 207 214 print """Content-type: text/html" 208 215 %s … … 217 224 </body> 218 225 </html>""" % (sessCookie.output(), pageTitle, hdrTxt, bodyTxt) 219 220 221 #_________________________________________________________________________ 222 def processCredsRequest(self, 226 227 228 #_________________________________________________________________________ 229 def createCookie(self, sessID, sessMgrURI, encodedExpiry=None): 230 """Convert credentials passed over URI from users home site into a new 231 cookie""" 232 233 if encodedExpiry: 234 # Expiry is taken from encoded value passed over URI 235 dtExpiry = None 236 expiryStr = base64.b64decode(encodedExpiry) 237 else: 238 # Expiry is set from life time in hours input in __init__ 239 dtExpiry = self.dtCookieExpiry 240 expiryStr = None 241 242 return UserSession.createSecurityCookie(sessID, 243 sessMgrURI, 244 dtExpiry=dtExpiry, 245 expiryStr=expiryStr) 246 247 248 #_________________________________________________________________________ 249 def processCredsRequest(self, 223 250 returnURI=None, 224 251 sessCookie=None, … … 236 263 sessCookie = SimpleCookie(os.environ['HTTP_COOKIE']) 237 264 238 239 265 if sessCookie: 240 266 # Cookie is set - check for NDG cookie … … 248 274 # Return cookie to requestor 249 275 self.returnCreds(sessCookie, returnURI, **returnCredsKwArgs) 250 276 251 277 else: 252 278 # No cookie present - display login. Submit must redirect back to … … 284 310 285 311 if setCookie: 286 cookieTxt = sessCookie.output() + os.line _sep()312 cookieTxt = sessCookie.output() + os.linesep 287 313 else: 288 314 cookieTxt = '' 289 315 290 print"""Content-type: text/html291 %s 316 output = """Content-type: text/html 317 %s 292 318 <html> 293 319 <head> 294 320 <title>%s</title> 295 321 <meta http-equiv="REFRESH" 296 content="%d; url=%s?NDG-ID1=%s&NDG-ID2=%s ">322 content="%d; url=%s?NDG-ID1=%s&NDG-ID2=%s&expires=%s"> 297 323 %s 298 324 </head> … … 306 332 sessCookie['NDG-ID1'].value, 307 333 sessCookie['NDG-ID2'].value, 334 base64.b64encode(sessCookie['NDG-ID1']['expires']), 308 335 hdrTxt, 309 336 redirectMsg) 310 311 337 print output 338 339 312 340 #_________________________________________________________________________ 313 341 def authenticate(self, bAuthorise=False): … … 332 360 333 361 if self.userName is None: 362 self.showLogin(returnURI=self['returnURI'].value, 363 setCookie=True, 364 contentTypeHdr=True, 365 pageTitle="NDG Login", 366 htmlTag=True, 367 bodyTag=True) 334 368 raise SecurityCGIError("no username set for authentication") 335 369 336 370 if self.passPhrase is None: 371 self.showLogin(returnURI=self['returnURI'].value, 372 setCookie=True, 373 contentTypeHdr=True, 374 pageTitle="NDG Login", 375 htmlTag=True, 376 bodyTag=True) 337 377 raise SecurityCGIError("no pass-phrase set for authentication") 338 378 … … 346 386 traceFile=traceFile) 347 387 348 return smClnt.connect(userName=self.userName, 349 pPhrase=self.passPhrase, 350 clntPriKeyPwd=self.clntPriKeyPwd) 388 sSessCookie = smClnt.connect(userName=self.userName, 389 pPhrase=self.passPhrase, 390 clntPriKeyPwd=self.clntPriKeyPwd) 391 sessCookie = SimpleCookie(sSessCookie) 392 return sessCookie 393 351 394 except Exception, e: 395 self.showLogin(returnURI=self['returnURI'].value, 396 setCookie=True, 397 contentTypeHdr=True, 398 pageTitle="NDG Login", 399 htmlTag=True, 400 bodyTag=True) 352 401 raise SecurityCGIError("Session client: " + str(e)) 353 402 … … 413 462 "noMapping": ''} 414 463 415 if self._ _authorisationMethod is None:464 if self._authorisationMethod is None: 416 465 # Default to safest option for user 417 466 authorisationMethodChk["allowMappingWithPrompt"] = ' checked' 418 467 else: 419 authorisationMethodChk[self._ _authorisationMethod] = ' checked'468 authorisationMethodChk[self._authorisationMethod] = ' checked' 420 469 421 470 print \ … … 439 488 var style = document.layers[whichLayer].style; 440 489 } 441 style.visibility = style.visibility == "visible" ? "hidden":"visible";442 }490 style.visibility = style.visibility == "visible" ? 491 "hidden":"visible"; } 443 492 //--> 444 493 </script> … … 458 507 <tr> 459 508 <td colspan="2" align="right"> 460 <a href="javascript:toggleLayer('advSettings');">Advanced Settings</a>461 <input type=submit value="Login">509 <a href="javascript:toggleLayer('advSettings');">Advanced 510 Settings</a> <input type=submit value="Login"> 462 511 </td> 463 512 </tr> … … 470 519 <div id="advSettings" style="position: relative; visibility: hidden;"> 471 520 <h4>Role Mapping for access to other trusted sites</h4> 472 <p>Your account has roles or <i>privileges</i> which determine what data you have access to. If you access data at another NDG trusted site, these roles can be mapped to local roles at that site to help you gain access:473 </p> 474 <table bgcolor=#ADD8E6 cellspacing=0 border=0 cellpadding=5> 475 <tbody>521 <p>Your account has roles or <i>privileges</i> which determine what data 522 you have access to. If you access data at another NDG trusted site, these roles 523 can be mapped to local roles at that site to help you gain access: </p> 524 <table bgcolor=#ADD8E6 cellspacing=0 border=0 cellpadding=5> <tbody> 476 525 <tr> 477 526 <td> 478 <input type="radio" name="authorisationMethod" value="allowMapping"%s>479 </td>527 <input type="radio" name="authorisationMethod" 528 value="allowMapping"%s> </td> 480 529 <td> 481 Allow my roles to be mapped to local roles at other NDG trusted sites.482 </td>530 Allow my roles to be mapped to local roles at other NDG trusted 531 sites. </td> 483 532 </tr> 484 533 <tr> 485 534 <td> 486 <input type="radio" name="authorisationMethod" value="allowMappingWithPrompt"%s>487 </td>535 <input type="radio" name="authorisationMethod" 536 value="allowMappingWithPrompt"%s> </td> 488 537 <td> 489 Allow my roles to be mapped, but prompt me so that I may choose which roles to map before gaining access.490 </td>538 Allow my roles to be mapped, but prompt me so that I may choose 539 which roles to map before gaining access. </td> 491 540 <tr> 492 541 <td> … … 613 662 clntPubKeyFilePath=self.clntPubKeyFilePath, 614 663 clntPriKeyFilePath=self.clntPriKeyFilePath, 615 traceFile=traceFile) 664 traceFile=traceFile) 616 665 617 666 self.trustedHostInfo = aaClnt.getTrustedHostInfo( -
TI12-security/trunk/python/NDG/Session.py
r1022 r1035 73 73 74 74 75 76 77 75 #_____________________________________________________________________________ 78 76 # Inheriting from 'object' allows Python 'new-style' class with Get/Set … … 82 80 83 81 # Session ID 84 __sessIDlen = 12882 __sessIDlen = 64 85 83 86 84 __cookieTags = ("NDG-ID1", "NDG-ID2") … … 91 89 __cookieDomainTag = 'domain' 92 90 __cookieExpiryTag = "expires" 93 94 __sessCookieExpiryFmt = "%a, %d-%b-%Y %H:%M:%S GMT" 91 92 # Quotes are vital (and part of the official cookei format) - otherwise it 93 # will not be parsed correctly 94 __sessCookieExpiryFmt = "\"%a, %d-%b-%Y %H:%M:%S GMT\"" 95 95 96 96 … … 327 327 if not isinstance(dtExpiry, datetime): 328 328 UserSessionError, \ 329 "Expecting va ild datetime object with dtExpiry keyword"329 "Expecting valid datetime object with dtExpiry keyword" 330 330 331 331 expiryStr = dtExpiry.strftime(cls.__sessCookieExpiryFmt) -
TI12-security/trunk/python/Tests/SecurityClientTest.py
r1022 r1035 50 50 51 51 # Attribute Authority client tests 52 aaWSDL = 'http://g abriel.bnsc.rl.ac.uk/attAuthority.wsdl'52 aaWSDL = 'http://glue.badc.rl.ac.uk/attAuthority.wsdl' 53 53 aaPubKeyFilePath = None 54 54 … … 183 183 import pdb 184 184 pdb.set_trace() 185 role = ' staff'185 role = 'acsoe' 186 186 try: 187 187 trustedHosts = self.aaClnt.getTrustedHostInfo( -
TI12-security/trunk/python/Tests/security.py
r1022 r1035 21 21 22 22 #_________________________________________________________________________ 23 def showLogin(self, returnURI=None, **kwargs):23 def showLogin(self, returnURI=None, bAuthorise=False, **kwargs): 24 24 """Display initial NDG login form""" 25 25 26 26 if returnURI: 27 27 returnURIfield = \ 28 "<input type=hidden name=returnURIvalue=\"%s\">" % returnURI28 "<input type=hidden name=\"returnURI\" value=\"%s\">" % returnURI 29 29 else: 30 30 returnURIfield = '' … … 32 32 33 33 if bAuthorise: 34 authoriseArg = "<input type=hidden name=authorise value=\"1\">" 34 authoriseField = \ 35 "<input type=hidden name=\"authorise\" value=\"1\">" 35 36 else: 36 authorise Arg= ""37 authoriseField = "" 37 38 38 39 … … 41 42 "allowMappingWithPrompt" : '', 42 43 "noMapping": ''} 43 44 if self._ _authorisationMethod is None:44 45 if self._authorisationMethod is None: 45 46 # Default to safest option for user 46 47 authorisationMethodChk["allowMappingWithPrompt"] = ' checked' 47 48 else: 48 authorisationMethodChk[self._ _authorisationMethod] = ' checked'49 49 authorisationMethodChk[self._authorisationMethod] = ' checked' 50 50 51 51 52 print """Content-type: text/html 52 53 <html> "53 54 <html> 54 55 <head> 55 <title> %s</title>56 <title>NDG Login</title> 56 57 <style type=\"text/css\"> 57 58 <!-- … … 90 91 var style = document.layers[whichLayer].style; 91 92 } 92 style.visibility = style.visibility == "visible" ? "hidden":"visible";93 }93 style.visibility = style.visibility == "visible" ? 94 "hidden":"visible"; } 94 95 //--> 95 96 </script> 96 97 <h3>NERC Data Grid Site Login (Test)<BR clear=all></h3> 97 98 <hr> 98 99 99 100 <form action="%s" method="POST"> 100 101 101 102 <table bgcolor=#ADD8E6 cellspacing=0 border=0 cellpadding=5> 102 103 <tbody> 103 <tr><td>User Name:</td> <td><input type=text name=userName value=""> 104 </td></tr> 105 <tr> 106 <td>Password:</td> 107 <td><input type=password name=passPhrase></td> 108 </tr> 109 <tr> 110 <td colspan="2" align="right"> 111 <a href="javascript:toggleLayer('advSettings');">Advanced Settings</a> 112 <input type=submit value="Login"> 113 </td> 114 </tr> 115 <input type=hidden name=authenticate value="1"> 116 %s""" % (self.scriptName, returnURIfield) 117 104 <tr> 105 <td>User Name:</td> 106 <td><input type=text name="userName" value=""></td> 107 </tr> 108 <tr> 109 <td>Password:</td> 110 <td><input type=password name="passPhrase"></td> 111 </tr> 112 <tr> 113 <td colspan="2" align="right"> 114 <a href="javascript:toggleLayer('advSettings');"> 115 Advanced Settings 116 </a> 117 <input type=submit value="Login"> 118 </td> 119 </tr> 120 <input type=hidden name="authenticate" value="1"> 121 </tbody> 122 </table> 123 %s 124 %s 125 </form> 126 </body> 127 </html>""" % (self.scriptName, returnURIfield, authoriseField) 128 118 129 print \ 119 """</tbody></table> 130 """ </tbody> 131 </table> 120 132 <br> 121 133 <div id="advSettings" style="position: relative; visibility: hidden;"> 122 <h4>Role Mapping for access to other trusted sites</h4> 123 <p>Your account has roles or <i>privileges</i> which determine what data you have access to. If you access data at another NDG trusted site, these roles can be mapped to local roles at that site to help you gain access: 124 </p> 125 <table bgcolor=#ADD8E6 cellspacing=0 border=0 cellpadding=5> 126 <tbody> 134 <h4>Role Mapping for access to other trusted sites</h4> 135 <p>Your account has roles or <i>privileges</i> which determine what data 136 you have access to. If you access data at another NDG trusted site, these 137 roles can be mapped to local roles at that site to help you gain access: 138 </p> 139 <table bgcolor=#ADD8E6 cellspacing=0 border=0 cellpadding=5> 140 <tbody> 141 <tr> 142 <td><input type="radio" name="authorisationMethod" 143 value="allowMapping"%s> 144 </td> 145 <td> 146 Allow my roles to be mapped to local roles at other NDG trusted sites. 147 </td> 148 </tr> 149 <tr> 150 <td> 151 <input type="radio" name="authorisationMethod" 152 value="allowMappingWithPrompt"%s> 153 </td> 154 <td> 155 Allow my roles to be mapped, but prompt me so that I may choose 156 which roles to map before gaining access. 157 </td> 127 158 <tr> 128 159 <td> 129 <input type="radio" name="authorisationMethod" value="allowMapping"%s>130 </td> 131 132 Allow my roles to be mapped to local roles at other NDG trusted sites.133 160 <input type="radio" name="authorisationMethod" value="noMapping"%s> 161 </td> 162 <td> 163 Don't allow mapping of my roles. 164 </td> 134 165 </tr> 135 <tr> 136 <td> 137 <input type="radio" name="authorisationMethod" value="allowMappingWithPrompt"%s> 138 </td> 139 <td> 140 Allow my roles to be mapped, but prompt me so that I may choose which roles to map before gaining access. 141 </td> 142 <tr> 143 <td> 144 <input type="radio" name="authorisationMethod" value="noMapping"%s> 145 </td> 146 <td> 147 Don't allow mapping of my roles. 148 </td> 149 </tr> 150 </tbody> 151 </table> 166 </tbody> 167 </table> 152 168 </div> 153 169 </form> … … 214 230 215 231 216 232 #_________________________________________________________________________ 233 def showCredsReceived(self, 234 sessCookie, 235 pageTitle='', 236 hdrTxt='', 237 bodyTxt=''): 238 """Called from receiveCredsResponse() once a cookie has been created. 239 Makes a page to set the cookie and display to the user that they have 240 been authenticated. Derived class should override this method as 241 required""" 242 print """Content-type: text/html 243 %s 244 245 <html> 246 <head> 247 <title>NDG Authentication</title> 248 <style type=\"text/css\"> 249 <!-- 250 .al { 251 text-align: justify 252 } 253 a{ 254 text-decoration:none; 255 } 256 a:hover{ 257 color:#0000FF; 258 } 259 body { font-family: Verdana, sans-serif; font-size: 11} 260 table { font-family: Verdana, sans-serif; font-size: 11} 261 --> 262 </style> 263 </head> 264 <body> 265 New cookie set from credentials transfered from other domain 266 </body> 267 </html>""" % sessCookie.output() 268 269 270 #_____________________________________________________________________________ 217 271 if __name__ == "__main__": 218 272 219 273 smWSDL = "http://gabriel.bnsc.rl.ac.uk/sessionMgr.wsdl" 220 274 aaWSDL = 'http://gabriel.bnsc.rl.ac.uk/attAuthority.wsdl' … … 222 276 smPubKeyFilePath = "/usr/local/NDG/conf/certs/gabriel-sm-cert.pem" 223 277 aaPubKeyFilePath = "/usr/local/NDG/conf/certs/gabriel-aa-cert.pem" 224 278 225 279 clntPubKeyFilePath = "../certs/GabrielCGI-cert.pem" 226 280 clntPriKeyFilePath = "../certs/GabrielCGI-key.pem"
Note: See TracChangeset
for help on using the changeset viewer.