Completed
Pull Request — master (#98)
by Alejandro
06:29
created

AuthenticateAction::dispatch()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 14
Ratio 56 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 3
nop 2
dl 14
loc 25
ccs 16
cts 16
cp 1
crap 4
rs 8.5806
c 0
b 0
f 0
1
<?php
2
namespace Shlinkio\Shlink\Rest\Action;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
5
use Interop\Http\ServerMiddleware\DelegateInterface;
6
use Psr\Http\Message\ResponseInterface as Response;
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Psr\Log\LoggerInterface;
9
use Shlinkio\Shlink\Rest\Authentication\JWTService;
10
use Shlinkio\Shlink\Rest\Authentication\JWTServiceInterface;
11
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
12
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
13
use Shlinkio\Shlink\Rest\Util\RestUtils;
14
use Zend\Diactoros\Response\JsonResponse;
15
use Zend\I18n\Translator\TranslatorInterface;
16
17
class AuthenticateAction extends AbstractRestAction
18
{
19
    /**
20
     * @var TranslatorInterface
21
     */
22
    private $translator;
23
    /**
24
     * @var ApiKeyService|ApiKeyServiceInterface
25
     */
26
    private $apiKeyService;
27
    /**
28
     * @var JWTServiceInterface
29
     */
30
    private $jwtService;
31
32
    /**
33
     * AuthenticateAction constructor.
34
     * @param ApiKeyServiceInterface|ApiKeyService $apiKeyService
35
     * @param JWTServiceInterface|JWTService $jwtService
36
     * @param TranslatorInterface $translator
37
     * @param LoggerInterface|null $logger
38
     *
39
     * @Inject({ApiKeyService::class, JWTService::class, "translator", "Logger_Shlink"})
40
     */
41 3
    public function __construct(
42
        ApiKeyServiceInterface $apiKeyService,
43
        JWTServiceInterface $jwtService,
44
        TranslatorInterface $translator,
45
        LoggerInterface $logger = null
46
    ) {
47 3
        parent::__construct($logger);
48 3
        $this->translator = $translator;
49 3
        $this->apiKeyService = $apiKeyService;
50 3
        $this->jwtService = $jwtService;
51 3
    }
52
53
    /**
54
     * @param Request $request
55
     * @param DelegateInterface $delegate
56
     * @return null|Response
57
     * @throws \InvalidArgumentException
58
     */
59 3
    public function process(Request $request, DelegateInterface $delegate)
60
    {
61 3
        $authData = $request->getParsedBody();
62 3 View Code Duplication
        if (! isset($authData['apiKey'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63 1
            return new JsonResponse([
64 1
                'error' => RestUtils::INVALID_ARGUMENT_ERROR,
65 1
                'message' => $this->translator->translate(
66 1
                    'You have to provide a valid API key under the "apiKey" param name.'
67
                ),
68 1
            ], self::STATUS_BAD_REQUEST);
69
        }
70
71
        // Authenticate using provided API key
72 2
        $apiKey = $this->apiKeyService->getByKey($authData['apiKey']);
73 2 View Code Duplication
        if (! isset($apiKey) || ! $apiKey->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74 1
            return new JsonResponse([
75 1
                'error' => RestUtils::INVALID_API_KEY_ERROR,
76 1
                'message' => $this->translator->translate('Provided API key does not exist or is invalid.'),
77 1
            ], self::STATUS_UNAUTHORIZED);
78
        }
79
80
        // Generate a JSON Web Token that will be used for authorization in next requests
81 1
        $token = $this->jwtService->create($apiKey);
82 1
        return new JsonResponse(['token' => $token]);
83
    }
84
}
85