From 02441da426cdec9a362b427a3bf03e0d9b00155d Mon Sep 17 00:00:00 2001 From: U-Zyn Chua Date: Mon, 23 Jul 2012 13:46:23 +0800 Subject: [PATCH] Init --- .gitignore | 2 + GitHubStrategy.php | 152 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 47 ++++++++++++++ composer.json | 23 +++++++ 4 files changed, 224 insertions(+) create mode 100644 .gitignore create mode 100644 GitHubStrategy.php create mode 100644 README.md create mode 100644 composer.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ca0973 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store + diff --git a/GitHubStrategy.php b/GitHubStrategy.php new file mode 100644 index 0000000..8d3be89 --- /dev/null +++ b/GitHubStrategy.php @@ -0,0 +1,152 @@ + 'email'); + */ + public $defaults = array( + 'redirect_uri' => '{complete_url_to_strategy}oauth2callback', + 'scope' => 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email' + ); + + /** + * Auth request + */ + public function request(){ + $url = 'https://accounts.google.com/o/oauth2/auth'; + $params = array( + 'client_id' => $this->strategy['client_id'], + 'redirect_uri' => $this->strategy['redirect_uri'], + 'response_type' => 'code', + 'scope' => $this->strategy['scope'] + ); + + foreach ($this->optionals as $key){ + if (!empty($this->strategy[$key])) $params[$key] = $this->strategy[$key]; + } + + $this->clientGet($url, $params); + } + + /** + * Internal callback, after OAuth + */ + public function oauth2callback(){ + if (array_key_exists('code', $_GET) && !empty($_GET['code'])){ + $code = $_GET['code']; + $url = 'https://accounts.google.com/o/oauth2/token'; + $params = array( + 'code' => $code, + 'client_id' => $this->strategy['client_id'], + 'client_secret' => $this->strategy['client_secret'], + 'redirect_uri' => $this->strategy['redirect_uri'], + 'grant_type' => 'authorization_code' + ); + $response = $this->serverPost($url, $params, null, $headers); + + $results = json_decode($response); + + if (!empty($results) && !empty($results->access_token)){ + $userinfo = $this->userinfo($results->access_token); + + $this->auth = array( + 'provider' => 'Google', + 'uid' => $userinfo->id, + 'info' => array( + 'name' => $userinfo->name, + 'email' => $userinfo->email, + 'first_name' => $userinfo->given_name, + 'last_name' => $userinfo->family_name, + 'image' => $userinfo->picture + ), + 'credentials' => array( + 'token' => $results->access_token, + 'expires' => date('c', time() + $results->expires_in) + ), + 'raw' => $userinfo + ); + + if (!empty($userinfo->link)) $this->auth['info']['urls']['google'] = $userinfo->link; + + $this->callback(); + } + else{ + $error = array( + 'provider' => 'Google', + 'code' => 'access_token_error', + 'message' => 'Failed when attempting to obtain access token', + 'raw' => array( + 'response' => $response, + 'headers' => $headers + ) + ); + + $this->errorCallback($error); + } + } + else{ + $error = array( + 'provider' => 'Google', + 'code' => 'oauth2callback_error', + 'raw' => $_GET + ); + + $this->errorCallback($error); + } + } + + /** + * Queries Google API for user info + * + * @param string $access_token + * @return array Parsed JSON results + */ + private function userinfo($access_token){ + $userinfo = $this->serverGet('https://www.googleapis.com/oauth2/v1/userinfo', array('access_token' => $access_token), null, $headers); + if (!empty($userinfo)){ + return json_decode($userinfo); + } + else{ + $error = array( + 'provider' => 'Google', + 'code' => 'userinfo_error', + 'message' => 'Failed when attempting to query for user information', + 'raw' => array( + 'response' => $userinfo, + 'headers' => $headers + ) + ); + + $this->errorCallback($error); + } + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..7abc01d --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +Opauth-GitHub +============= +[Opauth][1] strategy for GitHub authentication. + +Implemented based on http://developer.github.com/v3/oauth/ using OAuth2. + +Opauth is a multi-provider authentication framework for PHP. + +Getting started +---------------- +1. Install Opauth-GitHub: + ```bash + cd path_to_opauth/Strategy + git clone git://github.com/uzyn/opauth-github.git GitHub + ``` + +2. Register a GitHub application at https://github.com/settings/applications/new + - Enter URL as your application URL (this can be outside of Opauth) + - Callback URL: enter `http://path_to_opauth/github/oauth2callback` + +3. Configure Opauth-GitHub strategy with `client_id` and `client_secret`. + +4. Direct user to `http://path_to_opauth/github` to authenticate + + +Strategy configuration +---------------------- + +Required parameters: + +```php + array( + 'client_id' => 'YOUR CLIENT ID', + 'client_secret' => 'YOUR CLIENT SECRET' +) +``` + +Optional parameters: +`scope`, `state` + +License +--------- +Opauth-GitHub is MIT Licensed +Copyright © 2012 U-Zyn Chua (http://uzyn.com) + +[1]: https://github.com/uzyn/opauth \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..47339fc --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "opauth/github", + "description": "GitHub authentication strategy for Opauth", + "keywords": ["authentication","auth","github"], + "homepage": "http://opauth.org", + "license": "MIT", + "authors": [ + { + "name": "U-Zyn Chua", + "email": "chua@uzyn.com", + "homepage": "http://uzyn.com" + } + ], + "require": { + "php": ">=5.2.0", + "opauth/opauth": ">=0.4.0" + }, + "autoload": { + "psr-0": { + "": "." + } + } +} \ No newline at end of file