public function createToken(Request $request) { $json = $request->json()->all(); $request = new Request(); $request->request->replace($json); Authorizer::setRequest($request); return $this->respond(Authorizer::issueAccessToken()); }
/** * @param Request $request * @return \Illuminate\Http\RedirectResponse|\Laravel\Lumen\Http\Redirector */ public function callback(Request $request) { $state = $request->get('state'); $sessionState = Session::get('google.oauth2state'); $code = $request->get('code'); if ($request->get('error')) { $request->session()->flash('error', 'auth.error'); return redirect(route('auth.loginForm')); } if (empty($state) || $state !== $sessionState) { Session::forget('google.oauth2state'); $request->session()->flash('error', 'auth.error'); return redirect(route('auth.loginForm')); } $token = $this->provider->getAccessToken('authorization_code', ['code' => $code]); try { /** @var GoogleUser $ownerDetails */ $ownerDetails = $this->provider->getResourceOwner($token); $email = $ownerDetails->getEmail(); // if we already have the email in DB we log the user if (!$this->repository->exists(['email' => $email])) { $lastName = $ownerDetails->getLastName(); $firstName = $ownerDetails->getFirstName(); $this->createUser($firstName, $lastName, $email); } // we try to logged in the user with the email and the google oauth access token Input::merge(['client_id' => Config::get('oauth2.web_client.client_id')]); Input::merge(['client_secret' => Config::get('oauth2.web_client.client_secret')]); Input::merge(['grant_type' => 'google']); Input::merge(['username' => $email]); Input::merge(['password' => $token->getToken()]); try { Authorizer::issueAccessToken(); return redirect('/'); } catch (\Exception $e) { $request->session()->flash('error', 'auth.login_error'); return redirect(route('auth.loginForm')); } } catch (ModelNotValid $e) { $request->session()->flash('error', 'auth.error'); Log::warn($e->getMessage()); return redirect(route('auth.loginForm')); } catch (\Exception $e) { $request->session()->flash('error', 'auth.error'); Log::warn($e->getMessage()); return redirect(route('auth.loginForm')); } }
* * Routes For Authenticate Client : Line 47 * * Method : Post * * Parameter Send : [grant_type => 'client_credentials', client_id => 'xx', client_secret => 'xx'] * * Returned JSend : [token => [access_token => 'xx', token_type => 'Bearer', expires_in => 00]] */ $app->post('/oauth/access_token', function () { $issue['token'] = \LucaDegasperi\OAuth2Server\Facades\Authorizer::issueAccessToken(); if (\Illuminate\Support\Facades\Auth::check()) { $issue['me'] = \Illuminate\Support\Facades\Auth::user()->toArray(); return new \App\Libraries\JSend('success', (array) $issue); } else { return new \App\Libraries\JSend('error', (array) ['No Data'], 'User invalid :( '); } }); $app->post('/oauth/client/access_token', function () { $issue['token'] = \LucaDegasperi\OAuth2Server\Facades\Authorizer::issueAccessToken(); return new \App\Libraries\JSend('success', (array) $issue); }); $app->group(['middleware' => 'oauth', 'namespace' => 'App\\Http\\Controllers'], function ($app) { // ------------------------------------------------------------------------------------ // Gettin' Me // ------------------------------------------------------------------------------------ $app->get('/me', function () { $user = \LucaDegasperi\OAuth2Server\Facades\Authorizer::getResourceOwnerId(); return $user; }); });
}); Route::get('oauth/authorize', ['as' => 'oauth.authorize.get', 'middleware' => ['check-authorization-params', 'auth'], function () { // display a form where the user can authorize the client to access it's data $authParams = Authorizer::getAuthCodeRequestParams(); $formParams = array_except($authParams, 'client'); $formParams['client_id'] = $authParams['client']->getId(); return View::make('oauth.authorization-form', ['params' => $formParams, 'client' => $authParams['client']]); }]); Route::post('oauth/authorize', ['as' => 'oauth.authorize.post', 'middleware' => ['csrf', 'check-authorization-params', 'auth'], function () { $params = Authorizer::getAuthCodeRequestParams(); $params['user_id'] = Auth::user()->id; $redirectUri = ''; // if the user has allowed the client to access its data, redirect back to the client with an auth code if (Input::get('approve') !== null) { $redirectUri = Authorizer::issueAuthCode('user', $params['user_id'], $params); } // if the user has denied the client to access its data, redirect back to the client with an error message if (Input::get('deny') !== null) { $redirectUri = Authorizer::authCodeRequestDeniedRedirectUri(); } return Redirect::to($redirectUri); }]); Route::post('oauth/access_token', ['as' => 'access_token', function () { header('Content-Type:application/json; charset=utf-8'); return Response::json(Authorizer::issueAccessToken()); }]); Route::get('/callback', function () { if (Input::has('code')) { return view('callback'); } });
return view('welcome'); }); //Testing Routes Route::get('/test-oauth', function () { return view('test.integracaoOAuth'); }); Route::get('/test-payment', function () { return view('test.payment'); }); //User Routes Route::get('user/info', ['middleware' => 'oauth', function () { return Authorizer::getResourceOwnerId(); }]); //Products Routes Route::get('products/info', function () { return response()->json(Product::all()); }); //Laravel Socialite Login, Data and Login Routes Route::get('service/{providerName}/login', 'ServiceAuthController@redirectToProvider'); Route::get('service/{providerName}/data', 'ServiceAuthController@handleProviderData'); //Payment Routes Route::post('payment/{service}/post', 'PaymentController@postPayment'); Route::get('payment/{service}/status', 'PaymentController@getPaymentStatus'); Route::get('payment/{service}/cancel', 'PaymentController@getPaymentCancel'); //Oauth Routes Route::post('service/login', function () { return response()->json(Authorizer::issueAccessToken()); }); Route::post('oauth/access_token', function () { return response()->json(Authorizer::issueAccessToken()); });
/** * Issue oauth acess token & refresh access token. * * @return Response */ public function issueAccessToken() { return Authorizer::issueAccessToken(); }
| Here is where you can register all of the routes for an application. | It is a breeze. Simply tell Lumen the URIs it should respond to | and give it the Closure to call when that URI is requested. | */ $app->get('/', function () use($app) { return $app->version(); }); $app->get('/hello', function () use($app) { return 'Hello! It\'s me'; }); $app->get('/example', function () use($app) { return view('example'); }); $app->post('oauth/access_token', function () use($app) { $token = Authorizer::issueAccessToken(); return response()->json($token); }); $app->get('oauth/authorize', ['as' => 'oauth.authorize.get', 'middleware' => ['check-authorization-params', 'auth'], function () { $authParams = Authorizer::getAuthCodeRequestParams(); $formParams = array_except($authParams, 'client'); $formParams['client_id'] = $authParams['client']->getId(); $formParams['scope'] = implode(config('oauth2.scope_delimiter'), array_map(function ($scope) { return $scope->getId(); }, $authParams['scopes'])); return View::make('authorization', ['params' => $formParams, 'client' => $authParams['client']]); }]); $app->post('oauth/authorize', ['as' => 'oauth.authorize.post', 'middleware' => ['csrf', 'check-authorization-params', 'auth'], function () { $params = Authorizer::getAuthCodeRequestParams(); $params['user_id'] = 6; //Auth::user()->id;
/** * Log the given user ID into the application. * * @param mixed $id * * @return \Illuminate\Contracts\Auth\Authenticatable * * @internal param bool $remember */ public function tokenById($id) { $user = $this->provider->retrieveById($id); if (!is_null($user)) { $this->request['user_id'] = $user->id; $this->request['grant_type'] = 'forced'; return Authorizer::issueAccessToken(); } return false; }