Best laravel framework open-source packages.

GoogleOauth2Component

Google Auth Component for CakePHP Project - Requires the Google_Client and Google_Oauth2Service client libraries
Updated 9 years ago

DISCLAIMER

This work is put on here to assist anyone working with CakePHP projects and needing a wrapper class for the Google OAuth 2.0 library. It is provided AS-IS.

PLUGIN VS. COMPONENT

There are cases when it is best to use a plugin in CakePHP, and other cases where it works out to use a component. Depending on your preferences, you could swing in either direction - this component provides functionality that some might see better fit for a plugin - and there might be a plugin version in the future. For now - quick and dirty!

REQUIREMENTS

This component requires the following libraries:

CakePHP - Your application should be based on the CakePHP Framework. You can download the framework code at http://cakephp.org/.

A working knowledge of coding with CakePHP. The CakePHP documentation can be found here: http://book.cakephp.org/2.0/en/index.html

A Google Console Project with OAuth credentials. To setup your google console project, visit http://cloud.google.com.

The Google_Oauth2Service class, available in the Google API Client Library for PHP library

The Google_Client class, also available in the Google API Client Library for PHP library

INSTALLATION

Copy the component to your /<Application Root>/app/Controller/Component/ directory, or any other directory bootstrapped to search for components.

Replace the paths to both the Google_Oauth2Service and Google_Client class files with the correct path on your server - e.g. /var/www/libraries/google/google-api-client/src/contrib/<ClassFile> assuming that is where you have the Google API library installed on your server

That's it! You're ready to start using the component within your CakePHP controllers.

USING THE COMPONENT

Include the component name in your $components array

    public $components = array('GoogleOauth2');

Next, use the connect() method to initialize the component when you are ready to attempt authentication against Google ID. See the method reference below for accepted arguments.

LOADING COMPONENTS ON THE FLY

You can load the component on the fly in your CakePHP application using the following syntax:

$this->GoogleAuth = $this->Components->load('GoogleOauth2');

FUNCTIONS

connect($controller, $config, $scopes, $api_mode)

This method is most likey be well used within your project. It initializes your authentication attempt againt the user's Google ID, and accepts the following arguments:

controller: Use the $this variable to pass your controller into the component for controller-level callbacks

config: This is an associative array with the following parameters:

client_id: Your Google API client ID

client_secret: Your Google API secret

redirect_url: Your success redirect url once the authentication action is completed. This url MUST be included in the list of accepted callbacks in your google console

scopes: This is an array of Google API scopes. These scopes are in url format. A typical array of scopes will look like this:

$scopes = array('https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file');

The email and profile scopes are required for this component.

api_mode: This is a boolean variable. If set to true, the component will return an associative array with a success index that indicates whether the auth attempt was successful or not. This will be useful if you are authenticating via an API and would like to parse the associate array to a JSON string (for instance) instead of the auto-redirect action.

isReady()

This returns true or false after a connect() call for a success or failure to authenticate.

getAuthUrl()

Returns the authentication Url against Google's OAuth API. Is useful if you are handling redirection manually. In this case, the api_mode variable of your connect() function can come in handy to turn off the auto-redirect when authentication is needed.

getUser()

Will return an associate array of the session user data.

getUserId()

Will return the Google User ID from the session user array.

cleanUser()

Useful for logging out. Call in your logout() controller function to delete the google user data from the session.

getTokens()

Returns a JSON string of the tokens returned on successful authentication and stored in the session array.

QUESTIONS?

Reach out @ https://twitter.com/websiteinapage. I'll do my best to respond in a timely fasion.