. */ namespace Poniverse\Ponyfm\Http\Controllers; use Carbon\Carbon; use Illuminate\Support\Facades\Input; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use Log; use Poniverse\Lib\Client; use Poniverse\Ponyfm\Models\User; use Auth; use Config; use DB; use Request; use Redirect; class AuthController extends Controller { protected $poniverse; public function __construct() { $this->poniverse = new Client(config('poniverse.client_id'), config('poniverse.secret'), new \GuzzleHttp\Client()); } public function getLogin() { if (Auth::guest()) { return Redirect::to( $this->poniverse ->getOAuthProvider(['redirectUri' => action('AuthController@getOAuth')]) ->getAuthorizationUrl()); } return Redirect::to('/'); } public function postLogout() { Auth::logout(); return Redirect::to('/'); } public function getOAuth() { $oauthProvider = $this->poniverse->getOAuthProvider(); try { $accessToken = $oauthProvider->getAccessToken('authorization_code', [ 'code' => Request::query('code'), 'redirect_uri' => action('AuthController@getOAuth') ]); $this->poniverse->setAccessToken($accessToken); $resourceOwner = $oauthProvider->getResourceOwner($accessToken); } catch (IdentityProviderException $e) { Log::error($e); return Redirect::to('/')->with( 'message', 'Unfortunately we are having problems attempting to log you in at the moment. Please try again at a later time.' ); } /** @var \Poniverse\Lib\Entity\Poniverse\User $poniverseUser */ $poniverseUser = $resourceOwner; $token = DB::table('oauth2_tokens') ->where('external_user_id', '=', $poniverseUser->id) ->where('service', '=', 'poniverse') ->first(); $setData = [ 'access_token' => $accessToken, 'expires' => Carbon::createFromTimestampUTC($accessToken->getExpires()), 'type' => 'Bearer', ]; if (!empty($accessToken->getRefreshToken())) { $setData['refresh_token'] = $accessToken->getRefreshToken(); } if ($token) { //User already exists, update access token and refresh token if provided. DB::table('oauth2_tokens')->where('id', '=', $token->id)->update($setData); return $this->loginRedirect(User::find($token->user_id)); } // Check by login name to see if they already have an account $user = User::findOrCreate($poniverseUser->username, $poniverseUser->display_name, $poniverseUser->email); if (!$user->wasRecentlyCreated) { // We need to insert a new token row :O $setData['user_id'] = $user->id; $setData['external_user_id'] = $poniverseUser->id; $setData['service'] = 'poniverse'; DB::table('oauth2_tokens')->insert($setData); } return $this->loginRedirect($user); } } protected function loginRedirect($user, $rememberMe = true) { Auth::login($user, $rememberMe); return Redirect::to('/'); } }