Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 16 |
| Jwt | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 16 |
| getUser | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 9 |
|||
| getTtl | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 5 |
|||
| authUser | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| authTtl | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\AuthConnector\Connector; |
| 4 | |
| 5 | use Illuminate\Http\Response; |
| 6 | use Illuminate\Support\Facades\Log; |
| 7 | use \Qmp\Laravel\MicroService\Client\Tools\Request as ClientRequest; |
| 8 | use \Qmp\Laravel\MicroService\Client\Client; |
| 9 | use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; |
| 10 | use Tymon\JWTAuth\Exceptions\JWTException; |
| 11 | use Tymon\JWTAuth\Facades\JWTAuth; |
| 12 | use Tymon\JWTAuth\Http\Middleware\Authenticate; |
| 13 | |
| 14 | class Jwt implements ConnectorInterface |
| 15 | { |
| 16 | /** |
| 17 | * @param string $token |
| 18 | * @return false|array |
| 19 | */ |
| 20 | public function getUser(string $token) |
| 21 | { |
| 22 | try { |
| 23 | $user = $this->authUser(); |
| 24 | //Log::debug('user JWT:' . var_export($user, true)); |
| 25 | //$user = JWTAuth::setToken($token)->toUser(); |
| 26 | if (! $user) { |
| 27 | throw new UnauthorizedHttpException('jwt-auth', 'User not found'); |
| 28 | } |
| 29 | |
| 30 | if ($user->active != 1) { |
| 31 | throw new UnauthorizedHttpException('jwt-auth', 'User not active'); |
| 32 | } |
| 33 | } catch (JWTException $e) { |
| 34 | Log::debug('Unable to retrieve user:' . var_export(['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile()], true)); |
| 35 | throw new UnauthorizedHttpException('jwt-auth', $e->getMessage(), $e, $e->getCode()); |
| 36 | } |
| 37 | |
| 38 | return $user->toArray(); |
| 39 | } |
| 40 | |
| 41 | public function getTtl() |
| 42 | { |
| 43 | $ttl = $this->authTtl(); |
| 44 | |
| 45 | $timeInSecondToLeft = $ttl - time(); |
| 46 | if ($timeInSecondToLeft > 0) { |
| 47 | return $timeInSecondToLeft; |
| 48 | } |
| 49 | |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | protected function authUser() |
| 54 | { |
| 55 | return JWTAuth::parseToken()->authenticate(); |
| 56 | } |
| 57 | |
| 58 | protected function authTtl() |
| 59 | { |
| 60 | return JWTAuth::parseToken()->getPayload()->get('exp'); |
| 61 | } |
| 62 | } |