Search code examples
python-3.xherokuflaskgoogle-oauth

How do you restrict Google Login (Oauth2) to emails from a specific Google Apps domain for a Flask WebApp?


Developing a Flask app (Python3/Heroku) for internal company use and successfully implemented Google Login (Oauth2) based on brijieshb42's article which uses requests_oauthlib.

Research has indicated that if I pass parameter "hd" (hosted domain) in my authorization url it should do the trick. E.g.

https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=OUR_CLIENT_ID&redirect_uri=https%3A%2F%2FOUR_APP.herokuapp.com%2Fconnect&scope=profile+email&state=STATE&hd=our_google_apps_domain.com&access_type=offline

My understanding based is that this parameter should provide client-side restriction and only allow logins from emails from our google apps domain (server-side I'll handle after this!) based on Google Documentation, this mailing list post and these stackoverflow posts: post1, post2.

However, though my code generates the authorization URL I pasted above -- I can still login with my personal gmail account (@gmail.com vs @our apps domain.com).

Can anyone shed some light as to why this isn't working? Or provide a different approach? Basically would prefer preventing non-employees from logging in.

I can share code as needed, but pretty much pasted from the brijeshb42 article and essentially looks like this:

OAuth2Session(
  OUR_CLIENT_ID,
  redirect_uri=https://OUR_APP.herokuapp.com/connect,
  scope=['profile', 'email']).authorization_url(
      https://accounts.google.com/o/oauth2/auth,
      hd='our_google_apps_domain.com',
      access_type='offline')

Which returns the auth url I pasted above!


Solution

  • After successful authentication, you have to check the provided email yourself. I have added the code snippet from the my article that you have referenced. I have added the extra check required in after comment.

    @app.route('/gCallback')
    def callback():
        # Redirect user to home page if already logged in.
        if current_user is not None and current_user.is_authenticated():
            return redirect(url_for('index'))
        if 'error' in request.args:
            if request.args.get('error') == 'access_denied':
                return 'You denied access.'
            return 'Error encountered.'
        if 'code' not in request.args and 'state' not in request.args:
            return redirect(url_for('login'))
        else:
            # Execution reaches here when user has
            # successfully authenticated our app.
            google = get_google_auth(state=session['oauth_state'])
            try:
                token = google.fetch_token(
                    Auth.TOKEN_URI,
                    client_secret=Auth.CLIENT_SECRET,
                    authorization_response=request.url)
            except HTTPError:
                return 'HTTPError occurred.'
            google = get_google_auth(token=token)
            resp = google.get(Auth.USER_INFO)
            if resp.status_code == 200:
                user_data = resp.json()
                email = user_data['email']
                """
                Your Domain specific check will come here.
                """
                if email.split('@')[1] != 'domain.com':
                    flash('You cannot login using this email', 'error')
                    return redirect(url_for('login'))
                user = User.query.filter_by(email=email).first()
                if user is None:
                    user = User()
                    user.email = email
                user.name = user_data['name']
                print(token)
                user.tokens = json.dumps(token)
                user.avatar = user_data['picture']
                db.session.add(user)
                db.session.commit()
                login_user(user)
                return redirect(url_for('index'))
            return 'Could not fetch your information.'