mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-14 07:27:47 +00:00
fix(#136): mac address matching is case senstive
This commit is contained in:
parent
a5cb38421e
commit
1298814521
3 changed files with 82 additions and 5 deletions
|
|
@ -20,6 +20,14 @@ class Device extends Model
|
||||||
|
|
||||||
protected $guarded = ['id'];
|
protected $guarded = ['id'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the MAC address attribute, normalizing to uppercase.
|
||||||
|
*/
|
||||||
|
public function setMacAddressAttribute(?string $value): void
|
||||||
|
{
|
||||||
|
$this->attributes['mac_address'] = $value ? mb_strtoupper($value) : null;
|
||||||
|
}
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'battery_notification_sent' => 'boolean',
|
'battery_notification_sent' => 'boolean',
|
||||||
'proxy_cloud' => 'boolean',
|
'proxy_cloud' => 'boolean',
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ use Illuminate\Support\Str;
|
||||||
Route::get('/display', function (Request $request) {
|
Route::get('/display', function (Request $request) {
|
||||||
$mac_address = $request->header('id');
|
$mac_address = $request->header('id');
|
||||||
$access_token = $request->header('access-token');
|
$access_token = $request->header('access-token');
|
||||||
$device = Device::where('mac_address', $mac_address)
|
$device = Device::where('mac_address', mb_strtoupper($mac_address ?? ''))
|
||||||
->where('api_key', $access_token)
|
->where('api_key', $access_token)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
|
|
@ -29,7 +29,7 @@ Route::get('/display', function (Request $request) {
|
||||||
if ($auto_assign_user) {
|
if ($auto_assign_user) {
|
||||||
// Create a new device and assign it to this user
|
// Create a new device and assign it to this user
|
||||||
$device = Device::create([
|
$device = Device::create([
|
||||||
'mac_address' => $mac_address,
|
'mac_address' => mb_strtoupper($mac_address ?? ''),
|
||||||
'api_key' => $access_token,
|
'api_key' => $access_token,
|
||||||
'user_id' => $auto_assign_user->id,
|
'user_id' => $auto_assign_user->id,
|
||||||
'name' => "{$auto_assign_user->name}'s TRMNL",
|
'name' => "{$auto_assign_user->name}'s TRMNL",
|
||||||
|
|
@ -204,7 +204,7 @@ Route::get('/setup', function (Request $request) {
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$device = Device::where('mac_address', $mac_address)->first();
|
$device = Device::where('mac_address', mb_strtoupper($mac_address))->first();
|
||||||
|
|
||||||
if (! $device) {
|
if (! $device) {
|
||||||
// Check if there's a user with assign_new_devices enabled
|
// Check if there's a user with assign_new_devices enabled
|
||||||
|
|
@ -219,7 +219,7 @@ Route::get('/setup', function (Request $request) {
|
||||||
|
|
||||||
// Create a new device and assign it to this user
|
// Create a new device and assign it to this user
|
||||||
$device = Device::create([
|
$device = Device::create([
|
||||||
'mac_address' => $mac_address,
|
'mac_address' => mb_strtoupper($mac_address),
|
||||||
'api_key' => Str::random(22),
|
'api_key' => Str::random(22),
|
||||||
'user_id' => $auto_assign_user->id,
|
'user_id' => $auto_assign_user->id,
|
||||||
'name' => "{$auto_assign_user->name}'s TRMNL",
|
'name' => "{$auto_assign_user->name}'s TRMNL",
|
||||||
|
|
@ -345,7 +345,7 @@ Route::post('/display/update', function (Request $request) {
|
||||||
Route::post('/screens', function (Request $request) {
|
Route::post('/screens', function (Request $request) {
|
||||||
$mac_address = $request->header('id');
|
$mac_address = $request->header('id');
|
||||||
$access_token = $request->header('access-token');
|
$access_token = $request->header('access-token');
|
||||||
$device = Device::where('mac_address', $mac_address)
|
$device = Device::where('mac_address', mb_strtoupper($mac_address ?? ''))
|
||||||
->where('api_key', $access_token)
|
->where('api_key', $access_token)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -954,3 +954,72 @@ test('setup endpoint handles non-existent device model gracefully', function ():
|
||||||
expect($device)->not->toBeNull()
|
expect($device)->not->toBeNull()
|
||||||
->and($device->device_model_id)->toBeNull();
|
->and($device->device_model_id)->toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('setup endpoint matches MAC address case-insensitively', function (): void {
|
||||||
|
// Create device with lowercase MAC address
|
||||||
|
$device = Device::factory()->create([
|
||||||
|
'mac_address' => 'a1:b2:c3:d4:e5:f6',
|
||||||
|
'api_key' => 'test-api-key',
|
||||||
|
'friendly_id' => 'test-device',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Request with uppercase MAC address should still match
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'id' => 'A1:B2:C3:D4:E5:F6',
|
||||||
|
])->get('/api/setup');
|
||||||
|
|
||||||
|
$response->assertOk()
|
||||||
|
->assertJson([
|
||||||
|
'status' => 200,
|
||||||
|
'api_key' => 'test-api-key',
|
||||||
|
'friendly_id' => 'test-device',
|
||||||
|
'message' => 'Welcome to TRMNL BYOS',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('display endpoint matches MAC address case-insensitively', function (): void {
|
||||||
|
// Create device with lowercase MAC address
|
||||||
|
$device = Device::factory()->create([
|
||||||
|
'mac_address' => 'a1:b2:c3:d4:e5:f6',
|
||||||
|
'api_key' => 'test-api-key',
|
||||||
|
'current_screen_image' => 'test-image',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Request with uppercase MAC address should still match
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'id' => 'A1:B2:C3:D4:E5:F6',
|
||||||
|
'access-token' => $device->api_key,
|
||||||
|
'rssi' => -70,
|
||||||
|
'battery_voltage' => 3.8,
|
||||||
|
'fw-version' => '1.0.0',
|
||||||
|
])->get('/api/display');
|
||||||
|
|
||||||
|
$response->assertOk()
|
||||||
|
->assertJson([
|
||||||
|
'status' => '0',
|
||||||
|
'filename' => 'test-image.bmp',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('screens endpoint matches MAC address case-insensitively', function (): void {
|
||||||
|
Queue::fake();
|
||||||
|
|
||||||
|
// Create device with uppercase MAC address
|
||||||
|
$device = Device::factory()->create([
|
||||||
|
'mac_address' => 'A1:B2:C3:D4:E5:F6',
|
||||||
|
'api_key' => 'test-api-key',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Request with lowercase MAC address should still match
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'id' => 'a1:b2:c3:d4:e5:f6',
|
||||||
|
'access-token' => $device->api_key,
|
||||||
|
])->post('/api/screens', [
|
||||||
|
'image' => [
|
||||||
|
'content' => '<div>Test content</div>',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertOk();
|
||||||
|
Queue::assertPushed(GenerateScreenJob::class);
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue