public function registerGithubUser() { if (!Session::has('github_access_token')) { return Response::make('Unauthorized GitHub access.', 401); } $token = Session::get('github_access_token'); $ch = curl_init('https://api.github.com/user'); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: token {$token}")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_USERAGENT, 'SWAMP'); $response = curl_exec($ch); $github_user = json_decode($response, true); // Append email information // $ch = curl_init('https://api.github.com/user/emails'); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: token {$token}")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_USERAGENT, 'SWAMP'); $response = curl_exec($ch); $github_emails = json_decode($response); $github_user['email'] = ''; $primary_verified = false; foreach ($github_emails as $email) { if ($email->primary == '1' && $email->verified == '1') { $primary_verified = true; $github_user['email'] = $email->email; } else { if ($email->primary == '1') { $github_user['email'] = $email->email; } } } $names = array_key_exists('name', $github_user) ? explode(' ', $github_user['name']) : array('', ''); $user = new User(array('first_name' => array_key_exists(0, $names) ? $names[0] : '', 'last_name' => array_key_exists(1, $names) ? $names[1] : '', 'preferred_name' => array_key_exists('name', $github_user) ? $github_user['name'] : '', 'username' => $github_user['login'], 'password' => md5(uniqid()) . strtoupper(md5(uniqid())), 'user_uid' => GUID::create(), 'email' => $github_user['email'], 'address' => array_key_exists('location', $github_user) ? $github_user['location'] : '')); // Attempt username permutations // for ($i = 1; $i < 21; $i++) { $errors = array(); if ($user->isValid($errors, true)) { break; } if ($i == 20) { return Response::make('Unable to generate SWAMP GitHub user:<br/><br/>' . implode('<br/>', $errors), 401); } $user->username = $github_user['login'] . $i; } $user->add(); $linkedAccount = new LinkedAccount(array('user_uid' => $user->user_uid, 'user_external_id' => $github_user['id'], 'linked_account_provider_code' => 'github', 'enabled_flag' => 1)); $linkedAccount->save(); if ($primary_verified) { // Mark user account email verified flag $userAccount = $user->getUserAccount(); $userAccount->email_verified_flag = 1; $userAccount->save(); Mail::send('emails.welcome', array('user' => $user, 'logo' => Config::get('app.cors_url') . '/images/logos/swamp-logo-small.png', 'manual' => Config::get('app.cors_url') . '/documentation/SWAMP-UserManual.pdf'), function ($message) use($user) { $message->to($user->email, $user->getFullName()); $message->subject('Welcome to the Software Assurance Marketplace'); }); return Response::json(array('primary_verified' => true, 'user' => $user)); } else { $emailVerification = new EmailVerification(array('user_uid' => $user->user_uid, 'verification_key' => GUID::create(), 'email' => $user->email)); $emailVerification->save(); $emailVerification->send('#register/verify-email'); return Response::json(array('primary_verified' => false, 'user' => $user)); } }