-
Notifications
You must be signed in to change notification settings - Fork 41
Handle basic auth without token validation #26
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package lib | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGetRequestRoutingInfoBasicAuth(t *testing.T) { | ||
| manager := &QueueManager{} | ||
|
germanoeich marked this conversation as resolved.
|
||
| req, err := http.NewRequest("POST", "https://discord.com/api/v10/oauth2/token/revoke", nil) | ||
| if err != nil { | ||
| t.Fatalf("failed to create request: %v", err) | ||
| } | ||
|
|
||
| hash, path, queueType := manager.GetRequestRoutingInfo(req, "Basic ZmFrZVRva2Vu") | ||
| if queueType != NoAuth { | ||
| t.Fatalf("expected queue type %v, got %v", NoAuth, queueType) | ||
| } | ||
|
|
||
| if hash != HashCRC64(path) { | ||
| t.Fatalf("expected routing hash to match path hash") | ||
| } | ||
|
germanoeich marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func TestGetRequestRoutingInfoBearerAuth(t *testing.T) { | ||
| manager := &QueueManager{} | ||
| req, err := http.NewRequest("GET", "https://discord.com/api/v10/users/@me", nil) | ||
| if err != nil { | ||
| t.Fatalf("failed to create request: %v", err) | ||
| } | ||
|
|
||
| hash, _, queueType := manager.GetRequestRoutingInfo(req, "Bearer some-token") | ||
| if queueType != Bearer { | ||
| t.Fatalf("expected queue type %v, got %v", Bearer, queueType) | ||
| } | ||
|
|
||
| if hash != HashCRC64("Bearer some-token") { | ||
| t.Fatalf("expected bearer routing hash to use token") | ||
| } | ||
|
germanoeich marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func TestGetRequestRoutingInfoBotToken(t *testing.T) { | ||
| manager := &QueueManager{} | ||
| req, err := http.NewRequest("GET", "https://discord.com/api/v10/channels/123/messages", nil) | ||
| if err != nil { | ||
| t.Fatalf("failed to create request: %v", err) | ||
| } | ||
|
|
||
| hash, path, queueType := manager.GetRequestRoutingInfo(req, "Bot Abc") | ||
| if queueType != Bot { | ||
| t.Fatalf("expected queue type %v, got %v", Bot, queueType) | ||
| } | ||
|
|
||
| if hash != HashCRC64(path) { | ||
| t.Fatalf("expected bot routing hash to match path hash") | ||
| } | ||
|
germanoeich marked this conversation as resolved.
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,22 +6,25 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const knownData = "test data" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Calculated using ISO table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const knownHash = 10232006911339297906 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func TestHashWorks(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HashCRC64(knownData) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if HashCRC64(knownData) != knownHash { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Fatalf("Invalid hash returned") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //Test for correctness | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test for correctness | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func TestHashIsConsistent(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ret := HashCRC64(knownData) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ret != knownHash { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Errorf("Invalid hash returned") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //Test for consistency when function is used for other data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test for consistency when function is used for other data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func TestHashIsConsistentAcrossMultipleRuns(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i := 0; i < 50000; i++ { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HashCRC64(strconv.Itoa(i)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -33,3 +36,25 @@ func TestHashIsConsistentAcrossMultipleRuns(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func TestHasAuthPrefix(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tests := []struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| token string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scheme string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {name: "basic with space", token: "Basic Zm9vOmJhcg==", scheme: "Basic", expected: true}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {name: "basic lowercase", token: "basic Zm9vOmJhcg==", scheme: "Basic", expected: true}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {name: "basic with tab", token: "Basic\tZm9vOmJhcg==", scheme: "Basic", expected: true}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {name: "basic only scheme", token: "Basic ", scheme: "Basic", expected: true}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {name: "missing space", token: "BasicZm9v", scheme: "Basic", expected: false}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {name: "different scheme", token: "Bot foo", scheme: "Basic", expected: false}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {name: "bearer", token: "Bearer token", scheme: "Bearer", expected: true}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+39
to
+53
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick Add a few more edge cases to lock in prefix semantics. Recommend covering empty token, no delimiter, double‑space, and tab‑only delimiter for both Basic and Bearer. tests := []struct {
name string
token string
scheme string
expected bool
}{
{name: "basic with space", token: "Basic Zm9vOmJhcg==", scheme: "Basic", expected: true},
{name: "basic lowercase", token: "basic Zm9vOmJhcg==", scheme: "Basic", expected: true},
{name: "basic with tab", token: "Basic\tZm9vOmJhcg==", scheme: "Basic", expected: true},
{name: "basic only scheme", token: "Basic ", scheme: "Basic", expected: true},
+ {name: "basic only scheme with tab", token: "Basic\t", scheme: "Basic", expected: true},
+ {name: "basic without delimiter", token: "Basic", scheme: "Basic", expected: false},
+ {name: "basic with double space", token: "Basic Zm9v", scheme: "Basic", expected: true},
+ {name: "empty token", token: "", scheme: "Basic", expected: false},
{name: "missing space", token: "BasicZm9v", scheme: "Basic", expected: false},
{name: "different scheme", token: "Bot foo", scheme: "Basic", expected: false},
{name: "bearer", token: "Bearer token", scheme: "Bearer", expected: true},
+ {name: "bearer with tab", token: "Bearer\ttoken", scheme: "Bearer", expected: true},
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, tc := range tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if result := HasAuthPrefix(tc.token, tc.scheme); result != tc.expected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Errorf("%s: expected %v, got %v", tc.name, tc.expected, result) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+55
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick Use subtests for clearer reporting. Subtests make failures easier to pinpoint and enable parallelism if needed. - for _, tc := range tests {
- if result := HasAuthPrefix(tc.token, tc.scheme); result != tc.expected {
- t.Errorf("%s: expected %v, got %v", tc.name, tc.expected, result)
- }
- }
+ for _, tc := range tests {
+ tc := tc
+ t.Run(tc.name, func(t *testing.T) {
+ if result := HasAuthPrefix(tc.token, tc.scheme); result != tc.expected {
+ t.Fatalf("expected %v, got %v (token=%q, scheme=%q)", tc.expected, result, tc.token, tc.scheme)
+ }
+ })
+ }🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.