From bf14a0af4d6857da21ecb019a54d7f06c676f1b9 Mon Sep 17 00:00:00 2001 From: Harshit Bansal Date: Sat, 2 Feb 2019 15:51:26 +0000 Subject: [PATCH] auth: Migrate google auth to python-social-auth. This replaces the two custom Google authentication backends originally written in 2012 with using the shared python-social-auth codebase that we already use for the GitHub authentication backend. These are: * GoogleMobileOauth2Backend, the ancient code path for mobile authentication last used by the EOL original Zulip Android app. * The `finish_google_oauth2` code path in zerver/views/auth.py, which was the webapp (and modern mobile app) Google authentication code path. This change doesn't fix any known bugs; its main benefit is that we get to remove hundreds of lines of security-sensitive semi-duplicated code, replacing it with a widely trusted, high quality third-party library. --- docs/production/authentication-methods.md | 2 +- docs/subsystems/auth.md | 10 +- requirements/common.in | 5 - requirements/dev.txt | 12 +- requirements/prod.txt | 12 +- static/js/portico/signup.js | 3 +- static/styles/portico/portico-signin.scss | 3 +- templates/zerver/accounts_home.html | 12 - templates/zerver/config_error.html | 4 +- templates/zerver/google-error.md | 4 +- templates/zerver/login.html | 13 +- version.py | 2 +- zerver/tests/test_auth_backends.py | 516 +++------------------- zerver/tests/test_docs.py | 20 +- zerver/tests/test_events.py | 2 +- zerver/views/auth.py | 163 +------ zproject/backends.py | 60 +-- zproject/dev_settings.py | 2 +- zproject/prod_settings_template.py | 10 +- zproject/settings.py | 14 +- zproject/test_settings.py | 2 + zproject/urls.py | 7 - 22 files changed, 146 insertions(+), 732 deletions(-) diff --git a/docs/production/authentication-methods.md b/docs/production/authentication-methods.md index 737c7add7e..de75784854 100644 --- a/docs/production/authentication-methods.md +++ b/docs/production/authentication-methods.md @@ -26,7 +26,7 @@ creating the initial realm and user. You can disable it after that. With just a few lines of configuration, your Zulip server can authenticate users with any of several single-sign-on (SSO) authentication providers: -* Google accounts, with `GoogleMobileOauth2Backend` +* Google accounts, with `GoogleAuthBackend` * GitHub accounts, with `GitHubAuthBackend` * Microsoft Azure Active Directory, with `AzureADAuthBackend` diff --git a/docs/subsystems/auth.md b/docs/subsystems/auth.md index 0e209d9b33..8eac1ae0ea 100644 --- a/docs/subsystems/auth.md +++ b/docs/subsystems/auth.md @@ -32,17 +32,17 @@ Here are the full procedures for dev: services" > "Credentials". Create a "Project" which will correspond to your dev environment. -* Navigate to "APIs & services" > "Library", and find the "Google+ - API". Choose "Enable". +* Navigate to "APIs & services" > "Library", and find the "Identity + Toolkit API". Choose "Enable". * Return to "Credentials", and select "Create credentials". Choose "OAuth client ID", and follow prompts to create a consent screen, etc. For "Authorized redirect URIs", fill in - `https://zulipdev.com:9991/accounts/login/google/done/` . + `http://zulipdev.com:9991/complete/google/` . * You should get a client ID and a client secret. Copy them. In - `dev-secrets.conf`, set `google_auth2_client_id` to the client ID - and `google_oauth2_client_secret` to the client secret. + `dev-secrets.conf`, set `social_auth_google_key` to the client ID + and `social_auth_google_secret` to the client secret. ### GitHub diff --git a/requirements/common.in b/requirements/common.in index d50969940d..d4119a5c59 100644 --- a/requirements/common.in +++ b/requirements/common.in @@ -46,9 +46,6 @@ django-statsd-mozilla==0.4.0 # Needed for Android push notifications python-gcm==0.4 -# Needed for Google Apps mobile auth -google-api-python-client==1.7.4 - # Needed for the email mirror html2text==2018.1.9 httplib2==0.12.3 @@ -72,8 +69,6 @@ markdown-include==0.5.1 # Needed for mock objects in decorators mock==2.0.0 -oauth2client==4.1.3 - # Needed to access rabbitmq # See #8466 for why we're not using the latest version. pika==0.13.0 diff --git a/requirements/dev.txt b/requirements/dev.txt index 6c3d4f4c8e..7e28bcd3a0 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -29,7 +29,7 @@ beautifulsoup4==4.7.1 boto3==1.9.183 # via moto boto==2.49.0 botocore==1.12.183 # via boto3, moto, s3transfer -cachetools==3.1.1 # via google-auth, premailer +cachetools==3.1.1 # via premailer cchardet==2.1.4 certifi==2019.3.9 # via requests cffi==1.12.3 # via argon2-cffi, cryptography @@ -62,9 +62,6 @@ ecdsa==0.13.2 # via python-jose fakeldap==0.6.1 first==2.0.2 # via pip-tools gitlint==0.11.0 -google-api-python-client==1.7.4 -google-auth-httplib2==0.0.3 # via google-api-python-client -google-auth==1.6.3 # via google-api-python-client, google-auth-httplib2 h2==2.6.2 # via hyper hpack==3.0.0 # via h2 html2text==2018.1.9 @@ -95,7 +92,6 @@ mock==2.0.0 moto==1.3.7 mypy-extensions==0.4.1 mypy==0.670 -oauth2client==4.1.3 oauthlib==3.0.1 # via requests-oauthlib, social-auth-core packaging==19.0 # via sphinx parsel==1.5.1 # via scrapy @@ -115,8 +111,8 @@ ptyprocess==0.6.0 # via pexpect py3dns==3.2.0 pyahocorasick==1.4.0 pyaml==19.4.1 # via moto -pyasn1-modules==0.2.5 # via google-auth, oauth2client, python-ldap, service-identity -pyasn1==0.4.5 # via oauth2client, pyasn1-modules, python-ldap, rsa, service-identity +pyasn1-modules==0.2.5 # via python-ldap, service-identity +pyasn1==0.4.5 # via pyasn1-modules, python-ldap, service-identity pycodestyle==2.5.0 pycparser==2.19 # via cffi pycryptodome==3.8.2 # via python-jose @@ -150,7 +146,6 @@ regex==2019.6.8 requests-oauthlib==1.0.0 # via python-twitter, social-auth-core requests[security]==2.22.0 # via aws-xray-sdk, docker, hypchat, matrix-client, moto, premailer, pyoembed, python-digitalocean, python-gcm, python-twitter, requests-oauthlib, responses, social-auth-core, sphinx, stripe, twilio responses==0.10.6 # via moto -rsa==4.0 # via google-auth, oauth2client s3transfer==0.2.1 # via boto3 scrapy==1.6.0 service-identity==18.1.0 # via scrapy @@ -178,7 +173,6 @@ twilio==6.26.2 twisted==19.2.1 typed-ast==1.3.5 # via mypy typing==3.6.6 -uritemplate==3.0.0 # via google-api-python-client urllib3==1.25.3 # via botocore, requests, transifex-client virtualenv-clone==0.5.3 w3lib==1.20.0 # via parsel, scrapy diff --git a/requirements/prod.txt b/requirements/prod.txt index dd6d8c557e..51d8cfce2e 100644 --- a/requirements/prod.txt +++ b/requirements/prod.txt @@ -22,7 +22,7 @@ babel==2.7.0 # via django-phonenumber-field backcall==0.1.0 # via ipython beautifulsoup4==4.7.1 boto==2.49.0 -cachetools==3.1.1 # via google-auth, premailer +cachetools==3.1.1 # via premailer cchardet==2.1.4 certifi==2019.3.9 # via requests cffi==1.12.3 # via argon2-cffi, cryptography @@ -45,9 +45,6 @@ django-statsd-mozilla==0.4.0 django-two-factor-auth==1.8.0 django-webpack-loader==0.6.0 django==1.11.22 -google-api-python-client==1.7.4 -google-auth-httplib2==0.0.3 # via google-api-python-client -google-auth==1.6.3 # via google-api-python-client, google-auth-httplib2 h2==2.6.2 # via hyper hpack==3.0.0 # via h2 html2text==2018.1.9 @@ -69,7 +66,6 @@ markupsafe==1.1.1 # via jinja2 matrix-client==0.3.2 mock==2.0.0 mypy_extensions==0.4.1 -oauth2client==4.1.3 oauthlib==3.0.1 # via requests-oauthlib, social-auth-core parso==0.5.0 # via jedi pbr==5.3.1 # via mock @@ -85,8 +81,8 @@ psycopg2==2.8.2 ptyprocess==0.6.0 # via pexpect py3dns==3.2.0 pyahocorasick==1.4.0 -pyasn1-modules==0.2.5 # via google-auth, oauth2client, python-ldap -pyasn1==0.4.5 # via oauth2client, pyasn1-modules, python-ldap, rsa +pyasn1-modules==0.2.5 # via python-ldap +pyasn1==0.4.5 # via pyasn1-modules, python-ldap pycparser==2.19 # via cffi pygments==2.3.1 pyjwt==1.7.1 @@ -107,7 +103,6 @@ redis==2.10.6 regex==2019.6.8 requests-oauthlib==1.0.0 # via python-twitter, social-auth-core requests[security]==2.22.0 # via hypchat, matrix-client, premailer, pyoembed, python-gcm, python-twitter, requests-oauthlib, social-auth-core, stripe, twilio -rsa==4.0 # via google-auth, oauth2client simplegeneric==0.8.1 # via ipython six==1.12.0 social-auth-app-django==3.1.0 @@ -122,7 +117,6 @@ tornado==4.5.3 traitlets==4.3.2 # via ipython twilio==6.26.2 typing==3.6.6 -uritemplate==3.0.0 # via google-api-python-client urllib3==1.25.3 # via requests uwsgi==2.0.17.1 virtualenv-clone==0.5.3 diff --git a/static/js/portico/signup.js b/static/js/portico/signup.js index e39684878f..3146e66328 100644 --- a/static/js/portico/signup.js +++ b/static/js/portico/signup.js @@ -118,8 +118,7 @@ $(function () { https://stackoverflow.com/questions/5283395/url-hash-is-persisting-between-redirects */ var email_formaction = $("#login_form").attr('action'); $("#login_form").attr('action', email_formaction + '/' + window.location.hash); - $("#google_login_form input[name='next']").attr('value', '/' + window.location.hash); - $("#social_login_form input[name='next']").attr('value', '/' + window.location.hash); + $(".social_login_form input[name='next']").attr('value', '/' + window.location.hash); var sso_address = $("#sso-login").attr('href'); $("#sso-login").attr('href', sso_address + window.location.hash); diff --git a/static/styles/portico/portico-signin.scss b/static/styles/portico/portico-signin.scss index 3f2df12713..eb24fec7fe 100644 --- a/static/styles/portico/portico-signin.scss +++ b/static/styles/portico/portico-signin.scss @@ -621,8 +621,9 @@ button.login-social-button:active { box-shadow: 0px 1px 1px hsla(0, 0%, 0%, 0.3); } -button.login-google-button { +.google-wrapper button.login-social-button { background-image: url('/static/images/landing-page/logos/googl_e-icon.png'); + width: 100%; } .github-wrapper::before { diff --git a/templates/zerver/accounts_home.html b/templates/zerver/accounts_home.html index 81f4a449ca..c5ac3b4981 100644 --- a/templates/zerver/accounts_home.html +++ b/templates/zerver/accounts_home.html @@ -73,18 +73,6 @@ page can be easily identified in it's respective JavaScript file --> {% endif %} {% endif %} - {% if google_auth_enabled %} -
-
- - - -
-
- {% endif %} - {% for backend in social_backends %}
diff --git a/templates/zerver/config_error.html b/templates/zerver/config_error.html index db1460b507..89c0f38333 100644 --- a/templates/zerver/config_error.html +++ b/templates/zerver/config_error.html @@ -50,9 +50,9 @@ {% if google_error %} {% if development_environment %} - {{ render_markdown_path('zerver/google-error.md', {"root_domain_uri": root_domain_uri, "settings_path": secrets_path, "secrets_path": secrets_path, "client_id_key_name": "google_oauth2_client_id"}) }} + {{ render_markdown_path('zerver/google-error.md', {"root_domain_uri": root_domain_uri, "settings_path": secrets_path, "secrets_path": secrets_path, "client_id_key_name": "social_auth_google_key"}) }} {% else %} - {{ render_markdown_path('zerver/google-error.md', {"root_domain_uri": root_domain_uri, "settings_path": settings_path, "secrets_path": secrets_path, "client_id_key_name": "GOOGLE_OAUTH2_CLIENT_ID"}) }} + {{ render_markdown_path('zerver/google-error.md', {"root_domain_uri": root_domain_uri, "settings_path": settings_path, "secrets_path": secrets_path, "client_id_key_name": "SOCIAL_AUTH_GOOGLE_KEY"}) }} {% endif %} {% endif %} diff --git a/templates/zerver/google-error.md b/templates/zerver/google-error.md index ae96393dcc..458db036e8 100644 --- a/templates/zerver/google-error.md +++ b/templates/zerver/google-error.md @@ -1,13 +1,13 @@ You are using the **Google auth backend**, but it is not properly configured. Please check the following: -* You have created a Google Oauth2 client and enabled the Google+ API. +* You have created a Google Oauth2 client and enabled the Identity Toolkit API. You can create OAuth2 apps at [the Google developer console](https://console.developers.google.com). * You have configured your OAuth2 client to allow redirects to your server's Google auth URL: `{{ root_domain_uri }}/accounts/login/google/done/`. * You have set `{{ client_id_key_name }}` in `{{ settings_path }}` and -`google_oauth2_client_secret` in `{{ secrets_path }}`. +`social_auth_google_secret` in `{{ secrets_path }}`. * Navigate back to the login page and attempt the Google auth flow again. diff --git a/templates/zerver/login.html b/templates/zerver/login.html index 76bd5367d5..830b0e472a 100644 --- a/templates/zerver/login.html +++ b/templates/zerver/login.html @@ -140,20 +140,9 @@ {% endif %} - {% if google_auth_enabled %} -
- {% endif %} - {% for backend in social_backends %}