# HG changeset patch
# User Robert Bradshaw <robertwb@math.washington.edu>
# Date 1199857970 28800
# Node ID 95ffb0a687e98678b2f0b0981079f16fcc3df6d9
# Parent addc96bacb8b96bb134f38260fd4d643900eec2a
Login redunant on server startup when page opens, disable.
diff -r addc96bacb8b -r 95ffb0a687e9 sage/server/notebook/avatars.py
|
a
|
b
|
class PasswordChecker(object): |
| 39 | 39 | implements(checkers.ICredentialsChecker) |
| 40 | 40 | credentialInterfaces = (credentials.IUsernamePassword,) |
| 41 | 41 | |
| 42 | | |
| 43 | | |
| 44 | 42 | def add_user(self, username, password, email, account_type='user'): |
| 45 | 43 | self.check_username(username) |
| 46 | 44 | U = twist.notebook.add_user(username, password, email, account_type) |
| … |
… |
class PasswordChecker(object): |
| 66 | 64 | return defer.succeed(username) |
| 67 | 65 | else: |
| 68 | 66 | return defer.succeed(FailedLogin(username,failure_type='password')) |
| | 67 | |
| | 68 | |
| | 69 | class ITokenCredentials(credentials.ICredentials): |
| | 70 | def checkToken(token): |
| | 71 | pass |
| | 72 | |
| | 73 | |
| | 74 | class TokenCred(object): |
| | 75 | implements(ITokenCredentials) |
| | 76 | |
| | 77 | def __init__(self, token=None): |
| | 78 | self.token = token |
| | 79 | |
| | 80 | def checkToken(self, token): |
| | 81 | return token == self.token |
| | 82 | |
| | 83 | |
| | 84 | class OneTimeTokenChecker(object): |
| | 85 | implements(checkers.ICredentialsChecker) |
| | 86 | credentialInterfaces = (ITokenCredentials,) |
| | 87 | |
| | 88 | is_startup = True |
| | 89 | |
| | 90 | def requestAvatarId(self, credentials): |
| | 91 | from twisted.python import log |
| | 92 | if self.token and credentials.checkToken(self.token): |
| | 93 | if self.is_startup: |
| | 94 | self.is_startup = False |
| | 95 | self.token = credentials.token = randint(0, 2**128) |
| | 96 | return defer.succeed('admin') |
| | 97 | else: |
| | 98 | return defer.fail("Bad token") |
| 69 | 99 | |
| 70 | 100 | |
| 71 | 101 | class LoginSystem(object): |
diff -r addc96bacb8b -r 95ffb0a687e9 sage/server/notebook/guard.py
|
a
|
b
|
class MySessionWrapper(object): |
| 149 | 149 | Inital logic occurs here to decide the |
| 150 | 150 | authentication status of a given user. |
| 151 | 151 | """ |
| 152 | | #log.msg("request: %s, segments: %s" % (str(request.args), segments)) |
| 153 | | #see if the user already has a session going |
| 154 | 152 | if segments and segments[0] == "login": |
| 155 | 153 | #log.msg("Login") |
| 156 | 154 | #get the username and password in the postdata |
| … |
… |
class MySessionWrapper(object): |
| 160 | 158 | l.addCallback(lambda _: self.requestPasswordAuthentication(request, segments)) |
| 161 | 159 | return l |
| 162 | 160 | |
| | 161 | if segments and segments[0] == "": |
| | 162 | if request.args.get('startup_token', [''])[0]: |
| | 163 | return self.requestPasswordAuthentication(request, segments) |
| | 164 | |
| | 165 | #see if the user already has a session going |
| 163 | 166 | session = self.getSession(request) |
| | 167 | log.msg("session: %s" % session) |
| 164 | 168 | if session is None: |
| 165 | | # log.msg("unknown session") |
| | 169 | #log.msg("unknown session") |
| 166 | 170 | return self.requestAnonymousAuthentication(request, segments) |
| 167 | 171 | else: |
| 168 | 172 | if segments and segments[0] == "logout": |
| … |
… |
class MySessionWrapper(object): |
| 238 | 242 | mind = [newCookie, None, None] |
| 239 | 243 | d = self.portal.login(creds, mind, self.credInterface) |
| 240 | 244 | d.addCallback(_success, request, segments) |
| 241 | | #d.addErrback(self._loginFailure, request, segments, 'Anonymous access not allowed.') |
| 242 | 245 | return d |
| 243 | 246 | |
| 244 | 247 | def getSession(self, request): |
| … |
… |
class MySessionWrapper(object): |
| 258 | 261 | or if they dont exist we have an anonymous |
| 259 | 262 | session |
| 260 | 263 | """ |
| | 264 | if request.args.get('startup_token', [''])[0]: |
| | 265 | import avatars |
| | 266 | return avatars.TokenCred(request.args.get('startup_token', [''])[0]) |
| 261 | 267 | username = request.args.get('email', [''])[0] |
| 262 | 268 | password = request.args.get('password', [''])[0] |
| 263 | 269 | return credentials.UsernamePassword(username, password) |
| … |
… |
class MySessionWrapper(object): |
| 269 | 275 | Also saved the credentials that the user used to log in |
| 270 | 276 | to later associate these credentials with the users session. |
| 271 | 277 | """ |
| | 278 | #log.msg("=== _loginSuccess ===") |
| 272 | 279 | session.set_authCreds(creds) |
| 273 | 280 | return rsrc, () |
| 274 | 281 | |
| 275 | 282 | def _loginFailure(self, *x): #TODO |
| 276 | | #log.msg("=== _loginFailure ===") |
| 277 | | print x |
| | 283 | log.msg("=== _loginFailure ===") |
| | 284 | |
| | 285 | log.msg(str(x)) |
| 278 | 286 | |
| 279 | 287 | def incorrectLoginError(self, error, ctx, segments, loginFailure): |
| 280 | 288 | pass |
diff -r addc96bacb8b -r 95ffb0a687e9 sage/server/notebook/run_notebook.py
|
a
|
b
|
def notebook_twisted(self, |
| 143 | 143 | address, port, secure) |
| 144 | 144 | |
| 145 | 145 | if open_viewer: |
| 146 | | open_page = "from sage.server.misc import open_page; open_page('%s', %s, %s, '%s')"%(address, port, secure, start_path) |
| | 146 | if secure: |
| | 147 | start_path = "'/?startup_token=%s' % startup_token" |
| | 148 | else: |
| | 149 | start_path = "'" + start_path + "'" |
| | 150 | open_page = "from sage.server.misc import open_page; open_page('%s', %s, %s, %s)"%(address, port, secure, start_path) |
| 147 | 151 | else: |
| 148 | 152 | open_page = '' |
| 149 | 153 | |
| … |
… |
import sage.server.notebook.worksheet as |
| 168 | 172 | import sage.server.notebook.worksheet as worksheet |
| 169 | 173 | worksheet.init_sage_prestart(twist.notebook.get_server(), twist.notebook.get_ulimit()) |
| 170 | 174 | |
| 171 | | import signal, sys |
| | 175 | import signal, sys, random |
| 172 | 176 | def my_sigint(x, n): |
| 173 | 177 | twist.notebook.save() |
| 174 | 178 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
| … |
… |
from twisted.cred import portal |
| 190 | 194 | |
| 191 | 195 | realm = avatars.LoginSystem() |
| 192 | 196 | p = portal.Portal(realm) |
| | 197 | startup_token = '%%x' %% random.randint(0, 2**128) |
| | 198 | startup_checker = avatars.OneTimeTokenChecker() |
| | 199 | startup_checker.token = startup_token |
| | 200 | p.registerChecker(startup_checker) |
| 193 | 201 | password_checker = avatars.PasswordChecker() |
| 194 | 202 | p.registerChecker(password_checker) |
| 195 | 203 | p.registerChecker(checkers.AllowAnonymousAccess()) |
diff -r addc96bacb8b -r 95ffb0a687e9 sage/server/notebook/twist.py
|
a
|
b
|
def notebook_updates(): |
| 102 | 102 | ###################################################################################### |
| 103 | 103 | # RESOURCES |
| 104 | 104 | ###################################################################################### |
| | 105 | |
| 105 | 106 | |
| 106 | 107 | ############################ |
| 107 | 108 | # An error message |
| … |
… |
class UserToplevel(Toplevel): |
| 1686 | 1687 | child_upload = Upload() |
| 1687 | 1688 | child_logout = Logout() |
| 1688 | 1689 | |
| | 1690 | |
| 1689 | 1691 | |
| 1690 | 1692 | #child_login = RedirectLogin() |
| 1691 | 1693 | |