Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict Login Email with Google OAuth2.0 to Specific Domain Name

I can't seem to find any documentation on how to restrict the login to my web application (which uses OAuth2.0 and Google APIs) to only accept authentication requests from users with an email on a specific domain name or set of domain names. I would like to whitelist as opposed to blacklist.

Does anyone have suggestions on how to do this, documentation on the officially accepted method of doing so, or an easy, secure work around?

For the record, I do not know any info about the user until they attempt to log in through Google's OAuth authentication. All I receive back is the basic user info and email.

like image 558
paradox870 Avatar asked Jun 02 '12 00:06

paradox870


People also ask

What is redirect URI in oauth2 Google?

The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses. These endpoints must adhere to Google's validation rules. For testing, you can specify URIs that refer to the local machine, such as http://localhost:8080 .


1 Answers

So I've got an answer for you. In the oauth request you can add "hd=domain.com" and it will restrict authentication to users from that domain (I don't know if you can do multiple domains). You can find hd parameter documented here

I'm using the google api libraries from here: http://code.google.com/p/google-api-php-client/wiki/OAuth2 so I had to manually edit the /auth/apiOAuth2.php file to this:

public function createAuthUrl($scope) {     $params = array(         'response_type=code',         'redirect_uri=' . urlencode($this->redirectUri),         'client_id=' . urlencode($this->clientId),         'scope=' . urlencode($scope),         'access_type=' . urlencode($this->accessType),         'approval_prompt=' . urlencode($this->approvalPrompt),         'hd=domain.com'     );      if (isset($this->state)) {         $params[] = 'state=' . urlencode($this->state);     }     $params = implode('&', $params);     return self::OAUTH2_AUTH_URL . "?$params"; } 

Edit: I'm still working on this app and found this, which may be the more correct answer to this question. https://developers.google.com/google-apps/profiles/

like image 106
Aaron Bruce Avatar answered Sep 22 '22 06:09

Aaron Bruce