Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class GithubAuth
- {
- public $OAUTH2_CLIENT_ID = '0165a1d9f929e1c6aa1dd';
- public $OAUTH2_CLIENT_SECRET = '0844611f829sjj9caeaa5122179ba905cb7f1c76e42';
- public $authorizeURL = 'https://github.com/login/oauth/authorize';
- public $tokenURL = 'https://github.com/login/oauth/access_token';
- public $apiURLBase = 'https://api.github.com/';
- public $redirect_uri = 'http://ctools.mooo.com/GithubAuth.class.php';
- function __construct()
- {
- session_start();
- }
- public function apiRequest($url, $post = FALSE, $headers = array())
- {
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- if ($post)
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
- $headers[] = 'Accept: application/json';
- $headers[] = 'User-Agent: browserling';
- if ($this->session('access_token'))
- $headers[] = 'Authorization: Bearer ' . $this->session('access_token');
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- $response = curl_exec($ch);
- return json_decode($response);
- }
- public function apiscauth($url)
- {
- $ch = curl_init();
- // Basic Authentication with token
- // https://developer.github.com/v3/auth/
- // https://github.com/blog/1509-personal-api-tokens
- // https://github.com/settings/tokens
- $access = '0165jdjdjja1dd:084483883hsjsjs;
- curl_setopt($ch, CURLOPT_URL, $url);
- //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
- curl_setopt($ch, CURLOPT_USERAGENT, 'Agent smith');
- curl_setopt($ch, CURLOPT_HEADER, 1);
- curl_setopt($ch, CURLOPT_USERPWD, $access);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- $output = curl_exec($ch);
- curl_close($ch);
- $result = json_decode(trim($output), true);
- return $output;
- }
- public function code()
- {
- if (!$this->get('state') || $_SESSION['state'] != $this->get('state')) {
- header('Location: ' . $_SERVER['PHP_SELF']);
- die();
- }
- $token = $this->apiRequest($this->tokenURL, array(
- 'client_id' => $this->OAUTH2_CLIENT_ID,
- 'client_secret' => $this->OAUTH2_CLIENT_SECRET,
- 'redirect_uri' => $this->redirect_uri,
- 'state' => $_SESSION['state'],
- 'code' => $this->get('code')
- ));
- $_SESSION['access_token'] = $token->access_token;
- setcookie("user", $token->access_token, time()+3652425);
- header('Location: ' . $this->redirect_uri);
- }
- public function get($key, $default = NULL)
- {
- return array_key_exists($key, $_GET) ? $_GET[$key] : $default;
- }
- public function session($key, $default = NULL)
- {
- return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default;
- }
- public function login()
- {
- $_SESSION['state'] = hash('sha256', microtime(TRUE) . rand() . $_SERVER['REMOTE_ADDR']);
- unset($_SESSION['access_token']);
- $params = array(
- 'client_id' => $this->OAUTH2_CLIENT_ID,
- 'redirect_uri' => $this->redirect_uri,
- 'scope' => 'user',
- 'state' => $_SESSION['state'],
- 'token' => $_SESSION['access_token']
- );
- header('Location: ' . $this->authorizeURL . '?' . http_build_query($params));
- die();
- }
- }
- $auth = new GithubAuth();
- if($auth->get('action') == 'login') {
- $auth->login();
- }
- if($auth->get('action') == 'logout') {
- session_destroy();
- unset($_SESSION['access_token']);
- }
- if($auth->get('code')) {
- $auth->code();
- print_r($auth->get('code'));
- }
- if($auth->session('access_token')) {
- $user = $auth->apiRequest($auth->apiURLBase . 'user/emails');
- print_r($auth->session('access_token'));
- print_r($user);
- /* get authorization apps */
- echo '<center>
- <form method="post">
- <b>getauth list</b>
- <br/>Client id <input class="inp-text" type="text" name="clid"
- value="0165a1d9f16e1c6aa1dd">
- <br/>Tokens <input class="inp-text" type="text" name="tken"
- value="this php files examples">
- <br/>
- <input name="getauth" class="inp-btn" type="submit" value="submit">
- </form></center>';
- if(isset($_POST['getauth'])) {
- $clid=$_POST['clid'];
- $tken=$_POST['tken'];
- $userauth = $auth->apiscauth($auth->apiURLBase . "applications/" . $clid . "/tokens/" . $tken);
- print_r($userauth);
- }
- }else{
- if(isset($_COOKIE["user"])) {
- $_SESSION['access_token'] = $_COOKIE["user"];
- header('Location: ' . $auth->redirect_uri);
- }
- }
- print_r($_COOKIE["user"]);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement