Warning
This class was made by a drunk idiot. It was never finished. Use it at your own risk. Don't email the idiot about it.
This page is still here just so people's links don't break.
Google Login PHP Class
This class acts as a gateway to the Google OpenID Federated Login API. It is free for public use under the GNU General Public License.
On This Page
- Google Federated Login Overview
- Using the GoogleOpenID class
- About Association Handles
- Retrieving the User's Email Address
- Notes
- Suggestions and Modifications
- Download the Class
Google Federated Login Overview
Google Federated Login allows users to leave your web site, sign in to their Google accounts, and return to your web site with a unique identifier (sort of like a username). The Google API also allows you to recieve the user's email address upon their return.
Here is a simplified version of how the process works:
- The user clicks "Login with Google" on your web site
- Your web site makes an API call to Google and fetches a redirect URL
- Your web site redirects the user to the given URL
- Google authenticates the user
- Google redirects the user back to your web site and passes the user's identifier (and, optionally, their email address)
This is advantageous from a developer's point of view, because it allows you to offer user accounts without keeping a password database. You need only store information based on a person's unique identifier.
Using the GoogleOpenID Class
Here is a sample of how one might implement Google login using my GoogleOpenID class:
In index.php:
<a href="login.php">Login with your Google Account</a>
In login.php:
<?php
$googleLogin = GoogleOpenID::createRequest("return.php");
$googleLogin->redirect();
?>
In return.php:
<?php
$googleLogin = GoogleOpenID::getResponse();
if($googleLogin->success()){
$user_id = $googleLogin->identity();
}
?>
Association Handles
With every API request, Google requires the caller to pass an association handle. These are obtained with a simple call to the Google discovery URL, which the GoogleOpenID class handles automatically.
However, it is strongly recommended that you retrieve your own association handle and cache it, since they are valid for two weeks.
<?php
//fetch an association handle
$association_handle = GoogleOpenID::getAssociationHandle();
//somehow, save the association handle (the below function is not real)
save_handle_somehow($association_handle);
...
//somehow, retrieve the saved association handle (the below function is not real)
$association_handle = get_saved_handle_somehow();
//use the saved association handle
$googleLogin = GoogleOpenID::createRequest("return.php", $association_handle);
?>
This way, the constructor doesn't need to fetch a new association handle every time it is called.
Retrieving the User's Email Address
If you wish to retrieve the user's email address, you must set the third parameter of createRequest() to true. Here are modified versions of login.php and return.php from the above example.
In login.php:
<?php
$googleLogin = GoogleOpenID::createRequest("return.php", $association_handle, true);
$googleLogin->redirect();
?>
In return.php:
<?php
$googleLogin = GoogleOpenID::getResponse();
if($googleLogin->success()){
$user_id = $googleLogin->identity();
$user_email = $googleLogin->email();
}
?>
Notes
- It is strongly recommended that you pass an absolute URL to createRequest(). If you pass a relative URL, the constructor will attempt to convert it to an absolute one. At this point the conversion is messy and buggy, so I recommend passing an absolute URL to begin with.
- Google uses a standard OpenID protocol for it's federated login API, and there are plenty of libraries out there that deal with the OpenID protocol. I didn't make a generic OpenID class because that was not my purpose--I wanted a simple class that would deal with the Google federated login API only.
Suggestions and Modifications
I will publish any changes or modified versions that are sent to me as long as they fit the purpose of this class as I see it, and as long as the changes are also released under the GPL.
Download the Class
Download the full class here.
