I have Django Social Auth (pypi package social-auth-app-django) working with Google OAuth2 on a site.
When they open a page they get redirected to Google's OAuth2 authentication. This works great, and if they try to log in with an email address that is not in my restricted list they get an AuthForbidden exception. I've added a catch for this and show them a page that they are not allowed to log into this site. All well and good so far.
However, I would prefer to not have the invalid email accounts show up at all in the list from Google's authentication page in the first place. I've done this with manual calls to the authentication page before using javascript with a parameter, but I'm not sure how to do so using the canned Django social auth module.
Can this be done and if so, how?
I have middleware that detects if a user is not logged in and returns a login() view which redirects them to the Google authentication page.
views.py
# Login using OAuth2.
@csrf_protect
def login(request):
next_page = request.path
if next_page is None or next_page == '':
next_page = request.POST.get('next', request.GET.get('next', ''))
# Check if they are already logged in and redirect them to the original page if so.
if hasattr(request, 'user') and request.user.is_authenticated:
return HttpResponseRedirect(next_page)
# Otherwise, send them to the OAuth2 page with the request url as the next parameter.
else:
return HttpResponseRedirect('/soc/login/google-oauth2?next=' + next_page + '&hd=mydomain.com')
EDIT - added picture to clarify my purpose...
What I'm asking for is when the Google OAuth2 dialog pops up asking to select the email address to use to log in, or to add a new one, that only the emails from restricted domains show up as options. This image shows the Google login, and two accounts that have been authenticated previously. One is a personal gmail account that I would prefer not to be displayed here. The other is from the domain that is in the whitelist and the only one I want to be displayed here: