-
Notifications
You must be signed in to change notification settings - Fork 20
[Repo Assist] refactor(auth): move generateRandomAPIKey to internal/auth package #3163
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
lpcox
merged 1 commit into
main
from
repo-assist/refactor-generateRandomAPIKey-2026-04-04-ec0a67b5375d7bb4
Apr 4, 2026
+44
−29
Merged
Changes from all commits
Commits
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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,18 @@ | ||||||||||||||||||||||
| package auth | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import ( | ||||||||||||||||||||||
| "crypto/rand" | ||||||||||||||||||||||
| "encoding/hex" | ||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // 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 { | ||||||||||||||||||||||
| return "", fmt.Errorf("failed to generate random API key: %w", err) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return hex.EncodeToString(bytes), nil | ||||||||||||||||||||||
|
Comment on lines
+13
to
+17
|
||||||||||||||||||||||
| bytes := make([]byte, 32) | |
| if _, err := rand.Read(bytes); err != nil { | |
| return "", fmt.Errorf("failed to generate random API key: %w", err) | |
| } | |
| return hex.EncodeToString(bytes), nil | |
| keyBytes := make([]byte, 32) | |
| if _, err := rand.Read(keyBytes); err != nil { | |
| return "", fmt.Errorf("failed to generate random API key: %w", err) | |
| } | |
| return hex.EncodeToString(keyBytes), 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,24 @@ | ||
| package auth_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/github/gh-aw-mcpg/internal/auth" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestGenerateRandomAPIKey verifies that GenerateRandomAPIKey produces a | ||
| // non-empty, unique, hex-encoded string per spec §7.3. | ||
| func TestGenerateRandomAPIKey(t *testing.T) { | ||
| key, err := auth.GenerateRandomAPIKey() | ||
| require.NoError(t, err, "GenerateRandomAPIKey() should not fail") | ||
| assert.NotEmpty(t, key, "generated key should not be empty") | ||
| // 32 bytes encoded as hex = 64 characters | ||
| assert.Len(t, key, 64, "generated key should be 64 hex characters") | ||
|
|
||
| // Verify keys are unique across calls | ||
| key2, err := auth.GenerateRandomAPIKey() | ||
| require.NoError(t, err) | ||
| assert.NotEqual(t, key, key2, "successive calls should produce unique keys") | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GenerateRandomAPIKeywraps therand.Readerror with "failed to generate random API key", and the caller inrun()wraps again with the same message. If this error ever surfaces, it will read redundantly ("failed...: failed..."). Prefer returning the rawrand.Readerror fromGenerateRandomAPIKey(or using a more specific/sentinel error) and let callers add context once.