Completed
Push — master ( 31e4dd...c38430 )
by Christian
07:22
created

AuthController::checkAuthAction()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 30
rs 8.439
cc 5
eloc 18
nc 4
nop 1
1
<?php
2
3
/**
4
 * This file is part of tenside/core-bundle.
5
 *
6
 * (c) Christian Schiffler <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core-bundle
14
 * @author     Christian Schiffler <[email protected]>
15
 * @author     Yanick Witschi <[email protected]>
16
 * @copyright  2015 Christian Schiffler <[email protected]>
17
 * @license    https://github.com/tenside/core-bundle/blob/master/LICENSE MIT
18
 * @link       https://github.com/tenside/core-bundle
19
 * @filesource
20
 */
21
22
namespace Tenside\CoreBundle\Controller;
23
24
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
25
use Symfony\Component\HttpFoundation\JsonResponse;
26
use Symfony\Component\HttpFoundation\Request;
27
use Tenside\CoreBundle\Annotation\ApiDescription;
28
use Tenside\CoreBundle\Security\UserInformationInterface;
29
30
/**
31
 * The main entry point.
32
 */
33
class AuthController extends AbstractController
34
{
35
    /**
36
     * Try to validate the user from the request and return a jwt authentication result then.
37
     *
38
     * @param Request $request The request.
39
     *
40
     * @return JsonResponse
41
     *
42
     * @throws \RuntimeException For invalid user classes.
43
     *
44
     * @ApiDoc(
45
     *   section="auth",
46
     *   statusCodes = {
47
     *     200 = "When everything worked out ok",
48
     *     401 = "When the request was unauthorized."
49
     *   },
50
     *   filters = {
51
     *     {
52
     *       "name": "ttl",
53
     *       "dataType" = "int",
54
     *       "description" = "The amount of seconds the token shall be valid or -1 for unlimited (default: 3600).",
55
     *       "required" = false
56
     *     }
57
     *   }
58
     * )
59
     * @ApiDescription(
60
     *   response={
61
     *    "status" = {
62
     *      "dataType" = "choice",
63
     *      "description" = "OK or unauthorized",
64
     *      "format" = "['OK', 'unauthorized']",
65
     *    },
66
     *    "token" = {
67
     *      "dataType" = "string",
68
     *      "description" = "The JWT (only if status ok).",
69
     *    },
70
     *    "acl" = {
71
     *      "actualType" = "collection",
72
     *      "subType" = "string",
73
     *      "description" = "The roles of the authenticated user.",
74
     *    },
75
     *    "username" = {
76
     *      "actualType" = "string",
77
     *      "description" = "The username of the authenticated user.",
78
     *    },
79
     *   },
80
     * )
81
     */
82
    public function checkAuthAction(Request $request)
83
    {
84
        $user = $this->getUser();
85
86
        if (null !== $user) {
87
            if (!$user instanceof UserInformationInterface) {
88
                throw new \RuntimeException('Invalid user object');
89
            }
90
91
            $lifetime = $request->get('ttl', 3600);
92
            if (-1 === $lifetime) {
93
                $lifetime = null;
94
            }
95
96
            $token = $this->get('tenside.jwt_authenticator')->getTokenForData($user, $lifetime);
97
            return new JsonResponse(
98
                [
99
                    'status'    => 'OK',
100
                    'token'     => $token,
101
                    'acl'       => $user->getRoles(),
102
                    'username'  => $user->getUsername(),
103
                    'ttl'       => (null === $lifetime) ? 'unlimited' : date('r', (time() + $lifetime))
104
                ],
105
                JsonResponse::HTTP_OK,
106
                ['Authentication' => $token]
107
            );
108
        }
109
110
        return new JsonResponse(['status' => 'unauthorized'], JsonResponse::HTTP_UNAUTHORIZED);
111
    }
112
}
113