def check_token_for_actor(self, actor): """Checks that the given actor is public and matches the request's token. Raises: :class:`HTTPException` with HTTP 400 """ if not actor: self.abort(400, f'Missing actor!') if not gr_source.Source.is_public(actor): self.abort( 400, f'Your {self.gr_source().NAME} account is private. Bridgy only supports public accounts.' ) token = util.get_required_param(self, 'token') domains = set( util.domain_from_link(util.replace_test_domains_with_localhost(u)) for u in microformats2.object_urls(actor)) domains.discard(self.source_class().GR_CLASS.DOMAIN) logging.info(f'Checking token against domains {domains}') for domain in ndb.get_multi(ndb.Key(Domain, d) for d in domains): if domain and token in domain.tokens: return self.abort(403, f'Token {token} is not authorized for any of: {domains}')
def get_author_urls(self): """Determine the author urls for a particular source. In debug mode, replace test domains with localhost. Return: a list of string URLs, possibly empty """ return [util.replace_test_domains_with_localhost(u) for u in self.domain_urls]
def get_author_url(self): """Determine the author url for a particular source. In debug mode, replace test domains with localhost Return: a string, the author's url or None """ return (util.replace_test_domains_with_localhost(self.domain_urls[0]) if self.domain_urls else None)
def add_or_update_domain(): domain = Domain.get_or_insert( util.domain_from_link( util.replace_test_domains_with_localhost( auth_entity.key.id()))) domain.auth = auth_entity.key if state not in domain.tokens: domain.tokens.append(state) domain.put() flash(f'Authorized you for {domain.key.id()}.')
def add_original_post_urls(self, post_id, obj, prop): """Extracts original post URLs and adds them to an object, in place. If the post object has upstreamDuplicates, *only* they are considered original post URLs and added as tags with objectType 'article', and the post's own links and 'article' tags are added with objectType 'mention'. Args: post_id: string post id obj: ActivityStreams post object prop: string property name in obj to add the original post URLs to """ post = None try: post = self.source.get_post(post_id) except: logging.warning('Error fetching source post %s', post_id, exc_info=True) return if not post: logging.warning('Source post %s not found', post_id) return original_post_discovery.discover(self.source, post, fetch_hfeed=False) tags = [tag for tag in post['object'].get('tags', []) if 'url' in tag and tag['objectType'] == 'article'] upstreams = post['object'].get('upstreamDuplicates', []) if not isinstance(obj.setdefault(prop, []), list): obj[prop] = [obj[prop]] if upstreams: obj[prop] += [{'url': url, 'objectType': 'article'} for url in upstreams] obj.setdefault('tags', []).extend( [{'url': tag.get('url'), 'objectType': 'mention'} for tag in tags]) else: obj[prop] += tags # check for redirects, and if there are any follow them and add final urls # in addition to the initial urls. seen = set() for url_list in obj[prop], obj.get('tags', []): for url_obj in url_list: url = util.clean_webmention_url(url_obj.get('url', '')) if not url or url in seen: continue seen.add(url) # when debugging locally, replace my (snarfed.org) URLs with localhost url_obj['url'] = url = util.replace_test_domains_with_localhost(url) resolved, _, send = util.get_webmention_target(url) if send and resolved != url and resolved not in seen: seen.add(resolved) url_list.append({'url': resolved, 'objectType': url_obj.get('objectType')}) logging.info('After original post discovery, urls are: %s', seen)
def finish(self, auth_entity, state=None): if not auth_entity: return assert state domain = Domain.get_or_insert( util.domain_from_link( util.replace_test_domains_with_localhost( auth_entity.key.id()))) domain.auth = auth_entity.key if state not in domain.tokens: domain.tokens.append(state) domain.put() self.messages.add(f'Authorized you for {domain.key.id()}.') self.redirect('/')
def add_original_post_urls(self, post, obj, prop): """Extracts original post URLs and adds them to an object, in place. If the post object has upstreamDuplicates, *only* they are considered original post URLs and added as tags with objectType 'article', and the post's own links and 'article' tags are added with objectType 'mention'. Args: post: ActivityStreams post object to get original post URLs from obj: ActivityStreams post object to add original post URLs to prop: string property name in obj to add the original post URLs to """ original_post_discovery.discover(self.source, post, fetch_hfeed=False) tags = [tag for tag in post['object'].get('tags', []) if 'url' in tag and tag['objectType'] == 'article'] upstreams = post['object'].get('upstreamDuplicates', []) if not isinstance(obj.setdefault(prop, []), list): obj[prop] = [obj[prop]] if upstreams: obj[prop] += [{'url': url, 'objectType': 'article'} for url in upstreams] obj.setdefault('tags', []).extend( [{'url': tag.get('url'), 'objectType': 'mention'} for tag in tags]) else: obj[prop] += tags # check for redirects, and if there are any follow them and add final urls # in addition to the initial urls. seen = set() tags = obj.get('tags', []) for url_list in obj[prop], tags: for url_obj in url_list: url = util.clean_webmention_url(url_obj.get('url', '')) if not url or url in seen: continue seen.add(url) # when debugging locally, replace my (snarfed.org) URLs with localhost url_obj['url'] = url = util.replace_test_domains_with_localhost(url) resolved, _, send = util.get_webmention_target(url) if send and resolved != url and resolved not in seen: seen.add(resolved) url_list.append({'url': resolved, 'objectType': url_obj.get('objectType')}) # if the http version of a link is in upstreams but the https one is just a # mention, or vice versa, promote them both to upstream. # https://github.com/snarfed/bridgy/issues/290 # # TODO: for links that came from resolving redirects above, this doesn't # also catch the initial pre-redirect link. ah well. prop_schemeful = set(tag['url'] for tag in obj[prop] if tag.get('url')) prop_schemeless = set(util.schemeless(url) for url in prop_schemeful) for url_obj in copy.copy(tags): url = url_obj.get('url', '') schemeless = util.schemeless(url) if schemeless in prop_schemeless and url not in prop_schemeful: obj[prop].append(url_obj) tags.remove(url_obj) prop_schemeful.add(url) logging.info('After original post discovery, urls are: %s', seen)