Add Twitter revoke view
This commit is contained in:
parent
f21b626802
commit
885a0a52b7
6 changed files with 50 additions and 5 deletions
|
|
@ -84,3 +84,7 @@ class User(AbstractUser):
|
||||||
def delete(self, *args, **kwargs):
|
def delete(self, *args, **kwargs):
|
||||||
self.task.delete()
|
self.task.delete()
|
||||||
return super().delete(*args, **kwargs)
|
return super().delete(*args, **kwargs)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def has_twitter_auth(self):
|
||||||
|
return self.twitter_oauth_token and self.twitter_oauth_token_secret
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if twitter_revoke_url %}
|
{% if twitter_revoke_url %}
|
||||||
<a class="link button button--twitter" href="#">
|
<a class="link button button--twitter" href="{{ twitter_revoke_url }}">
|
||||||
{% trans "Deauthorize account" %}
|
{% trans "Deauthorize account" %}
|
||||||
</a>
|
</a>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ from newsreader.accounts.views import (
|
||||||
RegistrationView,
|
RegistrationView,
|
||||||
SettingsView,
|
SettingsView,
|
||||||
TwitterAuthRedirectView,
|
TwitterAuthRedirectView,
|
||||||
|
TwitterRevokeRedirectView,
|
||||||
TwitterTemplateView,
|
TwitterTemplateView,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -96,6 +97,11 @@ urlpatterns = [
|
||||||
login_required(TwitterTemplateView.as_view()),
|
login_required(TwitterTemplateView.as_view()),
|
||||||
name="twitter-template",
|
name="twitter-template",
|
||||||
),
|
),
|
||||||
|
path(
|
||||||
|
"settings/integrations/twitter/revoke/",
|
||||||
|
login_required(TwitterRevokeRedirectView.as_view()),
|
||||||
|
name="twitter-revoke",
|
||||||
|
),
|
||||||
path(
|
path(
|
||||||
"settings/integrations",
|
"settings/integrations",
|
||||||
login_required(IntegrationsView.as_view()),
|
login_required(IntegrationsView.as_view()),
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ from newsreader.accounts.views.integrations import (
|
||||||
RedditTemplateView,
|
RedditTemplateView,
|
||||||
RedditTokenRedirectView,
|
RedditTokenRedirectView,
|
||||||
TwitterAuthRedirectView,
|
TwitterAuthRedirectView,
|
||||||
|
TwitterRevokeRedirectView,
|
||||||
TwitterTemplateView,
|
TwitterTemplateView,
|
||||||
)
|
)
|
||||||
from newsreader.accounts.views.password import (
|
from newsreader.accounts.views.password import (
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ from newsreader.news.collection.twitter import (
|
||||||
TWITTER_ACCESS_TOKEN_URL,
|
TWITTER_ACCESS_TOKEN_URL,
|
||||||
TWITTER_AUTH_URL,
|
TWITTER_AUTH_URL,
|
||||||
TWITTER_REQUEST_TOKEN_URL,
|
TWITTER_REQUEST_TOKEN_URL,
|
||||||
|
TWITTER_REVOKE_URL,
|
||||||
)
|
)
|
||||||
from newsreader.news.collection.utils import post
|
from newsreader.news.collection.utils import post
|
||||||
|
|
||||||
|
|
@ -69,11 +70,9 @@ class IntegrationsView(TemplateView):
|
||||||
|
|
||||||
def get_twitter_context(self, **kwargs):
|
def get_twitter_context(self, **kwargs):
|
||||||
twitter_revoke_url = None
|
twitter_revoke_url = None
|
||||||
user = self.request.user
|
|
||||||
|
|
||||||
# TODO add revoke url
|
if self.request.user.has_twitter_auth:
|
||||||
if user.twitter_oauth_token and user.twitter_oauth_token_secret:
|
twitter_revoke_url = reverse_lazy("accounts:twitter-revoke")
|
||||||
twitter_revoke_url = "https://foo/bar"
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"twitter_request_url": reverse_lazy("accounts:twitter-request"),
|
"twitter_request_url": reverse_lazy("accounts:twitter-request"),
|
||||||
|
|
@ -176,6 +175,39 @@ class RedditRevokeRedirectView(RedirectView):
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
# TODO write tests
|
||||||
|
class TwitterRevokeRedirectView(RedirectView):
|
||||||
|
url = reverse_lazy("accounts:integrations")
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
if not request.user.has_twitter_auth:
|
||||||
|
messages.error(request, _("No twitter credentials found"))
|
||||||
|
return super().get(request, *args, **kwargs)
|
||||||
|
|
||||||
|
oauth = OAuth(
|
||||||
|
settings.TWITTER_CONSUMER_ID,
|
||||||
|
client_secret=settings.TWITTER_CONSUMER_SECRET,
|
||||||
|
resource_owner_key=request.user.twitter_oauth_token,
|
||||||
|
resource_owner_secret=request.user.twitter_oauth_token_secret,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
post(TWITTER_REVOKE_URL, auth=oauth)
|
||||||
|
except StreamException:
|
||||||
|
logger.exception("Failed revoking Twitter account")
|
||||||
|
|
||||||
|
messages.error(request, _("Unable revoke Twitter account"))
|
||||||
|
return super().get(request, *args, **kwargs)
|
||||||
|
|
||||||
|
request.user.twitter_oauth_token = None
|
||||||
|
request.user.twitter_oauth_token_secret = None
|
||||||
|
request.user.save()
|
||||||
|
|
||||||
|
messages.success(request, _("Twitter account revoked"))
|
||||||
|
return super().get(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO write tests
|
||||||
class TwitterAuthRedirectView(RedirectView):
|
class TwitterAuthRedirectView(RedirectView):
|
||||||
url = reverse_lazy("accounts:integrations")
|
url = reverse_lazy("accounts:integrations")
|
||||||
|
|
||||||
|
|
@ -210,6 +242,7 @@ class TwitterAuthRedirectView(RedirectView):
|
||||||
|
|
||||||
|
|
||||||
# TODO remove cached tokens
|
# TODO remove cached tokens
|
||||||
|
# TODO write tests
|
||||||
class TwitterTemplateView(TemplateView):
|
class TwitterTemplateView(TemplateView):
|
||||||
template_name = "accounts/views/twitter.html"
|
template_name = "accounts/views/twitter.html"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ TWITTER_API_URL = "https://api.twitter.com/1.1"
|
||||||
TWITTER_REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token"
|
TWITTER_REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token"
|
||||||
TWITTER_AUTH_URL = "https://api.twitter.com/oauth/authorize"
|
TWITTER_AUTH_URL = "https://api.twitter.com/oauth/authorize"
|
||||||
TWITTER_ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token"
|
TWITTER_ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token"
|
||||||
|
TWITTER_REVOKE_URL = f"{TWITTER_API_URL}/oauth/invalidate_token"
|
||||||
|
|
||||||
|
|
||||||
class TwitterBuilder(PostBuilder):
|
class TwitterBuilder(PostBuilder):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue