Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added support for oAuth

  • Loading branch information...
commit 3da29a3d9c76c69385a819b70b4d3c12ec1a5ae5 1 parent eeef4a4
Nick Palamarchuk lividgreen authored
Showing with 54 additions and 2 deletions.
  1. +50 −0 lib/phpGitHubApi.php
  2. +4 −2 lib/request/phpGitHubApiRequest.php
50 lib/phpGitHubApi.php
View
@@ -32,6 +32,12 @@ class phpGitHubApi
const AUTH_HTTP_TOKEN = 'http_token';
/**
+ * Constant for authentication method. Indicates the new login method using oAuth
+ * @see https://gist.github.com/419219
+ */
+ const AUTH_OAUTH = 'oauth';
+
+ /**
* The request instance used to communicate with GitHub
* @var phpGitHubApiRequest
*/
@@ -82,6 +88,50 @@ public function authenticate($login, $secret, $method = NULL)
}
/**
+ * Authenticate a user by GitHub oAuth
+ *
+ * @throws phpGitHubApiRequestException
+ * @param string $client_id GitHub application client id
+ * @param string $redirect_url GitHub application redirect url
+ * @param string $client_secret GitHub application client secret
+ * @param string $code Code, returned from GitHub after user's authorization
+ * @return phpGitHubApi fluent interface
+ */
+ public function authenticateOAuth($client_id, $redirect_url, $client_secret, $code)
+ {
+ $ch = curl_init('https://github.com/login/oauth/access_token');
+ curl_setopt_array($ch, array(
+ CURLOPT_RETURNTRANSFER => 1,
+ CURLOPT_FOLLOWLOCATION => 1,
+ CURLOPT_HEADER => 0,
+ CURLOPT_POST => 1,
+ CURLOPT_POSTFIELDS => array(
+ 'client_id' => $client_id,
+ 'redirect_url' => $redirect_url,
+ 'client_secret' => $client_secret,
+ 'code' => $code
+ )
+ ));
+ $response = curl_exec($ch);
+ curl_close($ch);
+
+ if (preg_match('/^access_token=([0-9a-f]+)$/u', $response, $matches)) {
+ $token = $matches[1];
+ return $this->authenticate(null, $token, self::AUTH_OAUTH);
+ }
+
+ throw new phpGitHubApiRequestException("Not authenticated using oAuth");
+ }
+
+ public function getUserAuthorizationUrl($client_id, $redirect_url, $scope = null) {
+ $url = 'https://github.com/login/oauth/authorize?client_id='.$client_id.'&redirect_uri='.$redirect_url;
+ if (isset($scope)) {
+ $url .= '&scope='.$scope;
+ }
+ return $url;
+ }
+
+ /**
* Deauthenticate a user for all next requests
*
* @return phpGitHubApi fluent interface
6 lib/request/phpGitHubApiRequest.php
View
@@ -139,9 +139,12 @@ public function doSend($apiPath, array $parameters = array(), $httpMethod = 'GET
$curlOptions = array();
- if($this->options['login'])
+ if($this->options['login'] || $this->options['auth_method'] == phpGitHubApi::AUTH_OAUTH)
{
switch($this->options['auth_method']) {
+ case phpGitHubApi::AUTH_OAUTH:
+ $parameters['access_token'] = $this->options['secret'];
+ break;
case phpGitHubApi::AUTH_HTTP_PASSWORD:
$curlOptions += array(
CURLOPT_USERPWD => $this->options['login'] . ':' . $this->options['secret'],
@@ -162,7 +165,6 @@ public function doSend($apiPath, array $parameters = array(), $httpMethod = 'GET
}
}
-
if (!empty($parameters))
{
$queryString = utf8_encode(http_build_query($parameters, '', '&'));
Please sign in to comment.
Something went wrong with that request. Please try again.