Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Models/ApiKey.php
10263 views
1
<?php
2
3
namespace Pterodactyl\Models;
4
5
use Illuminate\Support\Str;
6
use Webmozart\Assert\Assert;
7
use Pterodactyl\Services\Acl\Api\AdminAcl;
8
use Laravel\Sanctum\Contracts\HasAbilities;
9
use Illuminate\Database\Eloquent\Relations\BelongsTo;
10
use Illuminate\Database\Eloquent\Factories\HasFactory;
11
12
/**
13
* Pterodactyl\Models\ApiKey.
14
*
15
* @property int $id
16
* @property int $user_id
17
* @property int $key_type
18
* @property string $identifier
19
* @property string $token
20
* @property array|null $allowed_ips
21
* @property string|null $memo
22
* @property \Illuminate\Support\Carbon|null $last_used_at
23
* @property \Illuminate\Support\Carbon|null $expires_at
24
* @property \Illuminate\Support\Carbon|null $created_at
25
* @property \Illuminate\Support\Carbon|null $updated_at
26
* @property int $r_servers
27
* @property int $r_nodes
28
* @property int $r_allocations
29
* @property int $r_users
30
* @property int $r_locations
31
* @property int $r_nests
32
* @property int $r_eggs
33
* @property int $r_database_hosts
34
* @property int $r_server_databases
35
* @property User $tokenable
36
* @property User $user
37
*
38
* @method static \Database\Factories\ApiKeyFactory factory(...$parameters)
39
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey newModelQuery()
40
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey newQuery()
41
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey query()
42
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereAllowedIps($value)
43
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereCreatedAt($value)
44
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereId($value)
45
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereIdentifier($value)
46
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereKeyType($value)
47
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereLastUsedAt($value)
48
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereMemo($value)
49
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRAllocations($value)
50
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRDatabaseHosts($value)
51
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereREggs($value)
52
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRLocations($value)
53
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRNests($value)
54
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRNodes($value)
55
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRServerDatabases($value)
56
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRServers($value)
57
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRUsers($value)
58
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereToken($value)
59
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereUpdatedAt($value)
60
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereUserId($value)
61
*
62
* @mixin \Eloquent
63
*/
64
class ApiKey extends Model implements HasAbilities
65
{
66
/** @use HasFactory<\Database\Factories\ApiKeyFactory> */
67
use HasFactory;
68
69
/**
70
* The resource name for this model when it is transformed into an
71
* API representation using fractal.
72
*/
73
public const RESOURCE_NAME = 'api_key';
74
/**
75
* Different API keys that can exist on the system.
76
*/
77
public const TYPE_NONE = 0;
78
public const TYPE_ACCOUNT = 1;
79
/* @deprecated */
80
public const TYPE_APPLICATION = 2;
81
/* @deprecated */
82
public const TYPE_DAEMON_USER = 3;
83
/* @deprecated */
84
public const TYPE_DAEMON_APPLICATION = 4;
85
/**
86
* The length of API key identifiers.
87
*/
88
public const IDENTIFIER_LENGTH = 16;
89
/**
90
* The length of the actual API key that is encrypted and stored
91
* in the database.
92
*/
93
public const KEY_LENGTH = 32;
94
95
/**
96
* The table associated with the model.
97
*/
98
protected $table = 'api_keys';
99
100
/**
101
* Cast values to correct type.
102
*/
103
protected $casts = [
104
'allowed_ips' => 'array',
105
'user_id' => 'int',
106
'last_used_at' => 'datetime',
107
'expires_at' => 'datetime',
108
self::CREATED_AT => 'datetime',
109
self::UPDATED_AT => 'datetime',
110
'r_' . AdminAcl::RESOURCE_USERS => 'int',
111
'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int',
112
'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'int',
113
'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'int',
114
'r_' . AdminAcl::RESOURCE_EGGS => 'int',
115
'r_' . AdminAcl::RESOURCE_LOCATIONS => 'int',
116
'r_' . AdminAcl::RESOURCE_NESTS => 'int',
117
'r_' . AdminAcl::RESOURCE_NODES => 'int',
118
'r_' . AdminAcl::RESOURCE_SERVERS => 'int',
119
];
120
121
/**
122
* Fields that are mass assignable.
123
*/
124
protected $fillable = [
125
'identifier',
126
'token',
127
'allowed_ips',
128
'memo',
129
'last_used_at',
130
'expires_at',
131
];
132
133
/**
134
* Fields that should not be included when calling toArray() or toJson()
135
* on this model.
136
*/
137
protected $hidden = ['token'];
138
139
/**
140
* Rules to protect against invalid data entry to DB.
141
*/
142
public static array $validationRules = [
143
'user_id' => 'required|exists:users,id',
144
'key_type' => 'present|integer|min:0|max:4',
145
'identifier' => 'required|string|size:16|unique:api_keys,identifier',
146
'token' => 'required|string',
147
'memo' => 'required|nullable|string|max:500',
148
'allowed_ips' => 'nullable|array',
149
'allowed_ips.*' => 'string',
150
'last_used_at' => 'nullable|date',
151
'expires_at' => 'nullable|date',
152
'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3',
153
'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'integer|min:0|max:3',
154
'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'integer|min:0|max:3',
155
'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'integer|min:0|max:3',
156
'r_' . AdminAcl::RESOURCE_EGGS => 'integer|min:0|max:3',
157
'r_' . AdminAcl::RESOURCE_LOCATIONS => 'integer|min:0|max:3',
158
'r_' . AdminAcl::RESOURCE_NESTS => 'integer|min:0|max:3',
159
'r_' . AdminAcl::RESOURCE_NODES => 'integer|min:0|max:3',
160
'r_' . AdminAcl::RESOURCE_SERVERS => 'integer|min:0|max:3',
161
];
162
163
public function can($ability)
164
{
165
// todo: this was never initially implemented and only became obvious once
166
// internal tooling was updated and started catching this mistake.
167
return false;
168
}
169
170
public function cant($ability)
171
{
172
return ! $this->can($ability);
173
}
174
175
/**
176
* Returns the user this token is assigned to.
177
*
178
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Pterodactyl\Models\User, $this>
179
*/
180
public function user(): BelongsTo
181
{
182
return $this->belongsTo(User::class);
183
}
184
185
/**
186
* Required for support with Laravel Sanctum.
187
*
188
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Pterodactyl\Models\User, $this>
189
*
190
* @see \Laravel\Sanctum\Guard::supportsTokens()
191
*/
192
public function tokenable(): BelongsTo
193
{
194
return $this->user();
195
}
196
197
/**
198
* Finds the model matching the provided token.
199
*/
200
public static function findToken(string $token): ?self
201
{
202
$identifier = substr($token, 0, self::IDENTIFIER_LENGTH);
203
204
$model = static::where('identifier', $identifier)->first();
205
if (!is_null($model) && decrypt($model->token) === substr($token, strlen($identifier))) {
206
return $model;
207
}
208
209
return null;
210
}
211
212
/**
213
* Returns the standard prefix for API keys in the system.
214
*/
215
public static function getPrefixForType(int $type): string
216
{
217
Assert::oneOf($type, [self::TYPE_ACCOUNT, self::TYPE_APPLICATION]);
218
219
return $type === self::TYPE_ACCOUNT ? 'ptlc_' : 'ptla_';
220
}
221
222
/**
223
* Generates a new identifier for an API key.
224
*/
225
public static function generateTokenIdentifier(int $type): string
226
{
227
$prefix = self::getPrefixForType($type);
228
229
return $prefix . Str::random(self::IDENTIFIER_LENGTH - strlen($prefix));
230
}
231
}
232
233