Example #1
0
def build_service_account_credentials(scope, user=None):
    """
    Builds service account credentials using the configuration stored in :mod:`~ferris3.settings`
    and masquerading as the provided user.
    """
    if not SignedJwtAssertionCredentials:
        raise EnvironmentError(
            "Service account can not be used because PyCrypto is not available. Please install PyCrypto."
        )

    config = _get_config()

    if not isinstance(scope, (list, tuple)):
        scope = [scope]

    key = _generate_storage_key(config['client_email'], scope, user)
    storage = StorageByKeyName(ServiceAccountStorage, key, 'credentials')

    creds = SignedJwtAssertionCredentials(
        service_account_name=config['client_email'],
        private_key=config['private_key'],
        scope=scope,
        prn=user)
    creds.set_store(storage)

    return creds
Example #2
0
def from_private_key(account_name, private_key=None, private_key_path=None,
                     storage=None, storage_path=None, api_version="v3",
                     readonly=False):
    """Create a client for a service account.

    Create a client with an account name and a private key.

     Args:
      account_name: str, the account identifier (probably the account email).
      private_key: str, the private key as a string.
      private_key_path: str, path to a file with the private key in.
      storage: oauth2client.client.Storage, a Storage implementation to store
               credentials.
      storage_path: str, path to a file storage.
      readonly: bool, default False, if True only readonly access is requested
                from GA.
    """
    if not private_key:
        if not private_key_path:
            raise GapyError(
                "Must provide either a private_key or a private_key_file")
        if isinstance(private_key_path, basestring):
            private_key_path = open(private_key_path)
        private_key = private_key_path.read()

    storage = _get_storage(storage, storage_path)

    scope = GOOGLE_API_SCOPE_READONLY if readonly else GOOGLE_API_SCOPE
    credentials = SignedJwtAssertionCredentials(account_name, private_key,
                                                scope)
    credentials.set_store(storage)

    return Client(_build(credentials, api_version))
Example #3
0
def addPointView(request):

    if request.method == 'POST':  # If the form has been submitted...
        form = AddPointForm(request.POST)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass
            
            # Check what waste types have been checked by user
            checked_waste_types = form.cleaned_data['waste_types']
            Paper, Metall, Glass, Cloth, Danger, Plastic, Other = [0, 0, 0, 0, 0, 0, 0]
            if ('Paper' in checked_waste_types): Paper = 1
            if ('Metall' in checked_waste_types): Metall = 1
            if ('Glass' in checked_waste_types): Glass = 1
            if ('Cloth' in checked_waste_types): Cloth = 1
            if ('Danger' in checked_waste_types): Danger = 1
            if ('Plastic' in checked_waste_types): Plastic = 1
            if ('Other' in checked_waste_types): Other = 1

            
            table_id = '1gvB3SedL89vG5r1128nUN5ICyyw7Wio5g1w1mbk'
            sqlQuery = ('INSERT INTO {0} ' +
                        '(Title, Location, Phone, \'Work time\', Comment, ' +
                        'Paper, Metall, Glass, Cloth, Danger, Plastic, Other) ' +
                        'VALUES ' +
                        '(\'{1}\', \'{2}\', \'{3}\', \'{4}\', \'{5}\', \'{6}\', ' +
                        '\'{7}\', \'{8}\', \'{9}\', \'{10}\', \'{11}\', ' +
                        '\'{12}\')').format(
                        table_id, form.cleaned_data['name'].encode('utf-8'), form.cleaned_data['adress'].encode('utf-8'),
                        form.cleaned_data['phone'], form.cleaned_data['work_time'],
                        form.cleaned_data['comment'].encode('utf-8'),
                        Paper, Metall, Glass, Cloth, Danger, Plastic, Other)
            # @todo: add correct waste_types ^^^^^^^^^^^^^^^^
            
            
            f = file(os.path.dirname(os.path.abspath(__file__)) + '/../data/d7509b5300823ddbc6a9d3a709a6804bf912d355-privatekey.p12', 'rb')
            key = f.read()
            f.close()

            credentials = SignedJwtAssertionCredentials(
                '*****@*****.**',
                key,
                scope='https://www.googleapis.com/auth/fusiontables')
            storage = Storage(os.path.dirname(os.path.abspath(__file__)) + '/../data/fusion.dat')
            credentials.set_store(storage)

            http = httplib2.Http()
            http = credentials.authorize(http)
            
            service = build("fusiontables", "v1", http=http)
            response = service.query().sql(sql=sqlQuery).execute(http)
            
            answer = 'not_ok'
            if (response['kind'] == 'fusiontables#sqlresponse'):
                answer = 'ok'
            
            return HttpResponse(answer)
    else:
        form = AddPointForm()  # else show only form

    return render_to_response('addPointForm.html', {'form': form})
def make_service_account_client(scope):
    """Make a Storage API client authenticated with a service account."""
    credential_storage = CredentialStorage(FLAGS.credentials_file)
    creds = credential_storage.get()
    if creds is None or creds.invalid:
        with open(FLAGS.key_file) as fd:
            creds = SignedJwtAssertionCredentials(
                    service_account_name=FLAGS.client_email,
                    private_key=fd.read(),
                    scope=scope)
            creds.set_store(credential_storage)

    http = creds.authorize(httplib2.Http())
    return discovery_build('storage', 'v1beta1', http=http)
Example #5
0
    def test_credentials_refresh_with_storage(self):
        private_key = datafile("privatekey.%s" % self.format)
        credentials = SignedJwtAssertionCredentials(
            "*****@*****.**", private_key, scope="read+write", sub="*****@*****.**"
        )

        filehandle, filename = tempfile.mkstemp()
        os.close(filehandle)
        store = Storage(filename)
        store.put(credentials)
        credentials.set_store(store)

        content = self._credentials_refresh(credentials)

        self.assertEqual(b"Bearer 3/3w", content[b"Authorization"])
        os.unlink(filename)
Example #6
0
    def test_credentials_refresh_with_storage(self):
        private_key = datafile('privatekey.%s' % self.format)
        credentials = SignedJwtAssertionCredentials('*****@*****.**',
                                                    private_key,
                                                    scope='read+write',
                                                    sub='*****@*****.**')

        (filehandle, filename) = tempfile.mkstemp()
        os.close(filehandle)
        store = Storage(filename)
        store.put(credentials)
        credentials.set_store(store)

        content = self._credentials_refresh(credentials)

        self.assertEqual(b'Bearer 3/3w', content[b'Authorization'])
        os.unlink(filename)
  def test_credentials_refresh_with_storage(self):
    private_key = datafile('privatekey.%s' % self.format)
    credentials = SignedJwtAssertionCredentials(
        '*****@*****.**',
        private_key,
        scope='read+write',
        sub='*****@*****.**')

    (filehandle, filename) = tempfile.mkstemp()
    os.close(filehandle)
    store = Storage(filename)
    store.put(credentials)
    credentials.set_store(store)

    content = self._credentials_refresh(credentials)

    self.assertEqual('Bearer 3/3w', content['Authorization'])
    os.unlink(filename)
Example #8
0
def from_private_key(account_name,
                     private_key=None,
                     private_key_path=None,
                     storage=None,
                     storage_path=None,
                     api_version="v3",
                     readonly=False,
                     http_client=None,
                     ga_hook=None):
    """Create a client for a service account.

    Create a client with an account name and a private key.

     Args:
      account_name: str, the account identifier (probably the account email).
      private_key: str, the private key as a string.
      private_key_path: str, path to a file with the private key in.
      storage: oauth2client.client.Storage, a Storage implementation to store
               credentials.
      storage_path: str, path to a file storage.
      readonly: bool, default False, if True only readonly access is requested
                from GA.
      http_client: httplib2.Http, Override the default http client used.
      ga_hook: function, a hook that is called every time a query is made
               against GA.
    """
    if not private_key:
        if not private_key_path:
            raise GapyError(
                "Must provide either a private_key or a private_key_file")
        if isinstance(private_key_path, basestring):
            private_key_path = open(private_key_path)
        private_key = private_key_path.read()

    storage = _get_storage(storage, storage_path)

    scope = GOOGLE_API_SCOPE_READONLY if readonly else GOOGLE_API_SCOPE
    credentials = SignedJwtAssertionCredentials(account_name, private_key,
                                                scope)
    credentials.set_store(storage)

    return Client(_build(credentials, api_version, http_client), ga_hook)
Example #9
0
def build_service_account_credentials(scope, user=None):
    """
    Builds service account credentials using the configuration stored in :mod:`~ferris3.settings`
    and masquerading as the provided user.
    """
    config = _get_config()
    try:
        from oauth2client.service_account import ServiceAccountCredentials
    except ImportError:
        ServiceAccountCredentials = None

    if ServiceAccountCredentials is not None and hasattr(
            ServiceAccountCredentials, "from_json_keyfile_dict"):
        # running oauth2client version 2.0
        creds = ServiceAccountCredentials.from_json_keyfile_dict(config, scope)
        if user is not None:
            creds = creds.create_delegated(user)
    else:
        try:
            from oauth2client.client import SignedJwtAssertionCredentials
        except ImportError:
            raise EnvironmentError(
                "Service account can not be used because PyCrypto is not available. Please install PyCrypto."
            )

        if not isinstance(scope, (list, tuple)):
            scope = [scope]

        creds = SignedJwtAssertionCredentials(
            service_account_name=config['client_email'],
            private_key=config['private_key'],
            scope=scope,
            prn=user)

    key = _generate_storage_key(config['client_email'], scope, user)
    storage = StorageByKeyName(ServiceAccountStorage, key, 'credentials')
    creds.set_store(storage)

    return creds
Example #10
0
def build_credentials(scope, user=None):
    """
    Builds service account credentials using the configuration stored in settings
    and masquerading as the provided user.
    """
    config = get_config()

    if not user:
        user = config['default_user']

    if not isinstance(scope, (list, tuple)):
        scope = [scope]

    key = generate_storage_key(config['client_email'], scope, user)
    storage = StorageByKeyName(ServiceAccountStorage, key, 'credentials')

    creds = SignedJwtAssertionCredentials(
        service_account_name=config['client_email'],
        private_key=config['private_key'],
        scope=scope,
        prn=user)
    creds.set_store(storage)

    return creds
Example #11
0
def build_credentials(scope, user=None):
    """
    Builds service account credentials using the configuration stored in settings
    and masquerading as the provided user.
    """
    if not SignedJwtAssertionCredentials:
        raise EnvironmentError(
            "Service account can not be used because PyCrypto is not available. Please install PyCrypto."
        )

    config = get_config()

    if not isinstance(scope, (list, tuple)):
        scope = [scope]

    key = generate_storage_key(config["client_email"], scope, user)
    storage = StorageByKeyName(ServiceAccountStorage, key, "credentials")

    creds = SignedJwtAssertionCredentials(
        service_account_name=config["client_email"], private_key=config["private_key"], scope=scope, prn=user
    )
    creds.set_store(storage)

    return creds
Example #12
0
def build_credentials(scope, user=None):
    """
    Builds service account credentials using the configuration stored in settings
    and masquerading as the provided user.
    """
    config = get_config()

    if not user:
        user = config['default_user']

    if not isinstance(scope, (list, tuple)):
        scope = [scope]

    key = generate_storage_key(config['client_email'], scope, user)
    storage = StorageByKeyName(ServiceAccountStorage, key, 'credentials')

    creds = SignedJwtAssertionCredentials(
        service_account_name=config['client_email'],
        private_key=config['private_key'],
        scope=scope,
        prn=user)
    creds.set_store(storage)

    return creds
Example #13
0
def build_service_account_credentials(scope, user=None):
    """
    Builds service account credentials using the configuration stored in :mod:`~ferris3.settings`
    and masquerading as the provided user.
    """
    config = _get_config()
    try:
        from oauth2client.service_account import ServiceAccountCredentials
    except ImportError:
        ServiceAccountCredentials = None

    if ServiceAccountCredentials is not None and hasattr(ServiceAccountCredentials, "from_json_keyfile_dict"):
        # running oauth2client version 2.0
        creds = ServiceAccountCredentials.from_json_keyfile_dict(config, scope)
        if user is not None:
            creds = creds.create_delegated(user)
    else:
        try:
            from oauth2client.client import SignedJwtAssertionCredentials
        except ImportError:
            raise EnvironmentError("Service account can not be used because PyCrypto is not available. Please install PyCrypto.")

        if not isinstance(scope, (list, tuple)):
            scope = [scope]

        creds = SignedJwtAssertionCredentials(
            service_account_name=config['client_email'],
            private_key=config['private_key'],
            scope=scope,
            prn=user)

    key = _generate_storage_key(config['client_email'], scope, user)
    storage = StorageByKeyName(ServiceAccountStorage, key, 'credentials')
    creds.set_store(storage)

    return creds
Example #14
0
class G2RSS:
    def __init__(self, config_path, logger):
        self.logger = logger
        self.config_path = config_path
        self.service = None
        self.http = None
        self.auth_time = time.time()

        # Authenticate and construct service.
        # Prepare credentials, and authorize HTTP object with them.
        storage = Storage(os.path.join(config_path, 'plus.dat'))
        self.credentials = storage.get()

        if self.credentials is None or self.credentials.invalid:
            self.init_credentials(storage)

        #precompile regex
        self.re_img = re.compile('(/[^/]+\.jpg)')
        self.re_br = re.compile('\r\n|\n|\r')

    def init_credentials(self, storage):
        self.logger.warning('Invalid credentials')
        f_key = file(os.path.join(self.config_path, 'privatekey.p12'), 'rb')
        key = f_key.read()
        f_key.close()
        f_meta = file(os.path.join(self.config_path, 'client_secrets.json'), 'rb')
        data = json.load(f_meta)
        self.credentials = SignedJwtAssertionCredentials(
            data['web']['client_email'],
            key,
            scope='https://www.googleapis.com/auth/plus.me')
        self.credentials.set_store(storage)

    def authorize(self):
        if self.service is None or time.time() - self.auth_time > 300:
            self.logger.info('Authorizing credentials')
            self.http = self.credentials.authorize(http=httplib2.Http())
            # Construct a service object via the discovery service.
            self.service = discovery.build('plus', 'v1', http=self.http)
            self.auth_time = time.time()

    def get_activities_since(self, user_id, max_results, stamp):
        """
        returns activities only if update timestamp is greater than given stamp
        """
        activities_doc = self.get_activities(user_id, max_results)
        updated = calendar.timegm(parse(activities_doc['updated']))
        if updated <= stamp:
            return None
        return activities_doc

    def get_activities(self, user_id, max_results):
        try:
            if self.credentials is None or self.credentials.invalid:
                raise tornado.web.HTTPError(403, 'Invalid Credentials')
            self.authorize()

            # query data from google
            request = self.service.activities().list(userId=user_id, collection='public', maxResults=max_results)
            activities_doc = request.execute()
            if 'updated' in activities_doc:
                self.logger.info('Received data for [{0}], updated [{1}]'.format(user_id, activities_doc['updated']))
            else:
                self.logger.info('Received empty data set for [{0}]'.format(user_id))

        except errors.HttpError as e:
            self.logger.warning('HttpError: {0}'.format(e.resp))
            return None
            #raise RetryError()

        except BadStatusLine as e:
            self.logger.warning('BadStatusLine: {0}'.format(e.line))
            raise RetryError()

        except Exception as e:
            self.logger.warning('get_activities for [{0}]: exception: {1}:{2}'.format(user_id, type(e), e))
            return None

        return activities_doc

    def render(self, user_id, option='', max_results='4'):
        activities_doc = self.get_activities(user_id, max_results)
        # generate rss items
        return self.process_items(option, activities_doc)

    def result(self, guid, updated, url, title, description, full_image=''):
        return {
            'title': self.re_br.sub(' ', title),
            'link': url,
            'fullImage': full_image,
            'description': description,
            'guid': guid,
            'pubDate': utils.formatdate(calendar.timegm(parse(updated).timetuple()))
        }

    @staticmethod
    def get_reshare_description(item, description):
        if 'annotation' in item:
            annotation = item['annotation']
        else:
            annotation = u''
        description = u'{0} <br> \r\n {1} <br> \r\n{3} ({2}) via {5} ({4})'.format(
            annotation.replace(u'<br>', u'<br>\r\n'),
            description.replace(u'<br>', u'<br>\r\n'),
            item['url'],
            item['actor']['displayName'],
            item['object']['url'],
            item['object']['actor']['displayName'])
        return description

    def process_photo(self, item, share):
        description = item['object']['content']
        title = item['title'][:70]
        #google issue again?
        url = item['object']['attachments'][0]['url']
        if not url.startswith('https://plus.google.com'):
            url = 'https://plus.google.com' + url

        if 'fullImage' in item['object']['attachments'][0]:
            full_image = item['object']['attachments'][0]['fullImage']['url']
            # cater for Google's issue with not providing full-size image links
            if 'width' in item['object']['attachments'][0]['fullImage']:
                strdim = 'w{0}-h{1}'.format(item['object']['attachments'][0]['fullImage']['width'],
                                            item['object']['attachments'][0]['fullImage']['height'])
                if full_image.count(strdim) == 0:
                    full_image = self.re_img.sub('/s0\g<1>', full_image)

            #description += '<br/><img src="{0}" />'.format(fullImage)
            url = full_image
        else:
            full_image = url

        if share:
            description = self.get_reshare_description(item, description)
        return self.result(item['id'], item['updated'], url, title, description, full_image)

    def process_text(self, item, share):
        description = item['object']['content']
        title = item['title'][:70]
        url = item['object']['url']
        if share:
            description = self.get_reshare_description(item, description)
        return self.result(item['id'], item['updated'], url, title, description)

    def process_link(self, item, share):
        description = item['object']['content']
        if 'displayName' in item['object']['attachments'][0]:
            title = item['object']['attachments'][0]['displayName']
        else:
            title = item['title'][:70]
        if 'url' in item['object']['attachments'][0]:
            url = item['object']['attachments'][0]['url']
        elif 'url' in item['object']:
            url = item['object']['url']
        else:
            url = item['url']
        if 'fullImage' in item['object']['attachments'][0]:
            full_image = item['object']['attachments'][0]['fullImage']['url']
        else:
            full_image = ''
        if share:
            description = self.get_reshare_description(item, description)

        return self.result(item['id'], item['updated'], url, title, description, full_image)

    def process_album(self, item, share):
        description = item['object']['content']
        title = item['title'][:70]
        url = item['object']['attachments'][0]['url']
        if not url.startswith('https://plus.google.com'):
            url = 'https://plus.google.com' + url
        if share:
            description = self.get_reshare_description(item, description)
        return self.result(item['id'], item['updated'], url, title, description, url)

    def process_items(self, option, activities_doc):
        #logger.info('Processing: filter={0}'.format(filter))
        for item in activities_doc.get('items', []):

            if 'attachments' in item['object']:
                if 'objectType' in item['object']['attachments'][0]:
                    object_type = item['object']['attachments'][0]['objectType']
                else:
                    object_type = 'link'

                if object_type == 'photo':
                    if option and option.count('photo') == 0:
                        continue
                    yield self.process_photo(item, item['verb'] == 'share')

                elif object_type == 'album':
                    if option and option.count('links') == 0:
                        continue
                    yield self.process_album(item, item['verb'] == 'share')

                else:
                    if option and option.count('links') == 0:
                        continue
                    yield self.process_link(item, item['verb'] == 'share')

            else:
                if option and (option.count('links-') > 0 or option.count('photo') > 0):
                    continue
                yield self.process_text(item, item['verb'] == 'share')