Files
probo/pkg/server/api/console/v1/connector_resolvers.go
Aurélien Sibiril 37121e7bac Verify Crisp website ownership before connecting
Crisp is a managed (Model B) connector: Probo holds one plugin token
server-side and each connection carries only a Website ID. Nothing
stops one organization from entering another organization's Website
ID, so prove control of the website before creating the connection.

Probo derives a per-(organization, website) verification code as an
HMAC over the token secret and exposes it through a new
crispVerificationCode query. The customer pastes it into the Probo
plugin's per-website settings; at connect time the resolver reads the
setting back through the managed plugin token and requires a
constant-time match before any row is written. The managed key and
plugin ID come from bootstrap, so the connector stays hidden until the
deployment configures them.

The settings fetch is injected so the create-time gate's branch wiring
is unit-tested (mismatch and not-subscribed reject, internal errors
stay generic, a matching code passes), and the managed-versus-client
key resolution is covered too.

Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
2026-07-11 16:50:32 +02:00

182 lines
5.8 KiB
Go

package console_v1
// This file will be automatically regenerated based on the schema, any resolver
// implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen version v0.17.93
import (
"context"
"errors"
"fmt"
"go.gearno.de/kit/log"
"go.probo.inc/probo/pkg/connector"
"go.probo.inc/probo/pkg/coredata"
"go.probo.inc/probo/pkg/probo"
"go.probo.inc/probo/pkg/server/api/console/v1/schema"
"go.probo.inc/probo/pkg/server/api/console/v1/types"
"go.probo.inc/probo/pkg/server/gqlutils"
)
// Oauth2Scopes is the resolver for the oauth2Scopes field.
func (r *connectorResolver) Oauth2Scopes(ctx context.Context, obj *types.Connector) ([]string, error) {
scopes := r.providerRegistry.ProviderOAuth2Scopes(obj.Provider)
if scopes == nil {
return []string{}, nil
}
return scopes, nil
}
// CreateAPIKeyConnector is the resolver for the createAPIKeyConnector field.
func (r *mutationResolver) CreateAPIKeyConnector(ctx context.Context, input types.CreateAPIKeyConnectorInput) (*types.CreateAPIKeyConnectorPayload, error) {
scope, err := r.authorize(ctx, input.OrganizationID, probo.ActionConnectorCreate)
if err != nil {
return nil, err
}
apiKey, err := r.resolveAPIKeyConnectorCredential(input.Provider, input.APIKey)
if err != nil {
return nil, gqlutils.Invalid(ctx, err)
}
req := probo.CreateConnectorRequest{
OrganizationID: input.OrganizationID,
Provider: input.Provider,
Protocol: coredata.ConnectorProtocolAPIKey,
Connection: r.newAPIKeyConnection(input.Provider, apiKey),
}
raw, err := apiKeyConnectorSettings(input)
if err != nil {
return nil, gqlutils.Invalid(ctx, err)
}
req.RawSettings = raw
// Crisp (ManagedAPIKey) requires proof the organization controls the Crisp
// website before the connection is created; every other API-key provider is
// unaffected. Runs after settings validation and before any write, so a
// failed check leaves no row.
if input.Provider == coredata.ConnectorProviderCrisp {
if err := r.verifyCrispOwnership(ctx, input); err != nil {
return nil, err
}
}
cnnctr, err := r.probo.Connectors.Create(ctx, scope, req)
if err != nil {
if errors.Is(err, coredata.ErrResourceAlreadyExists) {
return nil, gqlutils.Conflict(ctx, err)
}
r.logger.ErrorCtx(ctx, "cannot create API key connector", log.Error(err))
return nil, gqlutils.Internal(ctx)
}
return &types.CreateAPIKeyConnectorPayload{
Connector: types.NewConnector(cnnctr),
}, nil
}
// CreateClientCredentialsConnector is the resolver for the createClientCredentialsConnector field.
func (r *mutationResolver) CreateClientCredentialsConnector(ctx context.Context, input types.CreateClientCredentialsConnectorInput) (*types.CreateClientCredentialsConnectorPayload, error) {
scope, err := r.authorize(ctx, input.OrganizationID, probo.ActionConnectorCreate)
if err != nil {
return nil, err
}
oauth2Conn := &connector.OAuth2Connection{
GrantType: connector.OAuth2GrantTypeClientCredentials,
ClientID: input.ClientID,
ClientSecret: input.ClientSecret,
TokenURL: input.TokenURL,
}
if input.Scope != nil {
oauth2Conn.Scope = *input.Scope
}
req := probo.CreateConnectorRequest{
OrganizationID: input.OrganizationID,
Provider: input.Provider,
Protocol: coredata.ConnectorProtocolOAuth2,
Connection: oauth2Conn,
}
raw, err := clientCredentialsConnectorSettings(input)
if err != nil {
return nil, gqlutils.Invalid(ctx, err)
}
req.RawSettings = raw
cnnctr, err := r.probo.Connectors.Create(ctx, scope, req)
if err != nil {
if errors.Is(err, coredata.ErrResourceAlreadyExists) {
return nil, gqlutils.Conflict(ctx, err)
}
r.logger.ErrorCtx(ctx, "cannot create client credentials connector", log.Error(err))
return nil, gqlutils.Internal(ctx)
}
return &types.CreateClientCredentialsConnectorPayload{
Connector: types.NewConnector(cnnctr),
}, nil
}
// DeleteConnector is the resolver for the deleteConnector field.
func (r *mutationResolver) DeleteConnector(ctx context.Context, input types.DeleteConnectorInput) (*types.DeleteConnectorPayload, error) {
scope, err := r.authorize(ctx, input.ConnectorID, probo.ActionConnectorDelete)
if err != nil {
return nil, err
}
if err := r.probo.Connectors.Delete(ctx, scope, input.ConnectorID); err != nil {
panic(fmt.Errorf("cannot delete connector: %w", err))
}
return &types.DeleteConnectorPayload{
DeletedConnectorID: input.ConnectorID,
}, nil
}
// DeleteSlackConnection is the resolver for the deleteSlackConnection field.
func (r *mutationResolver) DeleteSlackConnection(ctx context.Context, input types.DeleteSlackConnectionInput) (*types.DeleteSlackConnectionPayload, error) {
scope, err := r.authorize(ctx, input.SlackConnectionID, probo.ActionConnectorDelete)
if err != nil {
return nil, err
}
if err := r.probo.Connectors.Delete(ctx, scope, input.SlackConnectionID); err != nil {
r.logger.ErrorCtx(ctx, "cannot delete slack connection", log.Error(err))
return nil, gqlutils.Internal(ctx)
}
return &types.DeleteSlackConnectionPayload{
DeletedSlackConnectionID: input.SlackConnectionID,
}, nil
}
// Permission is the resolver for the permission field.
func (r *slackConnectionResolver) Permission(ctx context.Context, obj *types.SlackConnection, action string) (bool, error) {
return r.Resolver.Permission(ctx, obj, action)
}
// Connector returns schema.ConnectorResolver implementation.
func (r *Resolver) Connector() schema.ConnectorResolver { return &connectorResolver{r} }
// SlackConnection returns schema.SlackConnectionResolver implementation.
func (r *Resolver) SlackConnection() schema.SlackConnectionResolver {
return &slackConnectionResolver{r}
}
type (
connectorResolver struct{ *Resolver }
slackConnectionResolver struct{ *Resolver }
)