1 | <?php |
||
2 | /** |
||
3 | * ApiToken.php |
||
4 | * |
||
5 | * api_tokens simple tokens for api |
||
6 | * |
||
7 | * This program is free software: you can redistribute it and/or modify |
||
8 | * it under the terms of the GNU General Public License as published by |
||
9 | * the Free Software Foundation, either version 3 of the License, or |
||
10 | * (at your option) any later version. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
15 | * GNU General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU General Public License |
||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
19 | * |
||
20 | * @link https://www.librenms.org |
||
21 | * |
||
22 | * @copyright 2018 Tony Murray |
||
23 | * @author Tony Murray <[email protected]> |
||
24 | */ |
||
25 | |||
26 | namespace App\Models; |
||
27 | |||
28 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
29 | |||
30 | class ApiToken extends BaseModel |
||
31 | { |
||
32 | public $timestamps = false; |
||
33 | protected $table = 'api_tokens'; |
||
34 | |||
35 | // ---- Helper Functions ---- |
||
36 | |||
37 | /** |
||
38 | * Check if the given token is valid |
||
39 | * |
||
40 | * @param string $token |
||
41 | * @return bool |
||
42 | */ |
||
43 | public static function isValid($token, $user_id = null) |
||
44 | { |
||
45 | $query = self::query()->isEnabled()->where('token_hash', $token); |
||
46 | |||
47 | if (! is_null($user_id)) { |
||
48 | $query->where('user_id', $user_id); |
||
49 | } |
||
50 | |||
51 | return $query->exists(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Get User model based on the given API token (or null if invalid) |
||
56 | * |
||
57 | * @param string $token |
||
58 | * @return User|null |
||
59 | */ |
||
60 | public static function userFromToken($token) |
||
61 | { |
||
62 | return User::find(self::idFromToken($token)); |
||
63 | } |
||
64 | |||
65 | public static function generateToken(User $user, $description = '') |
||
66 | { |
||
67 | $token = new static; |
||
68 | $token->user_id = $user->user_id; |
||
69 | $token->token_hash = $bytes = bin2hex(random_bytes(16)); |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
70 | $token->description = $description; |
||
71 | $token->disabled = false; |
||
72 | $token->save(); |
||
73 | |||
74 | return $token; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Get the user_id for the given token. |
||
79 | * |
||
80 | * @param string $token |
||
81 | * @return int |
||
82 | */ |
||
83 | public static function idFromToken($token) |
||
84 | { |
||
85 | return self::query()->isEnabled()->where('token_hash', $token)->value('user_id'); |
||
86 | } |
||
87 | |||
88 | // ---- Query scopes ---- |
||
89 | |||
90 | public function scopeIsEnabled($query) |
||
91 | { |
||
92 | return $query->where('disabled', 0); |
||
93 | } |
||
94 | |||
95 | // ---- Define Relationships ---- |
||
96 | |||
97 | public function user(): BelongsTo |
||
98 | { |
||
99 | return $this->belongsTo(\App\Models\User::class, 'user_id'); |
||
100 | } |
||
101 | } |
||
102 |