Add Twitter as an Oauth1 login provider
authorMagnus Hagander <magnus@hagander.net>
Tue, 25 Jun 2019 14:27:12 +0000 (16:27 +0200)
committerMagnus Hagander <magnus@hagander.net>
Tue, 25 Jun 2019 14:27:12 +0000 (16:27 +0200)
media/img/misc/btn_login_twitter.png [new file with mode: 0644]
pgweb/account/oauthclient.py

diff --git a/media/img/misc/btn_login_twitter.png b/media/img/misc/btn_login_twitter.png
new file mode 100644 (file)
index 0000000..3258f33
Binary files /dev/null and b/media/img/misc/btn_login_twitter.png differ
index e832d05d6b0f40a2c4ea5d07450e8de6b8bb55b7..86fc8b9bd58a69697c354cbbd8b8121fca7a1f6f 100644 (file)
@@ -256,6 +256,33 @@ def oauth_login_microsoft(request):
         _microsoft_auth_data)
 
 
+#
+# Twitter login
+#  Registration: https://developer.twitter.com/en/apps
+#
+def oauth_login_twitter(request):
+    def _twitter_auth_data(oa):
+        r = oa.get('https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true').json()
+        n = r['name'].split(None, 1)
+        while len(n) < 2:
+            # Handle single name names
+            n.append('')
+
+        return (
+            r['email'],
+            n[0],
+            n[1]
+        )
+
+    return _login_oauth1(
+        request,
+        'twitter',
+        'https://api.twitter.com/oauth/request_token',
+        'https://api.twitter.com/oauth/access_token',
+        'https://api.twitter.com/oauth/authorize',
+        _twitter_auth_data)
+
+
 def login_oauth(request, provider):
     fn = 'oauth_login_{0}'.format(provider)
     m = sys.modules[__name__]