Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
50 / 50
AuthController
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
8 / 8
16
100.00% covered (success)
100.00%
50 / 50
 authenticate
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
17 / 17
 user
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
7 / 7
 checkToken
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 disconnect
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 renewToken
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 getToken
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
6 / 6
 errorResponseAuth
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 successResponseAuth
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
1<?php
2
3namespace Qmp\Laravel\ExternalUsers\Controllers;
4
5use App\Http\Controllers\Controller;
6use Illuminate\Http\Exceptions\HttpResponseException;
7use Illuminate\Http\Response;
8use Illuminate\Http\Request;
9use Illuminate\Support\Facades\Hash;
10use Illuminate\Support\Facades\Log;
11use Illuminate\Validation\Rule;
12use Qmp\Laravel\ExternalUsers\Models\EntityUser;
13use Qmp\Laravel\ExternalUsers\Models\User;
14use Qmp\Laravel\ExternalUsers\Services\Auth\Auth;
15use Qmp\Laravel\MicroService\Controllers\AbstractMicroServiceController;
16
17use Qmp\Laravel\MicroService\Client\Tools\Request as ClientRequest;
18use Qmp\Laravel\MicroService\Client\Client;
19
20class AuthController extends AbstractMicroServiceController
21{
22    public function authenticate(Request $request)
23    {
24        // Authentication of external user
25        $request->validate([
26            'email' => 'required|email',
27            'password' => 'required|string',
28        ]);
29
30        $user = User::where('email', '=', $request->email)
31            ->where('active', 1)
32            ->first();
33
34
35
36        if (!$user) {
37            return $this->errorResponseAuth();
38        }
39
40        $httprequest = ClientRequest::createObject('service_entities', 'entity/from-url', ['body' => [
41            'url' => $this->httpOrigin
42        ]]);
43        $response = Client::systemSend('post', $httprequest)->content;
44
45
46        $entityPwd = EntityUser::where('entity_id', $response['id'])
47            ->where('user_id', $user->id)
48            ->first();
49
50        if (!$entityPwd || !Hash::check($request->password, $entityPwd->password)) {
51            return $this->errorResponseAuth();
52        }
53
54        // Create token
55        Auth::createToken($user);
56
57        return $this->successResponseAuth($user->id, $user->api_token);
58    }
59
60    public function user(Request $request)
61    {
62        // Check token validity
63        $token = $this->getToken($request);
64        $user = Auth::check($token);
65        if (!$user) {
66            return $this->errorResponseAuth();
67        }
68
69        return response()->json([
70            'status'=> 'ok',
71            'user' => $user
72        ]);
73    }
74
75    public function checkToken(Request $request)
76    {
77        // Check token validity
78        $token = $this->getToken($request);
79        $user = Auth::check($token);
80        if (!$user) {
81            return $this->errorResponseAuth();
82        }
83
84        return $this->successResponseAuth($user->id, $token);
85    }
86
87    public function disconnect(Request $request)
88    {
89        // Remove token
90        $token = $this->getToken($request);
91        Auth::delete($token);
92
93        return response()->json(['status'=> 'ok']);
94    }
95
96    public function renewToken(Request $request)
97    {
98        // renew token ttl
99        $token = $this->getToken($request);
100        $user = Auth::check($token);
101        if (!$user) {
102            return $this->errorResponseAuth();
103        }
104
105        Auth::createToken($user);
106
107        return $this->successResponseAuth($user->id, $token);
108    }
109
110    protected function getToken(Request $request)
111    {
112        // Check token validity
113        $token = $request->bearerToken();
114        if (!$token) {
115            $token = $request->token;
116        }
117
118        if (!$token) {
119            throw new HttpResponseException($this->errorResponseAuth());
120        }
121
122        return $token;
123    }
124
125    protected function errorResponseAuth()
126    {
127        return response()->json(['errors' => ['Login Failed.']], 403);
128    }
129
130    protected function successResponseAuth($userId, $token)
131    {
132        return response()->json([
133            'status'=> 'ok',
134            'user_id' => $userId,
135            'access_token' => [
136                'token' => $token,
137                'ttl' => Auth::ttl($token)
138            ]
139        ]);
140    }
141}