Skip to content

Commit 22a752d

Browse files
committed
Fix scopeOfKey when compatibility mode is on and keys are prefixed with ID
1 parent ce10b5e commit 22a752d

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/Models/ApiKey.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,46 @@ public function scopeOfKey(Builder $query, string $key): Builder
105105

106106
if ($compatibilityMode) {
107107
return $query->where(function (Builder $query) use ($key) {
108-
return $query->where('key', $key)
109-
->orWhere('key', hash('sha256', $key));
108+
if ($this->isMissingId($key)) {
109+
return $query->where('key', $key)
110+
->orWhere('key', hash('sha256', $key));
111+
}
112+
113+
$id = $this->extractId($key);
114+
$key = $this->extractKey($key);
115+
116+
return $query
117+
->where(function (Builder $query) use ($key, $id) {
118+
return $query->where('key', $key)
119+
->where('id', $id);
120+
})
121+
->orWhere(function (Builder $query) use ($key, $id) {
122+
return $query->where('key', hash('sha256', $key))
123+
->where('id', $id);
124+
});
110125
});
111126
}
112127

113-
if (strpos($key, '|') === false) {
128+
if ($this->isMissingId($key)) {
114129
return $query->where('key', hash('sha256', $key));
115130
}
116131

117-
[$id, $key] = explode('|', $key, 2);
132+
return $query->where('id', $this->extractId($key))
133+
->where('key', hash('sha256', $this->extractKey($key)));
134+
}
135+
136+
private function isMissingId(string $key): bool
137+
{
138+
return strpos($key, '|') === false;
139+
}
118140

119-
return $query->where('id', $id)->where('key', hash('sha256', $key));
141+
private function extractId(string $key): string
142+
{
143+
return explode('|', $key, 2)[0];
144+
}
145+
146+
private function extractKey(string $key): string
147+
{
148+
return explode('|', $key, 2)[1];
120149
}
121150
}

tests/Feature/CompatibilityMode.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,24 @@ public function accepts_both_hashed_and_non_hashed_api_keys_when_compatibility_m
7171
'key' => $apiKey2->fresh()->key,
7272
]);
7373

74-
// Assert the non hashed api keys works
74+
// Assert that non hashed api keys works
7575
$this->withHeaders([
76-
'Authorization' => 'Bearer ' . $plainTextApiKey1,
76+
'Authorization' => "Bearer {$plainTextApiKey1}",
7777
])->get("/api/posts/{$post->id}")->assertOk();
7878

79-
// Assert the hashed api keys works
79+
// Assert that non hashed api keys with ID prefix works
8080
$this->withHeaders([
81-
'Authorization' => 'Bearer ' . $plainTextApiKey2,
81+
'Authorization' => "Bearer {$apiKey1->id}|{$plainTextApiKey1}",
82+
])->get("/api/posts/{$post->id}")->assertOk();
83+
84+
// Assert that hashed api keys works
85+
$this->withHeaders([
86+
'Authorization' => "Bearer {$plainTextApiKey2}",
87+
])->get("/api/posts/{$post->id}")->assertOk();
88+
89+
// Assert that hashed api keys with ID prefix works
90+
$this->withHeaders([
91+
'Authorization' => "Bearer {$apiKey2->id}|{$plainTextApiKey2}",
8292
])->get("/api/posts/{$post->id}")->assertOk();
8393
}
8494
}

0 commit comments

Comments
 (0)