Exemplo n.º 1
0
def test_account(db, config):
    """Creates a fake account, ensuring that the refresh token can be retrieved.
    It also removes the account to ensure that we are not leaking secrets."""

    # Add the account
    email = "*****@*****.**"
    resp = {
        'access_token': '',
        'expires_in': 3600,
        'refresh_token': TV,
        'scope': '',
        'email': email,
        'family_name': '',
        'given_name': '',
        'name': '',
        'gender': '',
        'id': 0,
        'user_id': '',
        'id_token': '',
        'link': 'http://example.com',
        'locale': '',
        'picture': '',
        'hd': ''
    }
    account = create_account(db.session, email, resp)
    db.session.add(account)
    db.session.commit()

    # Check the account
    assert account.refresh_token == TV

    secret_id = account.refresh_token_id
    assert vault.get(secret_id) == TV

    # Make sure updating the account doesn't affect the stored valuet
    account.name = "new_name"
    db.session.add(account)
    db.session.commit()
    assert account.refresh_token == TV

    # Remove the account
    db.session.delete(account)
    db.session.commit()

    # make sure the account was deleted
    found = None
    try:
        found = db.session.query(GmailAccount) \
            .filter_by(email_address=email).one()
    except NoResultFound:
        pass
    assert not found

    # Ensure secrets aren't leaked
    try:
        found = vault.get(secret_id)
    except NotFound:
        pass

    assert not found
Exemplo n.º 2
0
def test_account(db, config):
    """Creates a fake account, ensuring that the refresh token can be retrieved.
    It also removes the account to ensure that we are not leaking secrets."""

    # Add the account
    email = "*****@*****.**"
    resp = {'access_token': '',
            'expires_in': 3600,
            'refresh_token': TV,
            'scope': '',
            'email': email,
            'family_name': '',
            'given_name': '',
            'name': '',
            'gender': '',
            'id': 0,
            'user_id': '',
            'id_token': '',
            'link': 'http://example.com',
            'locale': '',
            'picture': '',
            'hd': ''}
    account = create_account(db.session, email, resp)
    db.session.add(account)
    db.session.commit()

    # Check the account
    assert account.refresh_token == TV

    secret_id = account.refresh_token_id
    assert vault.get(secret_id) == TV

    # Make sure updating the account doesn't affect the stored valuet
    account.name = "new_name"
    db.session.add(account)
    db.session.commit()
    assert account.refresh_token == TV

    # Remove the account
    db.session.delete(account)
    db.session.commit()

    # make sure the account was deleted
    found = None
    try:
        found = db.session.query(GmailAccount) \
            .filter_by(email_address=email).one()
    except NoResultFound:
        pass
    assert not found

    # Ensure secrets aren't leaked
    try:
        found = vault.get(secret_id)
    except NotFound:
        pass

    assert not found
Exemplo n.º 3
0
def test_token(db, config):
    """
    Ensure tokens are encrypted.
    Ensure tokens are decrypted correctly on retrieval.
    Ensure tokens are not leaked.

    Note: This tests refresh_tokens but passwords work in the same way

    """
    token = 'tH*$&123abcº™™∞'

    email = '*****@*****.**'
    resp = {'access_token': '',
            'expires_in': 3600,
            'refresh_token': token,
            'scope': '',
            'email': email,
            'family_name': '',
            'given_name': '',
            'name': '',
            'gender': '',
            'id': 0,
            'user_id': '',
            'id_token': '',
            'link': 'http://example.com',
            'locale': '',
            'picture': '',
            'hd': ''}
    account = create_account(db.session, email, resp)

    db.session.add(account)
    db.session.commit()

    secret_id = account.refresh_token_id
    secret = db.session.query(Secret).get(secret_id)

    assert secret._secret != token, 'token not encrypted'

    decrypted_secret = secret.secret
    assert decrypted_secret == token and \
        account.refresh_token == decrypted_secret, \
        'token not decrypted correctly'

    db.session.delete(account)
    db.session.commit()

    account = db.session.query(GmailAccount).filter_by(
        email_address=email).scalar()
    assert not account

    secret = db.session.query(Secret).filter(Secret.id == secret_id).scalar()
    assert not secret, 'token not deleted on account deletion'
Exemplo n.º 4
0
def test_token(db, config, encrypt):
    """
    If encryption is enabled, ensure that:
    * tokens are encrypted.
    * tokens are decrypted correctly on retrieval.

    Note: This tests refresh_tokens but passwords work in the same way

    """
    config['ENCRYPT_SECRETS'] = encrypt
    token = 'tH*$&123abcº™™∞'

    email = '*****@*****.**'
    resp = {
        'access_token': '',
        'expires_in': 3600,
        'refresh_token': token,
        'scope': '',
        'email': email,
        'family_name': '',
        'given_name': '',
        'name': '',
        'gender': '',
        'id': 0,
        'user_id': '',
        'id_token': '',
        'link': 'http://example.com',
        'locale': '',
        'picture': '',
        'hd': ''
    }
    account = create_account(db.session, resp)

    db.session.add(account)
    db.session.commit()

    secret_id = account.refresh_token_id
    secret = db.session.query(Secret).get(secret_id)

    assert secret == account.secret

    if encrypt:
        assert secret._secret != token, 'token not encrypted'
    else:
        assert secret._secret == token, 'token encrypted when encryption disabled'

    decrypted_secret = secret.secret
    assert decrypted_secret == token and \
        account.refresh_token == decrypted_secret, \
        'token not decrypted correctly'
Exemplo n.º 5
0
def upgrade():
    from inbox.models.session import session_scope
    from inbox.ignition import main_engine
    engine = main_engine(pool_size=1, max_overflow=0)
    from inbox.models.backends.imap import ImapAccount
    import inbox.auth.gmail as gmail

    # Assert we have the dump file
    if not os.path.isfile(SQL_DUMP_FILENAME):
        print "Can't find old user SQL dump at {0}...\nMigration no users."\
            .format(SQL_DUMP_FILENAME)
        return

    # Imports to `imapaccount_old` table
    with open(SQL_DUMP_FILENAME, 'r') as f:
        print 'Importing old account data...',
        op.execute(f.read())
        print 'OK!'

    Base = declarative_base()
    Base.metadata.reflect(engine)

    class ImapAccount_Old(Base):
        __table__ = Base.metadata.tables['imapaccount_old']

    with session_scope() as db_session:
        migrated_accounts = []

        for acct in db_session.query(ImapAccount_Old):
            print 'Importing {0}'. format(acct.email_address)

            existing_account = db_session.query(ImapAccount)\
                .filter_by(email_address=acct.email_address)
            if existing_account.count() > 0:
                print 'Already have account for {0}' \
                    .format(acct.email_address)
                continue

            # Create a mock OAuth response using data from the old table
            mock_response = dict(
                email=acct.email_address,
                issued_to=acct.o_token_issued_to,
                user_id=acct.o_user_id,
                access_token=acct.o_access_token,
                id_token=acct.o_id_token,
                expires_in=acct.o_expires_in,
                access_type=acct.o_access_type,
                token_type=acct.o_token_type,
                audience=acct.o_audience,
                scope=acct.o_scope,
                refresh_token=acct.o_refresh_token,
                verified_email=acct.o_verified_email
            )

            new_account = gmail.create_account(db_session,
                                               acct.email_address,
                                               mock_response)

            # Note that this doesn't verify **anything** about the account.
            # We're just doing the migration now
            db_session.add(new_account)
            db_session.commit()
            migrated_accounts.append(new_account)

        print '\nDone! Imported {0} accounts.'.format(len(migrated_accounts))
        print '\nNow verifying refresh tokens...\n'

        verified_accounts = []
        for acct in migrated_accounts:
            print 'Verifying {0}... '.format(acct.email_address),
            if gmail.verify_account(acct):
                verified_accounts.append(acct)
                print 'OK!'
            else:
                print 'FAILED!'

        print 'Done! Verified {0} of {1}'.format(len(verified_accounts),
                                                 len(migrated_accounts))

    op.drop_table('imapaccount_old')
Exemplo n.º 6
0
def upgrade():
    from inbox.models.session import session_scope
    from inbox.ignition import main_engine
    engine = main_engine(pool_size=1, max_overflow=0)
    from inbox.models.backends.imap import ImapAccount
    import inbox.auth.gmail as gmail

    # Assert we have the dump file
    if not os.path.isfile(SQL_DUMP_FILENAME):
        print "Can't find old user SQL dump at {0}...\nMigration no users."\
            .format(SQL_DUMP_FILENAME)
        return

    # Imports to `imapaccount_old` table
    with open(SQL_DUMP_FILENAME, 'r') as f:
        print 'Importing old account data...',
        op.execute(f.read())
        print 'OK!'

    Base = declarative_base()
    Base.metadata.reflect(engine)

    class ImapAccount_Old(Base):
        __table__ = Base.metadata.tables['imapaccount_old']

    with session_scope() as db_session:
        migrated_accounts = []

        for acct in db_session.query(ImapAccount_Old):
            print 'Importing {0}'.format(acct.email_address)

            existing_account = db_session.query(ImapAccount)\
                .filter_by(email_address=acct.email_address)
            if existing_account.count() > 0:
                print 'Already have account for {0}' \
                    .format(acct.email_address)
                continue

            # Create a mock OAuth response using data from the old table
            mock_response = dict(email=acct.email_address,
                                 issued_to=acct.o_token_issued_to,
                                 user_id=acct.o_user_id,
                                 access_token=acct.o_access_token,
                                 id_token=acct.o_id_token,
                                 expires_in=acct.o_expires_in,
                                 access_type=acct.o_access_type,
                                 token_type=acct.o_token_type,
                                 audience=acct.o_audience,
                                 scope=acct.o_scope,
                                 refresh_token=acct.o_refresh_token,
                                 verified_email=acct.o_verified_email)

            new_account = gmail.create_account(db_session, acct.email_address,
                                               mock_response)

            # Note that this doesn't verify **anything** about the account.
            # We're just doing the migration now
            db_session.add(new_account)
            db_session.commit()
            migrated_accounts.append(new_account)

        print '\nDone! Imported {0} accounts.'.format(len(migrated_accounts))
        print '\nNow verifying refresh tokens...\n'

        verified_accounts = []
        for acct in migrated_accounts:
            print 'Verifying {0}... '.format(acct.email_address),
            if gmail.verify_account(acct):
                verified_accounts.append(acct)
                print 'OK!'
            else:
                print 'FAILED!'

        print 'Done! Verified {0} of {1}'.format(len(verified_accounts),
                                                 len(migrated_accounts))

    op.drop_table('imapaccount_old')