-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Oss 248 refactor y alphabet detectors #4293
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
base: main
Are you sure you want to change the base?
Oss 248 refactor y alphabet detectors #4293
Conversation
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.
Most of the code, looks good to me. I added few comments.
case http.StatusOK: | ||
return true, nil | ||
case http.StatusPaymentRequired: | ||
return true, fmt.Errorf("blocked due to billing issue/quota exceeded: %d", res.StatusCode) |
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.
If we're certain that the API returns this error when the key is valid but blocked due to billing issues or quota limits, then I don't think we should surface it as an error to the user. It's not a failure to verify the key itself.
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.
+1 to ^
We can consider it as a success iff we're certain that this is only due to billing issues.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"youneedabudget"}) + `\b([0-9a-f]{64})\b`) | ||
// YNAB Personal Access Tokens are 64-character hexadecimal strings | ||
// Based on documentation: access tokens are used with Bearer authentication | ||
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"youneedabudget", "ynab"}) + `["']?([A-Za-z0-9_-]{43,44})["']?`) |
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.
Are we sure the old pattern keys are no longer valid? Also, we don’t need ["']
in the regex - let’s just capture the exact pattern.
func verifyMatch(ctx context.Context, client *http.Client, token string) (bool, error) { | ||
// Try V2 API (legacy) as fallback | ||
isVerified, err := tryEndpoint(ctx, client, token, PROD_URL, "/users") | ||
if isVerified || (err != nil && !isAuthError(err)) { | ||
return isVerified, err | ||
} | ||
|
||
// Try V2 Staging as final fallback | ||
return tryEndpoint(ctx, client, token, STAGING_URL, "/users") | ||
} | ||
|
||
func tryEndpoint(ctx context.Context, client *http.Client, token, baseURL, endpoint string) (bool, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+endpoint, http.NoBody) | ||
if err != nil { |
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.
🚀
// 403: API key is valid but access is forbidden (quota exceeded, API not enabled, etc.) | ||
body, _ := io.ReadAll(res.Body) | ||
if strings.Contains(string(body), "quotaExceeded") { | ||
return true, fmt.Errorf("quota exceeded (valid key): %d", res.StatusCode) |
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.
If we're returning true, that means the key was successfully verified. So we shouldn't return a verification error in that case.
Checklist:
make test-community
)?make lint
this requires golangci-lint)?