diff options
author | Camil Staps | 2015-05-12 18:35:07 +0200 |
---|---|---|
committer | Camil Staps | 2015-05-12 18:35:07 +0200 |
commit | ac28498d8b96733dfa679ec86371aaa6825d3079 (patch) | |
tree | d6f6e5094e20fb4ee197830aa7dac7988e0ad279 /src/CamilStaps/BotleaguesApi/TokenAuthenticationProvider.php | |
parent | Cleanup; show user_token-token field on store request (POST) (diff) |
Authentication with tokens
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 |