Skip to content

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

SyedAliHamad
Copy link
Contributor

@SyedAliHamad SyedAliHamad commented Jul 7, 2025

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

@SyedAliHamad SyedAliHamad marked this pull request as ready for review July 7, 2025 21:53
@SyedAliHamad SyedAliHamad requested a review from a team as a code owner July 7, 2025 21:53
Copy link
Contributor

@kashifkhan0771 kashifkhan0771 left a 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)
Copy link
Contributor

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.

Copy link
Contributor

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})["']?`)
Copy link
Contributor

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.

Comment on lines +65 to +78
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 {
Copy link
Contributor

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)
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants