~mvo/ubuntu-sso-client/strawman-lp711413

« back to all changes in this revision

Viewing changes to ubuntu_sso/main.py

  • Committer: natalia.bidart at canonical
  • Date: 2011-01-11 19:13:19 UTC
  • mto: This revision was merged to the branch mainline in revision 668.
  • Revision ID: natalia.bidart@canonical.com-20110111191319-alzmdkexz9oy2y0l
The service should shutdown when unused (LP: #701606).

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
 
51
51
logger = setup_logging("ubuntu_sso.main")
52
52
U1_PING_URL = "https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/"
 
53
TIMEOUT_INTERVAL = 500
53
54
 
54
55
 
55
56
class SSOLoginProcessor(Account):
421
422
 
422
423
    """
423
424
 
 
425
    def __init__(self, timeout_func, shutdown_func, *args, **kwargs):
 
426
        super(CredentialsManagement, self).__init__(*args, **kwargs)
 
427
        self._ref_count = 0
 
428
        self.timeout_func = timeout_func
 
429
        self.shutdown_func = shutdown_func
 
430
 
424
431
    # Operator not preceded by a space (fails with dbus decorators)
425
432
    # pylint: disable=C0322
426
433
 
440
447
        """Process the 'failure' and emit CredentialsError."""
441
448
        self.CredentialsError(app_name, except_to_errdict(failure.value))
442
449
 
 
450
    def _get_ref_count(self):
 
451
        """Get value of ref_count."""
 
452
        logger.debug('ref_count is %r.', self._ref_count)
 
453
        return self._ref_count
 
454
 
 
455
    def _set_ref_count(self, new_value):
 
456
        """Set a new value to ref_count."""
 
457
        logger.debug('ref_count is %r, changing value to %r.',
 
458
                     self._ref_count, new_value)
 
459
        if new_value < 0:
 
460
            self._ref_count = 0
 
461
            msg = 'Attempting to decrease ref_count to a negative value (%r).'
 
462
            logger.warning(msg, new_value)
 
463
        else:
 
464
            self._ref_count = new_value
 
465
 
 
466
        if self._ref_count == 0:
 
467
            self.timeout_func(TIMEOUT_INTERVAL, self.shutdown_func)
 
468
 
 
469
    ref_count = property(fget=_get_ref_count, fset=_set_ref_count)
 
470
 
443
471
    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='s')
444
472
    def AuthorizationDenied(self, app_name):
445
473
        """Signal thrown when the user denies the authorization."""
 
474
        self.ref_count -= 1
446
475
        logger.info('%s: emitting AuthorizationDenied with app_name "%s".',
447
476
                    self.__class__.__name__, app_name)
448
477
 
449
478
    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='sa{ss}')
450
479
    def CredentialsFound(self, app_name, credentials):
451
480
        """Signal thrown when the credentials are found."""
 
481
        self.ref_count -= 1
452
482
        logger.info('%s: emitting CredentialsFound with app_name "%s".',
453
483
                    self.__class__.__name__, app_name)
454
484
 
455
485
    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='s')
456
486
    def CredentialsNotFound(self, app_name):
457
487
        """Signal thrown when the credentials are not found."""
 
488
        self.ref_count -= 1
458
489
        logger.info('%s: emitting CredentialsNotFound with app_name "%s".',
459
490
                    self.__class__.__name__, app_name)
460
491
 
461
492
    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='s')
462
493
    def CredentialsCleared(self, app_name):
463
494
        """Signal thrown when the credentials were cleared."""
 
495
        self.ref_count -= 1
464
496
        logger.info('%s: emitting CredentialsCleared with app_name "%s".',
465
497
                    self.__class__.__name__, app_name)
466
498
 
467
499
    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='s')
468
500
    def CredentialsStored(self, app_name):
469
501
        """Signal thrown when the credentials were cleared."""
 
502
        self.ref_count -= 1
470
503
        logger.info('%s: emitting CredentialsStored with app_name "%s".',
471
504
                    self.__class__.__name__, app_name)
472
505
 
473
506
    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='sa{ss}')
474
507
    def CredentialsError(self, app_name, error_dict):
475
508
        """Signal thrown when there is a problem getting the credentials."""
 
509
        self.ref_count -= 1
476
510
        logger.error('%s: emitting CredentialsError with app_name "%s" and '
477
511
                     'error_dict %r.', self.__class__.__name__, app_name,
478
512
                     error_dict)
488
522
        - 'args' is a dictionary, currently not used.
489
523
 
490
524
        """
 
525
        self.ref_count += 1
491
526
 
492
527
        def success_cb(credentials):
493
528
            """Find credentials and notify using signals."""
513
548
        - 'args' is a dictionary, currently not used.
514
549
 
515
550
        """
 
551
        self.ref_count += 1
 
552
 
516
553
        obj = Credentials(app_name)
517
554
        d = obj.clear_credentials()
518
555
        # pylint: disable=E1101
532
569
        'consumer_secret'.
533
570
 
534
571
        """
 
572
        self.ref_count += 1
 
573
 
535
574
        obj = Credentials(app_name)
536
575
        d = obj.store_credentials(args)
537
576
        # pylint: disable=E1101
542
581
                         in_signature='sa{ss}', out_signature='')
543
582
    def register(self, app_name, args):
544
583
        """Get credentials if found else prompt GUI to register."""
 
584
        self.ref_count += 1
 
585
 
545
586
        obj = Credentials(app_name, **self._parse_args(args))
546
587
        obj.register()
547
588
 
549
590
                         in_signature='sa{ss}', out_signature='')
550
591
    def login(self, app_name, args):
551
592
        """Get credentials if found else prompt GUI to login."""
 
593
        self.ref_count += 1
 
594
 
552
595
        obj = Credentials(app_name, **self._parse_args(args))
553
596
        obj.login()