diff options
Diffstat (limited to 'src/CamilStaps/BotleaguesApi/TokenAuthenticationProvider.php')
-rw-r--r-- | src/CamilStaps/BotleaguesApi/TokenAuthenticationProvider.php | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/CamilStaps/BotleaguesApi/TokenAuthenticationProvider.php b/src/CamilStaps/BotleaguesApi/TokenAuthenticationProvider.php new file mode 100644 index 0000000..f3dad6e --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/TokenAuthenticationProvider.php @@ -0,0 +1,50 @@ +<?php +/** + * Created by PhpStorm. + * User: camilstaps + * Date: 12-5-15 + * Time: 14:41 + */ + +namespace CamilStaps\BotleaguesApi; + +use Dingo\Api\Auth\ProviderInterface; +use Dingo\Api\Routing\Route; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Auth; +use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; + +class TokenAuthenticationProvider implements ProviderInterface { + + /** + * Authenticate the request and return the authenticated user instance. + * + * @param \Illuminate\Http\Request $request + * @param \Dingo\Api\Routing\Route $route + * + * @return mixed + */ + public function authenticate(Request $request, Route $route) { + if (!$request->has(['user_id', 'token'])) { + throw new UnauthorizedHttpException(null, "Include user_id and token in your request."); + } + + $user = User::find($request->get('user_id')); + if ($user != null && $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'; + } +}
\ No newline at end of file |