~lazr-developers/lazr.sshserver/trunk

« back to all changes in this revision

Viewing changes to src/lazr/sshserver/auth.py

  • Committer: Colin Watson
  • Date: 2020-10-27 09:15:26 UTC
  • mfrom: (88.1.1 py3-username-bytes)
  • Revision ID: cjwatson@canonical.com-20201027091526-8dn5bqfanft9solm
[r=ilasc] Fix lazr.sshserver.auth.PublicKeyFromLaunchpadChecker on Python 3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
154
154
            endpoint.
155
155
        :param username: The username to look up.
156
156
        """
 
157
        username = username.decode('UTF-8')
157
158
        if username in self.cache:
158
159
            return defer.succeed(self.cache[username])
159
160
        else:
331
332
        We check the key data in credentials against the keys the named user
332
333
        has registered in Launchpad.
333
334
        """
 
335
        try:
 
336
            username = credentials.username.decode('UTF-8')
 
337
        except UnicodeDecodeError:
 
338
            # Launchpad account names must be valid UTF-8.
 
339
            return defer.fail(UserDisplayedUnauthorizedLogin(
 
340
                "No such Launchpad account: %r" % credentials.username))
334
341
        d = credentials.mind.lookupUserDetails(
335
342
            self.authserver, credentials.username)
336
343
        d.addCallback(self._checkForAuthorizedKey, credentials)
337
 
        d.addErrback(self._reportNoSuchUser, credentials)
 
344
        d.addErrback(self._reportNoSuchUser, username)
338
345
        return d
339
346
 
340
 
    def _reportNoSuchUser(self, failure, credentials):
341
 
        """Report the user named in the credentials not existing nicely."""
 
347
    def _reportNoSuchUser(self, failure, username):
 
348
        """Report that the given username does not exist."""
342
349
        failure.trap(xmlrpc.Fault)
343
350
        fault = failure.value
344
351
        if fault.faultCode == NoSuchPersonWithName.error_code:
345
352
            raise UserDisplayedUnauthorizedLogin(
346
 
                "No such Launchpad account: %s" % credentials.username)
 
353
                "No such Launchpad account: %s" % username)
347
354
        raise failure
348
355
 
349
356
    def _checkForAuthorizedKey(self, user_dict, credentials):
362
369
 
363
370
        if len(user_dict['keys']) == 0:
364
371
            raise UserDisplayedUnauthorizedLogin(
365
 
                "Launchpad user %r doesn't have a registered SSH key"
366
 
                % credentials.username)
 
372
                "Launchpad user '%s' doesn't have a registered SSH key"
 
373
                % user_dict['name'])
367
374
 
368
375
        for keytype, keytext in user_dict['keys']:
369
376
            if keytype != wantKeyType:
376
383
 
377
384
        raise UnauthorizedLogin(
378
385
            "Your SSH key does not match any key registered for Launchpad "
379
 
            "user %s" % credentials.username)
 
386
            "user %s" % user_dict['name'])
380
387
 
381
388
    def _verifyKey(self, validKey, credentials):
382
389
        """Check whether the credentials themselves are valid.