Completed
Push — master ( d797db...0417d6 )
by Bart
02:16
created

ApiAuthService::getUserKeyModelByKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Craft;
4
5
/**
6
 * Class ApiAuthService.
7
 *
8
 * @author    Nerds & Company
9
 * @copyright Copyright (c) 2015, Nerds & Company
10
 * @license   MIT
11
 *
12
 * @link      http://www.nerds.company
13
 */
14
class ApiAuthService extends BaseApplicationComponent
15
{
16
    /**
17
     * @param string $key
18
     * @return bool
19
     */
20 3
    public function authenticateKey($key)
21
    {
22 3
        $userKeyModel = $this->getUserKeyModelByKey($key);
23 3
        if($userKeyModel && $userKeyModel->expires > new DateTime()) {
24 1
            return craft()->userSession->loginByUserId($userKeyModel->userId);
25
        }
26 2
        return false;
27
    }
28
29
    /**
30
     * @return string
31
     */
32 1
    public function generateKey()
33
    {
34 1
        return bin2hex(openssl_random_pseudo_bytes(32));
35
    }
36
37
    /**
38
     * @param UserModel $user
39
     * @param $key
40
     * @return bool
41
     */
42 2
    public function saveKey(UserModel $user, $key)
43
    {
44 2
        $model = $this->getNewUserKeyModel();
45 2
        $model->userId = $user->getAttribute('id');
46 2
        $model->key = $key;
47 2
        $model->expires = new DateTime('+ 1 week');
48 2
        return $model->save();
49
    }
50
51
    /**
52
     * Is it an options request?
53
     *
54
     * @return bool
55
     */
56 2
    public function isOptionsRequest()
0 ignored issues
show
Coding Style introduced by
isOptionsRequest uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
57
    {
58 2
        return isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS';
59
    }
60
61
    /**
62
     * Send an options response
63
     */
64 1
    public function setCorsHeaders()
65
    {
66 1
        $this->setHeader('Access-Control-Allow-Headers', 'Authorization');
67 1
        $this->setHeader('Access-Control-Allow-Origin', '*');
68 1
    }
69
70
    /**
71
     * @codeCoverageIgnore
72
     *
73
     * @return ApiAuth_UserKeyModel
74
     */
75
    protected function getNewUserKeyModel()
76
    {
77
        return new ApiAuth_UserKeyModel();
78
    }
79
80
    /**
81
     * @codeCoverageIgnore
82
     *
83
     * @param string $key
84
     * @return ApiAuth_UserKeyModel
85
     */
86
    protected function getUserKeyModelByKey($key)
87
    {
88
        return ApiAuth_UserKeyRecord::model()->findByAttributes(array('key' => $key));
89
    }
90
91
    /**
92
     * @codeCoverageIgnore
93
     *
94
     * @param string $key
95
     * @param string $value
96
     */
97
    protected function setHeader($key, $value)
98
    {
99
        HeaderHelper::setHeader($key . ': ' . $value);
100
    }
101
}
102