public static function apiPost($target, $payload, $token = null) { if ($token === null) { $token = \Session::get('token'); } return json_decode(\App\Models\Curl::postJson("https://api.github.com/{$target}?access_token={$token}", $payload)); }
<?php Route::get('/', function () { $state = \Request::input('state'); $code = \Request::input('code'); // If user has been redirected by githubs oauth api... if ($code && $state) { if ($state !== \Session::get('state')) { \App::abort(500); } $cid = env('APIGENCI_ID'); $secret = env('APIGENCI_SECRET'); $url = urlencode('https://' . $_SERVER['HTTP_HOST']); // Request access token $curl = \App\Models\Curl::post("https://github.com/login/oauth/access_token?client_id={$cid}&client_secret={$secret}&code={$code}&state={$state}&redirect_uri={$url}"); parse_str($curl, $arr); // Invalid nonce, hacking attempt? if (!isset($arr['access_token'])) { //abort(403); } $token = $arr['access_token']; \Session::put('token', $token); $user = Apigenci\Model::githubApiGet('user'); \Session::put('user', $user); $arr = ['id' => $user->id, 'email' => $user->email, 'login' => $user->login]; // Check if user exists and add if not... try { \DB::connection('apigenci')->table('users')->insert($arr); } catch (\Exception $ex) { \DB::connection('apigenci')->table('users')->where('id', $user->id)->update($arr); }
public static function githubApiGet($target) { $token = \Session::get('token'); return json_decode(\App\Models\Curl::get("https://api.github.com/{$target}?access_token={$token}")); }