Пример #1
0
def auth(request):
	try:
		name = request.GET.get('provider')
		provider = helpers.get_provider(name, True)
	except (ImportError, AttributeError):
		raise Http404('Provider %s not found.' % request.GET.get('provider'))
	
	if provider.token_required:
		token, auth_url = provider.get_token_and_auth_url()
		
		if token:
			request.session['unauthed_token'] = token.to_string()
		else:
			request.session['unauthed_token'] = None
	else:
		auth_url = provider.get_authorisation_url()
	
	if 'input_id' in request.GET:
		request.session['preauth_url'] = '::oauth;id=%s' % request.GET['input_id']
	else:
		request.session['preauth_url'] = request.META.get('HTTP_REFERER', '/')
	
	request.session['oauth_provider'] = name
	request.session.modified = True
	
	return HttpResponseRedirect(auth_url)
Пример #2
0
def deauth(request):
	try:
		name = request.GET.get('provider')
		provider = helpers.get_provider(name, True)
	except (ImportError, AttributeError):
		raise Http404('Provider %s not found.' % request.GET.get('provider'))
	
	request.user.megaphone_services.filter(provider = name).delete()
	messages.success(request, 'You\'ve been disconnected from %s.' % provider.verbose_name)
	preauth_url = request.session.get('preauth_url', '/')
	
	return HttpResponseRedirect(preauth_url)
Пример #3
0
	def post(self, obj, title, url, image = None, media = None):
		template = Template(self.message_template)
		model = self.get_model()
		provider = helpers.get_provider(self.service.provider, True)
		
		context = Context(
			{
				'verbose_name': model._meta.verbose_name,
				'verbose_name_plural': model._meta.verbose_name_plural,
				'noun': helpers.get_noun(model),
				'title': title,
				'url': url
			}
		)
		
		message = template.render(context)
		self.posts.post_message(self, obj, message, url, image, media)
Пример #4
0
	def send(self, url = None, image = None, media = None):
		provider = helpers.get_provider(self.action.service.provider, True)
		
		self.image = image or self.image
		self.media = media or self.media
		
		if provider.use_trackable_urls:
			self.url = url and provider.make_link(url) or self.url
		else:
			self.url = url or self.url
		
		access_token = provider.post_message(
			self.action.service.access_token,
			self.message, self.url, self.image, self.media
		)
		
		self.sent = datetime.utcnow().replace(tzinfo = utc)
		self.action.service.access_token = access_token
		self.action.service.save()
Пример #5
0
def callback(request):
	logger = logging.getLogger('bambu.megaphone')
	
	try:
		name = request.session.get('oauth_provider')
		provider = helpers.get_provider(name, True)
	except (ImportError, AttributeError):
		raise Http404('Provider %s not found.' % request.session.get('oauth_provider'))
	
	preauth_url = request.session.get('preauth_url', '/')
	if request.GET.get('error_reason'):
		if preauth_url.startswith('::oauth'):
			input_id = preauth_url[preauth_url.find(';id=') + 4:]
			response = HttpResponse("""<!DOCTYPE html><html><head></head><body>
				<script>opener.document.getElementById('%(input_id)s').value = '';
				window.close();
				</script></body></html>""" % {
					'input_id': input_id,
				}
			)
		else:
			response = HttpResponseRedirect(preauth_url)
		
		return response
	
	authed_token = request.GET.get(provider.oauth_token_GET_param, 'no-token')
	if provider.token_required:
		unauthed_token = request.session.get('unauthed_token', None)
		if unauthed_token and not provider.verify_auth_token(unauthed_token, authed_token):
			messages.error(request,
				'It wasn\'t possible to complete your connection to %s.' % provider.verbose_name
			)
			
			return HttpResponseRedirect(preauth_url)
		
		access_token = provider.verify_and_swap(request)
	else:
		access_token = provider.swap_tokens(authed_token)
	
	if preauth_url.startswith('::oauth'):
		input_id = preauth_url[preauth_url.find(';id=') + 4:]
		return HttpResponse("""<!DOCTYPE html><html><head></head><body>
			<script>opener.document.getElementById('%(input_id)s').value = '%(value)s';
			opener.document.getElementById('%(input_id)s_link').innerHTML = 'Connected to %(name)s';
			opener.document.getElementById('%(input_id)s_link').setAttribute('disabled', 'disabled');
			opener.document.getElementById('%(input_id)s_link').removeAttribute('href');
			window.close();
			</script></body></html>""" % {
				'input_id': input_id,
				'value': access_token,
				'name': provider.verbose_name
			}
		)
	else:
		request.user.megaphone_services.create(
			provider = name,
			access_token = unicode(access_token)
		)
		
		messages.success(request, 'You are now successully connected to %s.' % provider.verbose_name)
		return HttpResponseRedirect(preauth_url)
Пример #6
0
	def save(self, *args, **kwargs):
		if self.access_token and not self.identity:
			provider = helpers.get_provider(self.provider, True)
			self.identity = provider.get_identity(self.access_token)
		
		super(Service, self).save(*args, **kwargs)
Пример #7
0
	def get_identity_url(self):
		provider = helpers.get_provider(self.provider, True)
		return provider.public and provider.get_identity_link(self.identity) or ''
Пример #8
0
	def get_provider_display(self):
		provider = helpers.get_provider(self.provider, True)
		return providler.verbose_name