Completed
Push — master ( 6d13cc...4d6b90 )
by Mahmoud
03:30
created

AgentAuthentication::handle()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.5806
cc 4
eloc 13
nc 4
nop 2
1
<?php
2
3
namespace App\Containers\APIAuthentication\Middlewares;
4
5
use App\Containers\ApiAuthentication\Exceptions\AuthenticationFailedException;
6
use App\Containers\ApiAuthentication\Exceptions\MissingAgentIdException;
7
use App\Containers\User\Actions\CreateUserWithoutCredentialsAction;
8
use Closure;
9
use Illuminate\Foundation\Application;
10
use Jenssegers\Agent\Agent;
11
12
/**
13
 * Class AgentAuthentication
14
 *
15
 * @author  Mahmoud Zalt  <[email protected]>
16
 */
17
class AgentAuthentication
18
{
19
20
    /**
21
     * @var  \Jenssegers\Agent\Agent
22
     */
23
    private $agent;
24
25
    /**
26
     * @var  \Illuminate\Foundation\Application
27
     */
28
    private $app;
29
30
    /**
31
     * @var  \App\Containers\User\Actions\CreateUserWithoutCredentialsAction
32
     */
33
    private $createUserWithoutCredentialsAction;
34
35
    /**
36
     * AgentAuthentication constructor.
37
     *
38
     * @param \Illuminate\Foundation\Application                              $app
39
     * @param \Jenssegers\Agent\Agent                                         $agent
40
     * @param \App\Containers\User\Actions\CreateUserWithoutCredentialsAction $createUserWithoutCredentialsAction
41
     */
42
    public function __construct(
43
        Application $app,
44
        Agent $agent,
45
        CreateUserWithoutCredentialsAction $createUserWithoutCredentialsAction
46
    ) {
47
        $this->app = $app;
48
        $this->agent = $agent;
49
        $this->createUserWithoutCredentialsAction = $createUserWithoutCredentialsAction;
50
    }
51
52
53
    /**
54
     * Whenever the request doesn't have an Authorization header (token)
55
     * it must have a an Agent-Id header.
56
     *
57
     * @param  \Illuminate\Http\Request $request
58
     * @param  \Closure                 $next
59
     *
60
     * @return mixed
61
     */
62
    public function handle($request, Closure $next)
63
    {
64
        $token = $request->header('Authorization');
65
66
        if (!$token) {
67
            // read the agent ID header (set by the API users)
68
            $agentId = $request->header('Agent-Id');
69
70
            if (!$agentId) {
71
                throw new MissingAgentIdException();
72
            }
73
74
            $device = $this->agent->device();
75
            $platform = $this->agent->platform();
76
77
            $user = $this->createUserWithoutCredentialsAction->run($agentId, $device, $platform);
78
79
            if (!$user) {
80
                throw new AuthenticationFailedException(
81
                    'Something went wrong while trying to create user from the Agent ID: ' . $agentId
82
                );
83
            }
84
        }
85
86
        // return the response
87
        return $next($request);
88
    }
89
}
90