Completed
Push — master ( 951200...118666 )
by Nate
05:58 queued 03:32
created

Jwt::getSelfConsumable()   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 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/jwt/license
6
 * @link       https://www.flipboxfactory.com/jwt/organization/
7
 */
8
9
namespace flipbox\craft\jwt;
10
11
use Craft;
12
use craft\base\Plugin;
13
use craft\web\twig\variables\CraftVariable;
14
use flipbox\craft\jwt\models\Settings as SettingsModel;
15
use flipbox\craft\jwt\services\SelfConsumable;
16
use Lcobucci\JWT\Builder;
17
use Lcobucci\JWT\Claim\Factory as ClaimFactory;
18
use Lcobucci\JWT\Parser;
19
use Lcobucci\JWT\Parsing\Decoder;
20
use Lcobucci\JWT\Parsing\Encoder;
21
use Lcobucci\JWT\Token;
22
use Lcobucci\JWT\ValidationData;
23
use yii\base\Event;
24
use yii\log\Logger;
25
26
/**
27
 * @author Flipbox Factory <[email protected]>
28
 * @since 1.0.0
29
 *
30
 * @method SettingsModel getSettings()
31
 */
32
class Jwt extends Plugin
33
{
34
    /**
35
     * @inheritdoc
36
     */
37
    public function init()
38
    {
39
        parent::init();
40
41
        // Twig variables
42
        Event::on(
43
            CraftVariable::class,
44
            CraftVariable::EVENT_INIT,
45
            function (Event $event) {
46
                /** @var CraftVariable $variable */
47
                $variable = $event->sender;
48
                $variable->set('jwt', self::getInstance());
49
            }
50
        );
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function createSettingsModel(): SettingsModel
57
    {
58
        return new SettingsModel();
59
    }
60
61
    /*******************************************
62
     * SERVICES
63
     *******************************************/
64
65
    /**
66
     * @return services\SelfConsumable
67
     */
68
    public function getSelfConsumable(): SelfConsumable
69
    {
70
        return $this->get('authorization');
71
    }
72
73
    /*******************************************
74
     * JWT
75
     *******************************************/
76
77
    /**
78
     * @param Encoder|null $encoder
79
     * @param ClaimFactory|null $claimFactory
80
     *
81
     * @see [[Lcobucci\JWT\Builder::__construct()]]
82
     * @return Builder
83
     */
84
    public function getBuilder(Encoder $encoder = null, ClaimFactory $claimFactory = null): Builder
85
    {
86
        return new Builder($encoder, $claimFactory);
87
    }
88
89
    /**
90
     * @param Decoder|null $decoder
91
     * @param ClaimFactory|null $claimFactory
92
     *
93
     * @see [[Lcobucci\JWT\Parser::__construct()]]
94
     * @return Parser
95
     */
96
    public function getParser(Decoder $decoder = null, ClaimFactory $claimFactory = null): Parser
97
    {
98
        return new Parser($decoder, $claimFactory);
99
    }
100
101
    /**
102
     * @param int|null $currentTime
103
     *
104
     * @see [[Lcobucci\JWT\ValidationData::__construct()]]
105
     * @return ValidationData
106
     */
107
    public function getValidationData(int $currentTime = null): ValidationData
108
    {
109
        return new ValidationData($currentTime);
110
    }
111
112
    /**
113
     * @param Token $token
114
     * @param int|null $currentTime
115
     * @return bool
116
     */
117
    public function validateToken(Token $token, int $currentTime = null): bool
118
    {
119
        return $token->validate(
120
            $this->getValidationData($currentTime)
121
        );
122
    }
123
124
125
    /*******************************************
126
     * LOGGING
127
     *******************************************/
128
129
    /**
130
     * Logs a trace message.
131
     * Trace messages are logged mainly for development purpose to see
132
     * the execution work flow of some code.
133
     * @param string $message the message to be logged.
134
     * @param string $category the category of the message.
135
     */
136
    public static function trace($message, string $category = null)
137
    {
138
        Craft::getLogger()->log($message, Logger::LEVEL_TRACE, self::normalizeCategory($category));
139
    }
140
141
    /**
142
     * Logs an error message.
143
     * An error message is typically logged when an unrecoverable error occurs
144
     * during the execution of an application.
145
     * @param string $message the message to be logged.
146
     * @param string $category the category of the message.
147
     */
148
    public static function error($message, string $category = null)
149
    {
150
        Craft::getLogger()->log($message, Logger::LEVEL_ERROR, self::normalizeCategory($category));
151
    }
152
153
    /**
154
     * Logs a warning message.
155
     * A warning message is typically logged when an error occurs while the execution
156
     * can still continue.
157
     * @param string $message the message to be logged.
158
     * @param string $category the category of the message.
159
     */
160
    public static function warning($message, string $category = null)
161
    {
162
        Craft::getLogger()->log($message, Logger::LEVEL_WARNING, self::normalizeCategory($category));
163
    }
164
165
    /**
166
     * Logs an informative message.
167
     * An informative message is typically logged by an application to keep record of
168
     * something important (e.g. an administrator logs in).
169
     * @param string $message the message to be logged.
170
     * @param string $category the category of the message.
171
     */
172
    public static function info($message, string $category = null)
173
    {
174
        Craft::getLogger()->log($message, Logger::LEVEL_INFO, self::normalizeCategory($category));
175
    }
176
177
    /**
178
     * @param string|null $category
179
     * @return string
180
     */
181
    private static function normalizeCategory(string $category = null)
182
    {
183
        $normalizedCategory = 'JWT';
184
185
        if ($category === null) {
186
            return $normalizedCategory;
187
        }
188
189
        return $normalizedCategory . ': ' . $category;
190
    }
191
}
192