13 Commits

Author SHA1 Message Date
Head of Product & Engineering
65c1754bab fix: pass appId to /oauth/installedLocations request
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
The endpoint requires appId (GHL client ID) as a query parameter.
Without it the API returns 422 "appId must be a string".

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-06 11:09:28 +02:00
Head of Product & Engineering
59b0a8c93f fix: switch location lookup to /oauth/installedLocations (oauth.readonly scope)
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
/locations/search requires locations.readonly which GHL never includes in
company-level OAuth tokens. /oauth/installedLocations uses oauth.readonly,
which is always present in company tokens, and returns only locations where
this app is actually installed.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-06 10:57:16 +02:00
Head of Product & Engineering
3863e8f0cd fix: use installedLocations from bulk token response instead of /locations/search
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
GHL includes installedLocations[] in the company-level token response for
bulk installs. Use those IDs directly to avoid calling /locations/search,
which requires locations.readonly scope that GHL doesn't grant. Falls back
to /locations/search only when the list is absent. Also adds raw_body and
installed_locations fields to token response debug logging.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-06 10:47:40 +02:00
Head of Product & Engineering
3d1e80cd86 fix: add locations.readonly scope to OAuth install request
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
The /locations/search endpoint requires locations.readonly scope.
Without it the company token gets 401 when trying to list locations
during bulk install, blocking the per-location token exchange.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-06 10:17:45 +02:00
Head of Product & Engineering
dfbc40e201 fix: use html/template for success page to satisfy semgrep XSS rules
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Replaces fmt.Fprintf(w, ..., installed) with html/template.Execute to
avoid semgrep no-fprintf-to-responsewriter and raw-html-format findings.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-06 10:11:14 +02:00
Head of Product & Engineering
f97f31c8ac fix: exchange company token for per-location tokens on bulk OAuth install
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
GHL issues a Company-scoped token (userType=Company) for bulk/agency
installs even when Target User=Sub-account. This fix handles that case:

1. Detect userType=Company in HandleCallback
2. Call GET /locations/search to enumerate all company locations
3. For each location call POST /oauth/locationToken to get a
   Location-scoped token (userType=Location, includes locationId)
4. Store each location token individually in MongoDB

This allows webhook delivery and status updates to work per-location
without requiring the agency admin to re-install per sub-account.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-06 10:02:42 +02:00
Head of Product & Engineering
f01138474d debug: log full token response body and callback query params
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Temporary diagnostic logging to determine what GHL includes in the
token exchange response and the OAuth callback redirect URL when
user_type is Company (Sub-account target app, agency-level install).

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-06 09:54:41 +02:00
Head of Product & Engineering
ad2682c55d fix: guard against empty locationId in OAuth callback and log token fields
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
GHL returns an empty locationId when the user authorizes at agency level
instead of selecting a specific sub-account location. Without this guard
the token was silently stored under an empty key, making every subsequent
webhook fail with "no token for location".

Also logs location_id/company_id/user_type from the token response to
make future OAuth install debugging easier.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-06 01:15:27 +02:00
Head of Product & Engineering
6d3c9c071f fix: suppress remaining errcheck failures in test and oauth code
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
- internal/ghl/oauth.go:186: defer func(){ _ = resp.Body.Close() }()
- internal/cast/client_test.go: prefix all json.Decode/Encode calls with _ =
- internal/ghl/oauth_test.go: _ = r.ParseForm(), _, _ = w.Write(...)

golangci-lint exclusion rules in v2 are not suppressing test file errcheck
as expected, so fixes are applied directly in source.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-05 22:33:35 +02:00
Head of Product & Engineering
12c547d215 fix: address remaining golangci-lint warnings
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
- internal/ghl/oauth.go: acknowledge fmt.Fprint return (errcheck)
- internal/ghl/api.go: handle io.ReadAll error instead of discarding (errcheck)
- internal/cast/client.go: replace defer-in-loop with explicit Body.Close
  after ReadAll (gocritic defer-in-loop)
- internal/phone/normalize.go: move inline regexp.MustCompile to package-level
  var e164Pattern (gocritic / performance)

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-05 21:33:03 +02:00
Head of Product & Engineering
dcf1e3070e test: expand test coverage — uninstall, dedup, 401, token refresh
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
- cast/client_test: add TestSendSMS_Unauthorized (401 → CastAPIError)
- ghl/webhook_test: add duplicate messageId, 450-char message, HandleUninstall (valid/invalid sig)
- ghl/oauth_test: add GetValidToken auto-refresh and refresh-failure tests
- ghl/oauth: make tokenURL a struct field (default ghlTokenURL) so tests can inject a mock endpoint

Co-Authored-By: SideKx <sidekx.ai@sds.dev>
2026-04-05 00:36:45 +02:00
Head of Product & Engineering
d081875fce fix: add uninstall handler, idempotency guard, and OAuth error handling
GHL Marketplace submission blockers resolved:
- Add POST /api/ghl/v1/webhook/uninstall to delete token on app removal
- Add in-memory messageId deduplication (10-min TTL) to prevent duplicate SMS sends on webhook retries
- Handle ?error= param in OAuth callback for user-denied auth flows
- Pass store to WebhookHandler; update tests accordingly

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-04 17:52:09 +02:00
Head of Product & Engineering
a40a4aa626 feat: initial implementation of Cast GHL Provider
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Complete MVP implementation of the Cast GHL Conversation Provider bridge:
- Go module setup with chi router and mongo-driver dependencies
- Config loading with env var validation and defaults
- MongoDB token store with upsert, get, update, delete operations
- Cast.ph SMS client with 429 retry logic and typed errors
- Phone number normalization (E.164 ↔ Philippine local format)
- GHL OAuth 2.0 install/callback/refresh flow
- GHL webhook handler with ECDSA signature verification (async dispatch)
- GHL API client for message status updates and inbound message stubs
- Multi-stage Dockerfile, docker-compose with MongoDB, Woodpecker CI pipeline
- Unit tests for phone normalization, Cast client, GHL webhook, and OAuth handlers

Co-Authored-By: SideKx <sidekx.ai@sds.dev>
2026-04-04 17:27:05 +02:00