Files
payments/ce/plugins/tink/complete_user_link.go
zeekay 7d727874d5 chore: rebrand Formance Payments -> Hanzo Payments (hanzo-fi)
- go.mod module path + all self-imports: github.com/formancehq/payments -> github.com/hanzo-fi/payments
  (1590 .go files / 3152 refs; 20 modules: root, pkg/client, pkg/domain, 18 ce/plugins).
  External dep kept: formancehq/go-libs.
- plugins: each ce/plugins/* consumed a *published* pkg/domain; added local
  'replace .../pkg/domain => ../../../pkg/domain' (consistent with root) so every
  standalone plugin module builds in-monorepo. compile-plugin.sh MOD var rebranded too.
- branding: README, LICENSE (dual MIT+EE; upstream copyright retained + Hanzo added),
  goreleaser (release images -> registry.hanzo.ai/hanzo-fi, OCI labels, homepage),
  proto go_package, renovate, speakeasy gen.yaml, connector-template *.gotpl.
- CI: removed Numary/Formance workflows + composite action; added canonical hanzo.yml +
  cicd.yml (hanzoai/ci, gated HANZO_CI_ENABLED) + secretless ci.yml (go build/vet).
- kept as data/compat keys: wire capability enums ALLOW_FORMANCE_*, generated SDK type
  client.Formance, domain field FormanceRedirectURL, OTEL_SERVICE_NAME, upstream base image.
- verified: go build ./... (default + -tags ee) + all 20 nested modules green.
2026-07-05 13:08:55 -07:00

93 lines
2.6 KiB
Go

package tink
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"time"
"github.com/hanzo-fi/payments/pkg/domain/models"
)
const (
CredentialIDQueryParamID = "credentials_id"
StateQueryParamID = "state"
ErrorQueryParamID = "error"
ErrorMessageQueryParamID = "message"
)
func validateCompleteUserLinkRequest(req models.CompleteUserLinkRequest) error {
if req.RelatedAttempt == nil {
return fmt.Errorf("related attempt is required: %w", models.ErrInvalidRequest)
}
queryState, ok := req.HTTPCallInformation.QueryValues[StateQueryParamID]
if !ok || len(queryState) != 1 {
return fmt.Errorf("missing state: %w", models.ErrInvalidRequest)
}
decodedState, err := base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(queryState[0])
if err != nil {
return fmt.Errorf("failed to decode state: %w", err)
}
callbackState := models.CallbackState{}
if err := json.Unmarshal(decodedState, &callbackState); err != nil {
return fmt.Errorf("failed to unmarshal state: %w", err)
}
if callbackState.Randomized != req.RelatedAttempt.State.Randomized {
return fmt.Errorf("state mismatch: %w", models.ErrInvalidRequest)
}
_, ok = req.HTTPCallInformation.QueryValues[ErrorQueryParamID]
if ok {
return nil
}
_, ok = req.HTTPCallInformation.QueryValues[CredentialIDQueryParamID]
if !ok || len(req.HTTPCallInformation.QueryValues[CredentialIDQueryParamID]) != 1 ||
req.HTTPCallInformation.QueryValues[CredentialIDQueryParamID][0] == "" {
return fmt.Errorf("missing credential IDs: %w", models.ErrInvalidRequest)
}
return nil
}
func (p *Plugin) completeUserLink(_ context.Context, req models.CompleteUserLinkRequest) (models.CompleteUserLinkResponse, error) {
if err := validateCompleteUserLinkRequest(req); err != nil {
return models.CompleteUserLinkResponse{}, err
}
errorCode, ok := req.HTTPCallInformation.QueryValues[ErrorQueryParamID]
if ok {
errMessage := "got an error from tink"
if len(errorCode) > 0 {
errMessage += fmt.Sprintf(": %s", errorCode[0])
}
if len(req.HTTPCallInformation.QueryValues[ErrorMessageQueryParamID]) > 0 {
errMessage += fmt.Sprintf(": %s", req.HTTPCallInformation.QueryValues[ErrorMessageQueryParamID][0])
}
// Error callback
return models.CompleteUserLinkResponse{
Error: &models.UserLinkErrorResponse{
Error: errMessage,
},
}, nil
}
return models.CompleteUserLinkResponse{
Success: &models.UserLinkSuccessResponse{
Connections: []models.PSPOpenBankingConnection{
{
CreatedAt: time.Now().UTC(),
ConnectionID: req.HTTPCallInformation.QueryValues[CredentialIDQueryParamID][0],
},
},
},
}, nil
}