|
| 1 | +<?php |
| 2 | +/* |
| 3 | +Copyright (C) 2011 by Jim Saunders |
| 4 | + |
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +The above copyright notice and this permission notice shall be included in |
| 13 | +all copies or substantial portions of the Software. |
| 14 | + |
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +THE SOFTWARE. |
| 22 | +*/ |
| 23 | + |
| 24 | +require_once 'oauth_helper.php'; |
| 25 | + |
| 26 | +/** |
| 27 | + * This library is meant to get you an oauth access token |
| 28 | + * for a google service. You can define which service by |
| 29 | + * changing the SCOPE constant. You will need to pass in |
| 30 | + * your consumer key and secret upon loading this library. |
| 31 | + * |
| 32 | + * I currently use it in my YouTube API but it can be used |
| 33 | + * for any google service that allows for OAuth. |
| 34 | + */ |
| 35 | +class google_oauth |
| 36 | +{ |
| 37 | + const SCHEME = 'https'; |
| 38 | + const HOST = 'www.google.com'; |
| 39 | + const AUTHORIZE_URI = '/accounts/OAuthAuthorizeToken'; |
| 40 | + const REQUEST_URI = '/accounts/OAuthGetRequestToken'; |
| 41 | + const ACCESS_URI = '/accounts/OAuthGetAccessToken'; |
| 42 | + |
| 43 | + //This should be changed to correspond with the |
| 44 | + //google service you are authenticating against. |
| 45 | + const SCOPE = 'https://gdata.youtube.com'; //YouTube |
| 46 | + |
| 47 | + //Set this flag to true for detailed logging. |
| 48 | + const DEBUG = false; |
| 49 | + |
| 50 | + //Array that should contain the consumer secret and |
| 51 | + //key which should be passed into the constructor. |
| 52 | + private $_consumer = false; |
| 53 | + |
| 54 | + /** |
| 55 | + * Pass in the key and secret given to you by google |
| 56 | + * Note that the secret should either be a hash string for |
| 57 | + * HMAC signatures or a file path string for RSA signatures. |
| 58 | + * |
| 59 | + * @param string $key The oauth key given to you by google |
| 60 | + * @param string $secret The hash given to you by google or a path to your RSA sig file |
| 61 | + * @param (Optional) array $params Lets you change things like the signing algorithm |
| 62 | + */ |
| 63 | + public function __construct($key, $secret, array $params = array()) |
| 64 | + { |
| 65 | + $params['key'] = $key; |
| 66 | + $params['secret'] = $secret; |
| 67 | + //Set defaults for method and algorithm if they are not specified |
| 68 | + if(!array_key_exists('method', $params))$params['method'] = 'GET'; |
| 69 | + if(!array_key_exists('algorithm', $params))$params['algorithm'] = OAUTH_ALGORITHMS::HMAC_SHA1; |
| 70 | + |
| 71 | + $this->_consumer = $params; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * This is called to begin the oauth token exchange. This should only |
| 76 | + * need to be called once for a user, provided they allow oauth access. |
| 77 | + * It will return a URL that your site should redirect to, allowing the |
| 78 | + * user to login and accept your application. |
| 79 | + * |
| 80 | + * @param string $callback the page on your site you wish to return to |
| 81 | + * after the user grants your application access. |
| 82 | + * @return mixed either the URL to redirect to, or if they specified HMAC |
| 83 | + * signing an array with the token_secret and the redirect url |
| 84 | + */ |
| 85 | + public function get_request_token($callback) |
| 86 | + { |
| 87 | + $baseurl = self::SCHEME.'://'.self::HOST.self::REQUEST_URI; |
| 88 | + |
| 89 | + //Generate an array with the initial oauth values we need |
| 90 | + $auth = build_auth_array($baseurl, $this->_consumer['key'], $this->_consumer['secret'], |
| 91 | + array('oauth_callback'=>urlencode($callback), 'scope'=>urlencode(self::SCOPE)), |
| 92 | + $this->_consumer['method'], $this->_consumer['algorithm']); |
| 93 | + //Create the "Authorization" portion of the header |
| 94 | + $str = ''; |
| 95 | + foreach($auth AS $key=>$value) |
| 96 | + if($key != 'scope')$str .= ",{$key}=\"{$value}\"";//Do not include scope in the Authorization string. |
| 97 | + $str = substr($str, 1); |
| 98 | + $str = 'Authorization: OAuth '.$str; |
| 99 | + //Send it |
| 100 | + $response = $this->_connect("{$baseurl}?scope={$auth['scope']}", $str); |
| 101 | + //We should get back a request token and secret which |
| 102 | + //we will add to the redirect url. |
| 103 | + parse_str($response, $resarray); |
| 104 | + //Return the full redirect url and let the user decide what to do from there. |
| 105 | + $redirect = self::SCHEME.'://'.self::HOST.self::AUTHORIZE_URI."?oauth_token={$resarray['oauth_token']}"; |
| 106 | + //If they are using HMAC then we need to return the token secret for them to store. |
| 107 | + if($this->_consumer['algorithm'] == OAUTH_ALGORITHMS::RSA_SHA1)return $redirect; |
| 108 | + else return array('token_secret'=>$resarray['oauth_token_secret'], 'redirect'=>$redirect); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * This is called to finish the oauth token exchange. This too should |
| 113 | + * only need to be called once for a user. The token returned should |
| 114 | + * be stored in your database for that particular user. |
| 115 | + * |
| 116 | + * @param string $token this is the oauth_token returned with your callback url |
| 117 | + * @param string $secret this is the token secret supplied from the request (Only required if using HMAC) |
| 118 | + * @param string $verifier this is the oauth_verifier returned with your callback url |
| 119 | + * @return array access token and token secret |
| 120 | + */ |
| 121 | + public function get_access_token($token = false, $secret = false, $verifier = false) |
| 122 | + { |
| 123 | + //If no request token was specified then attempt to get one from the url |
| 124 | + if($token === false && isset($_GET['oauth_token']))$token = $_GET['oauth_token']; |
| 125 | + if($verifier === false && isset($_GET['oauth_verifier']))$verifier = $_GET['oauth_verifier']; |
| 126 | + //If all else fails attempt to get it from the request uri. |
| 127 | + if($token === false && $verifier === false) |
| 128 | + { |
| 129 | + $uri = $_SERVER['REQUEST_URI']; |
| 130 | + $uriparts = explode('?', $uri); |
| 131 | + |
| 132 | + $authfields = array(); |
| 133 | + parse_str($uriparts[1], $authfields); |
| 134 | + $token = $authfields['oauth_token']; |
| 135 | + $verifier = $authfields['oauth_verifier']; |
| 136 | + } |
| 137 | + |
| 138 | + $tokenddata = array('oauth_token'=>urlencode($token), 'oauth_verifier'=>urlencode($verifier)); |
| 139 | + if($secret !== false)$tokenddata['oauth_token_secret'] = urlencode($secret); |
| 140 | + |
| 141 | + $baseurl = self::SCHEME.'://'.self::HOST.self::ACCESS_URI; |
| 142 | + //Include the token and verifier into the header request. |
| 143 | + $auth = get_auth_header($baseurl, $this->_consumer['key'], $this->_consumer['secret'], |
| 144 | + $tokenddata, $this->_consumer['method'], $this->_consumer['algorithm']); |
| 145 | + $response = $this->_connect($baseurl, $auth); |
| 146 | + //Parse the response into an array it should contain |
| 147 | + //both the access token and the secret key. (You only |
| 148 | + //need the secret key if you use HMAC-SHA1 signatures.) |
| 149 | + parse_str($response, $oauth); |
| 150 | + //Return the token and secret for storage |
| 151 | + return $oauth; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Connects to the server and sends the request, |
| 156 | + * then returns the response from the server. |
| 157 | + * @param <type> $url |
| 158 | + * @param <type> $auth |
| 159 | + * @return <type> |
| 160 | + */ |
| 161 | + private function _connect($url, $auth) |
| 162 | + { |
| 163 | + $ch = curl_init($url); |
| 164 | + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ; |
| 165 | + curl_setopt($ch, CURLOPT_SSLVERSION,3); |
| 166 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
| 167 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
| 168 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 169 | + curl_setopt($ch, CURLOPT_HTTPHEADER, explode("\r\n", $auth)); |
| 170 | + curl_setopt($ch, CURLINFO_HEADER_OUT, true); |
| 171 | + |
| 172 | + $response = curl_exec($ch); |
| 173 | + |
| 174 | + if(self::DEBUG) |
| 175 | + { |
| 176 | + error_log(print_r(curl_getinfo($ch), true)); |
| 177 | + error_log($response); |
| 178 | + } |
| 179 | + curl_close($ch); |
| 180 | + return $response; |
| 181 | + } |
| 182 | +} |
| 183 | +// ./system/application/libraries |
| 184 | +?> |
0 commit comments