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