1 <?php
2
3 namespace Skimia\ApiFusion\Auth;
4
5 use Illuminate\Support\Str;
6 use Illuminate\Http\Request;
7 use Dingo\Api\Routing\Route;
8 use Dingo\Api\Auth\Provider\Authorization;
9 use Cartalyst\Sentinel\Sentinel as AuthManager;
10 use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
11 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
12
13 class Sentinel extends Authorization
14 {
15 16 17 18 19
20 protected $auth;
21
22 23 24 25 26
27 protected $identifier;
28
29 30 31 32 33 34 35 36
37 public function __construct(AuthManager $auth, $identifier = 'email')
38 {
39 $this->auth = $auth;
40 $this->identifier = $identifier;
41 }
42
43 44 45 46 47 48 49 50
51 public function authenticate(Request $request, Route $route)
52 {
53 $this->validateAuthorizationHeader($request);
54
55 if ($user = $this->auth->getUser()) {
56 return $user;
57 }
58
59 throw new UnauthorizedHttpException(null, 'Please log in before perform this query.');
60 }
61
62 63 64 65 66 67 68 69 70
71 public function validateAuthorizationHeader(Request $request)
72 {
73 if (Str::startsWith(strtolower($request->headers->get('shield')), $this->getAuthorizationMethod())) {
74 return true;
75 }
76
77 throw new BadRequestHttpException;
78 }
79
80 81 82 83 84
85 public function getAuthorizationMethod()
86 {
87 return 'sentinel';
88 }
89 }
90