* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ namespace CamilStaps\BotleaguesApi; use CamilStaps\BotleaguesApi\Database\User; use Dingo\Api\Auth\Provider\Provider; use Dingo\Api\Routing\Route; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; class TokenAuthenticationProvider implements Provider { /** * Authenticate the request and return the authenticated user instance. * * @param \Illuminate\Http\Request $request * @param \Dingo\Api\Routing\Route $route * * @throw UnauthorizedHttpException invalid authentication * * @return mixed */ public function authenticate(Request $request, Route $route) { if (!$request->has(['email', 'token'])) { throw new UnauthorizedHttpException(null, "Include email and token in your request."); } $user = User::findOrFail($request->get('email')); if ($user->validToken($request->get('token'))) { Auth::login($user); return Auth::user(); } throw new UnauthorizedHttpException(null, "Invalid credentials"); } /** * Get the providers authorization method. * * @return string */ public function getAuthorizationMethod() { return 'token'; } }