-
Notifications
You must be signed in to change notification settings - Fork 21
Deduplicate auth scheme parsing and random hex generation #3450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,18 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "encoding/hex" | ||
| "fmt" | ||
|
|
||
| "github.com/github/gh-aw-mcpg/internal/strutil" | ||
| ) | ||
|
|
||
| // GenerateRandomAPIKey generates a cryptographically random API key. | ||
| // Per spec §7.3, the gateway SHOULD generate a random API key on startup | ||
| // if none is provided. Returns a 32-byte hex-encoded string (64 chars). | ||
| func GenerateRandomAPIKey() (string, error) { | ||
| bytes := make([]byte, 32) | ||
| if _, err := rand.Read(bytes); err != nil { | ||
| key, err := strutil.RandomHex(32) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to generate random API key: %w", err) | ||
| } | ||
| return hex.EncodeToString(bytes), nil | ||
| return key, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package strutil | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "encoding/hex" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // RandomHex returns a hex-encoded string of n cryptographically random bytes. | ||
| // The returned string has length 2*n. | ||
| func RandomHex(n int) (string, error) { | ||
lpcox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| b := make([]byte, n) | ||
| if _, err := rand.Read(b); err != nil { | ||
| return "", fmt.Errorf("failed to generate %d random bytes: %w", n, err) | ||
| } | ||
| return hex.EncodeToString(b), nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package strutil | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRandomHex(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| n int | ||
| wantLen int | ||
| }{ | ||
| { | ||
| name: "16 bytes produces 32 hex chars", | ||
| n: 16, | ||
| wantLen: 32, | ||
| }, | ||
| { | ||
| name: "32 bytes produces 64 hex chars", | ||
| n: 32, | ||
| wantLen: 64, | ||
| }, | ||
| { | ||
| name: "1 byte produces 2 hex chars", | ||
| n: 1, | ||
| wantLen: 2, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result, err := RandomHex(tt.n) | ||
| require.NoError(t, err) | ||
| assert.Len(t, result, tt.wantLen) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRandomHex_Uniqueness(t *testing.T) { | ||
| seen := make(map[string]bool) | ||
| for i := 0; i < 100; i++ { | ||
| id, err := RandomHex(16) | ||
| require.NoError(t, err) | ||
| assert.NotEmpty(t, id) | ||
| assert.False(t, seen[id], "RandomHex should produce unique values") | ||
| seen[id] = true | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.