Move Vercel /v2/user fetch to pkg/connector/vercel.go → Migrate connector settings callers to generic
- Move Vercel /v2/user fetch to pkg/connector/vercel.go - Authorize NeedsConfiguration and SelectedOrganization - Add coredata.ConnectorSettings[T] generic helper - Migrate connector settings callers to generic Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
This commit is contained in:
@@ -469,6 +469,10 @@ func (r *accessSourceResolver) ProviderOrganizations(ctx context.Context, obj *t
|
||||
|
||||
// NeedsConfiguration is the resolver for the needsConfiguration field.
|
||||
func (r *accessSourceResolver) NeedsConfiguration(ctx context.Context, obj *types.AccessSource) (bool, error) {
|
||||
if err := r.authorize(ctx, obj.ID, probo.ActionAccessSourceGet); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if obj.ConnectorID == nil {
|
||||
return false, nil
|
||||
}
|
||||
@@ -560,6 +564,10 @@ func (r *accessSourceResolver) ConnectionStatus(ctx context.Context, obj *types.
|
||||
|
||||
// SelectedOrganization is the resolver for the selectedOrganization field.
|
||||
func (r *accessSourceResolver) SelectedOrganization(ctx context.Context, obj *types.AccessSource) (*string, error) {
|
||||
if err := r.authorize(ctx, obj.ID, probo.ActionAccessSourceGet); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if obj.ConnectorID == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -18,14 +18,11 @@ package console_v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"go.gearno.de/kit/httpclient"
|
||||
"go.gearno.de/kit/httpserver"
|
||||
"go.gearno.de/kit/log"
|
||||
"go.probo.inc/probo/pkg/accessreview"
|
||||
@@ -242,7 +239,7 @@ func handleConnectorComplete(
|
||||
teamID := query.Get("team_id")
|
||||
if teamID == "" {
|
||||
if oauth2Conn, ok := connection.(*connector.OAuth2Connection); ok && oauth2Conn.AccessToken != "" {
|
||||
if uid, err := fetchVercelUserID(r.Context(), oauth2Conn.AccessToken); err == nil {
|
||||
if uid, err := connector.FetchVercelUserID(r.Context(), oauth2Conn.AccessToken); err == nil {
|
||||
teamID = uid
|
||||
} else {
|
||||
logger.WarnCtx(r.Context(), "cannot fetch vercel user id for personal-account fallback", log.Error(err))
|
||||
@@ -322,43 +319,6 @@ func handleConnectorOAuth2Error(
|
||||
safeRedirect.Redirect(w, r, parsedURL.String(), "/", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// fetchVercelUserID calls Vercel's /v2/user with the freshly-minted access
|
||||
// token to retrieve the user's UID. This is used as a synthetic TeamID
|
||||
// when the OAuth callback omits team_id (personal-account installs).
|
||||
func fetchVercelUserID(ctx context.Context, accessToken string) (string, error) {
|
||||
reqCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, "https://api.vercel.com/v2/user", nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot create vercel user request: %w", err)
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
|
||||
client := httpclient.DefaultClient(httpclient.WithSSRFProtection())
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot execute vercel user request: %w", err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return "", fmt.Errorf("cannot fetch vercel user: unexpected status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var body struct {
|
||||
User struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"user"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
||||
return "", fmt.Errorf("cannot decode vercel user response: %w", err)
|
||||
}
|
||||
|
||||
return body.User.ID, nil
|
||||
}
|
||||
|
||||
// isValidPagerDutySubdomain reports whether s is a single DNS label
|
||||
// (RFC 1035 §2.3.1). PagerDuty subdomains are tenant identifiers that
|
||||
// will be embedded in API URLs; the OAuth callback is the only place
|
||||
|
||||
@@ -47,7 +47,7 @@ func NewSlackConnection(c *coredata.Connector) *SlackConnection {
|
||||
}
|
||||
|
||||
// Extract channel information from typed settings
|
||||
settings, _ := c.SlackSettings()
|
||||
settings, _ := coredata.ConnectorSettings[coredata.SlackConnectorSettings](c)
|
||||
if settings.Channel != "" {
|
||||
conn.Channel = &settings.Channel
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user