Skip to content

Commit 002c132

Browse files
authored
Merge pull request #17 from givebutter/fix/prefixed-key-with-compatibility-mode-on
2 parents fe26657 + e1b39c2 commit 002c132

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
"minimum-stability": "dev",
2222
"prefer-stable": true,
2323
"require": {
24-
"php": "^7.0|^8.0",
25-
"doctrine/dbal": "^3.7"
24+
"php": "^7.0|^8.0"
2625
},
2726
"autoload": {
2827
"psr-4": {

src/Models/ApiKey.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,32 @@ 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 (! str_contains($key, '|')) {
109+
return $query->where('key', $key)
110+
->orWhere('key', hash('sha256', $key));
111+
}
112+
113+
[$id, $key] = explode('|', $key, 2);
114+
115+
return $query
116+
->where(function (Builder $query) use ($key, $id) {
117+
return $query->where('key', $key)
118+
->where('id', $id);
119+
})
120+
->orWhere(function (Builder $query) use ($key, $id) {
121+
return $query->where('key', hash('sha256', $key))
122+
->where('id', $id);
123+
});
110124
});
111125
}
112126

113-
if (strpos($key, '|') === false) {
127+
if (! str_contains($key, '|')) {
114128
return $query->where('key', hash('sha256', $key));
115129
}
116130

117131
[$id, $key] = explode('|', $key, 2);
118132

119-
return $query->where('id', $id)->where('key', hash('sha256', $key));
133+
return $query->where('id', $id)
134+
->where('key', hash('sha256', $key));
120135
}
121136
}

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)