| @@ -2,6 +2,7 @@ package v2 |
| 2 | | 2 | |
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| | 5 | +added: "encoding/json" |
| 5 | "fmt" | 6 | "fmt" |
| 6 | "io" | 7 | "io" |
| 7 | "net/http" | 8 | "net/http" |
| @@ -31,6 +32,10 @@ var ( |
| 31 | keyPat = regexp.MustCompile(`\b(pat-(?:eu|na)1-[A-Za-z0-9]{8}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{12})\b`) | 32 | keyPat = regexp.MustCompile(`\b(pat-(?:eu|na)1-[A-Za-z0-9]{8}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{12})\b`) |
| 32 | ) | 33 | ) |
| 33 | | 34 | |
| | 35 | +added: type hubspotErrorResponse struct { |
| | 36 | +added: Category string `json:"category"` |
| | 37 | +added: } |
| | 38 | +added: |
| 34 | // Keywords are used for efficiently pre-filtering chunks. | 39 | // Keywords are used for efficiently pre-filtering chunks. |
| 35 | // Use identifiers in the secret preferably, or the provider name. | 40 | // Use identifiers in the secret preferably, or the provider name. |
| 36 | func (s Scanner) Keywords() []string { | 41 | func (s Scanner) Keywords() []string { |
| @@ -87,11 +92,23 @@ func verifyToken(ctx context.Context, client *http.Client, token string) (bool, |
| 87 | | 92 | |
| 88 | switch res.StatusCode { | 93 | switch res.StatusCode { |
| 89 | case http.StatusOK: | 94 | case http.StatusOK: |
| | 95 | +added: body, err := io.ReadAll(res.Body) |
| | 96 | +added: if err != nil { |
| | 97 | +added: return false, err |
| | 98 | +added: } |
| | 99 | +added: if !json.Valid(body) { |
| | 100 | +added: return false, fmt.Errorf("unexpected response body for HTTP 200") |
| | 101 | +added: } |
| 90 | return true, nil | 102 | return true, nil |
| 91 | case http.StatusUnauthorized: | 103 | case http.StatusUnauthorized: |
| 92 | return false, nil | 104 | return false, nil |
| 93 | case http.StatusForbidden: | 105 | case http.StatusForbidden: |
| 94 | −removed: // The token is valid but lacks permission for the endpoint. | 106 | +added: // Only HubSpot's own scope error proves the token authenticated; a 403 from anything |
| | 107 | +added: // else (e.g. a proxy) says nothing about the token. |
| | 108 | +added: var errResp hubspotErrorResponse |
| | 109 | +added: if err := json.NewDecoder(res.Body).Decode(&errResp); err != nil || errResp.Category != "MISSING_SCOPES" { |
| | 110 | +added: return false, fmt.Errorf("unexpected 403 response, unable to verify token") |
| | 111 | +added: } |
| 95 | return true, nil | 112 | return true, nil |
| 96 | default: | 113 | default: |
| 97 | return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) | 114 | return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) |