Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 47 |
| AuthJwt | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
306 | |
0.00% |
0 / 47 |
| login | |
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 10 |
|||
| googleLogin | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 14 |
|||
| logout | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| refresh | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| respondWithToken | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 10 |
|||
| getAllPermissions | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 10 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\AuthConnector\Authenticator; |
| 4 | |
| 5 | use App\User; |
| 6 | use Qmp\Laravel\GdprCryptData\Hash\HashDb; |
| 7 | use Tymon\JWTAuth\Facades\JWTAuth; |
| 8 | |
| 9 | class AuthJwt |
| 10 | { |
| 11 | /** |
| 12 | * @param array $credentials |
| 13 | * @return array |
| 14 | * @throws \Exception |
| 15 | */ |
| 16 | public static function login(array $credentials) |
| 17 | { |
| 18 | if (isset($credentials['email'])) { |
| 19 | $credentials['email'] = HashDb::make($credentials['email']); |
| 20 | } |
| 21 | |
| 22 | if (isset($credentials['name'])) { |
| 23 | $credentials['name'] = HashDb::make($credentials['name']); |
| 24 | } |
| 25 | |
| 26 | if (empty($credentials['password']) || ! $token = auth()->attempt($credentials)) { |
| 27 | throw new \Exception('Unauthorized', 401); |
| 28 | } |
| 29 | |
| 30 | $user = JWTAuth::setToken($token)->toUser(); |
| 31 | if ($user->active != 1) { |
| 32 | throw new \Exception('User not active', 401); |
| 33 | } |
| 34 | |
| 35 | return self::respondWithToken($token/*, self::getAllPermissions()*/); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param string $tokenId |
| 40 | * @return array |
| 41 | * @throws \Exception |
| 42 | */ |
| 43 | public static function googleLogin(string $tokenId, \Google_Client $client) |
| 44 | { |
| 45 | // Specify the CLIENT_ID of the app that accesses the backend |
| 46 | //$client = new \Google_Client(['client_id' => env('GOOGLE_ID', '')]); |
| 47 | $payload = $client->verifyIdToken($tokenId); |
| 48 | if ($payload) { |
| 49 | $user = User::whereHash('email', $payload['email'])->first(); |
| 50 | |
| 51 | if (!$user) { |
| 52 | throw new \Exception('User not exist', 401); |
| 53 | } |
| 54 | |
| 55 | $user->google_id = $payload['sub']; |
| 56 | $user->save(); |
| 57 | |
| 58 | $token = JWTAuth::fromUser($user); |
| 59 | $request = request(); |
| 60 | $request->merge(['token' => $token]); |
| 61 | |
| 62 | if ($user->active != 1) { |
| 63 | throw new \Exception('User not active', 401); |
| 64 | } |
| 65 | |
| 66 | return self::respondWithToken($token/*, self::getAllPermissions($user)*/); |
| 67 | } |
| 68 | |
| 69 | throw new \Exception('invalid token', 401); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return array |
| 74 | */ |
| 75 | public static function logout() |
| 76 | { |
| 77 | JWTAuth::parseToken()->invalidate(); |
| 78 | |
| 79 | return ['message' => 'Successfully logged out']; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return array |
| 84 | */ |
| 85 | public static function refresh() |
| 86 | { |
| 87 | return self::respondWithToken(JWTAuth::parseToken()->refresh()); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the token array structure. |
| 92 | * |
| 93 | * @param string $token |
| 94 | * |
| 95 | * @param array $permissions |
| 96 | * @return array |
| 97 | */ |
| 98 | protected static function respondWithToken($token, $permissions = []) |
| 99 | { |
| 100 | $user = JWTAuth::setToken($token)->toUser(); |
| 101 | $result = [ |
| 102 | 'access_token' => $token, |
| 103 | 'token_type' => 'bearer', |
| 104 | 'expires_in' => auth()->factory()->getTTL() * 60, |
| 105 | 'user' => [ |
| 106 | 'id' => $user->id, |
| 107 | 'name' => $user->name, |
| 108 | 'email' => $user->email |
| 109 | ] |
| 110 | ]; |
| 111 | |
| 112 | if (!empty($permissions)) { |
| 113 | $result['permissions'] = $permissions; |
| 114 | } |
| 115 | |
| 116 | return $result; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get All user's permissions |
| 121 | * |
| 122 | * @return array |
| 123 | */ |
| 124 | public static function getAllPermissions(User $user = null) |
| 125 | { |
| 126 | if (!$user) { |
| 127 | $user = auth()->user(); |
| 128 | } |
| 129 | |
| 130 | $userPermissions = $user->permissions()->get()->toArray(); |
| 131 | $userPermissions = array_column($userPermissions, 'name'); |
| 132 | $roles = $user->roles()->get(); |
| 133 | |
| 134 | foreach ($roles as $role) { |
| 135 | $rolePermissions = $role->permissions()->get()->toArray(); |
| 136 | $rolePermissions = array_column($rolePermissions, 'name'); |
| 137 | $userPermissions = array_merge([], $userPermissions, $rolePermissions); |
| 138 | } |
| 139 | |
| 140 | return array_unique($userPermissions); |
| 141 | } |
| 142 | |
| 143 | } |