diff --git a/pkg/probod/connector_config.go b/pkg/probod/connector_config.go new file mode 100644 index 000000000..dabd80985 --- /dev/null +++ b/pkg/probod/connector_config.go @@ -0,0 +1,75 @@ +// Copyright (c) 2025 Probo Inc . +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +package probod + +import ( + "encoding/json" + "fmt" + + "github.com/getprobo/probo/pkg/connector" +) + +type ( + connectorConfig struct { + Name string `json:"name"` + Type string `json:"type"` + Config connector.Connector `json:"-"` + } + + connectorOAuth2Config struct { + ClientID string `json:"client-id"` + ClientSecret string `json:"client-secret"` + RedirectURI string `json:"redirect-uri"` + Scopes []string `json:"scopes"` + AuthURL string `json:"auth-url"` + TokenURL string `json:"token-url"` + } +) + +func (c *connectorConfig) UnmarshalJSON(data []byte) error { + var tmp struct { + Name string `json:"name"` + Type string `json:"type"` + RawConfig json.RawMessage `json:"config"` + } + + if err := json.Unmarshal(data, &tmp); err != nil { + return fmt.Errorf("cannot unmarshal connector config: %w", err) + } + + c.Name = tmp.Name + c.Type = tmp.Type + + switch tmp.Type { + case "oauth2": + var cfg connectorOAuth2Config + if err := json.Unmarshal(tmp.RawConfig, &cfg); err != nil { + return fmt.Errorf("cannot unmarshal oauth2 config: %w", err) + } + + c.Config = &connector.OAuth2Connector{ + ClientID: cfg.ClientID, + ClientSecret: cfg.ClientSecret, + RedirectURI: cfg.RedirectURI, + Scopes: cfg.Scopes, + AuthURL: cfg.AuthURL, + TokenURL: cfg.TokenURL, + } + default: + return fmt.Errorf("unknown %q connector type: %s", tmp.Name, tmp.Type) + } + + return nil +} diff --git a/pkg/probod/probod.go b/pkg/probod/probod.go index b655dc79c..c47a89dee 100644 --- a/pkg/probod/probod.go +++ b/pkg/probod/probod.go @@ -25,6 +25,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/getprobo/probo/pkg/awsconfig" + "github.com/getprobo/probo/pkg/connector" "github.com/getprobo/probo/pkg/coredata" "github.com/getprobo/probo/pkg/crypto/passwdhash" "github.com/getprobo/probo/pkg/mailer" @@ -48,12 +49,13 @@ type ( } config struct { - Hostname string `json:"hostname"` - Pg pgConfig `json:"pg"` - Api apiConfig `json:"api"` - Auth authConfig `json:"auth"` - AWS awsConfig `json:"aws"` - Mailer mailerConfig `json:"mailer"` + Hostname string `json:"hostname"` + Pg pgConfig `json:"pg"` + Api apiConfig `json:"api"` + Auth authConfig `json:"auth"` + AWS awsConfig `json:"aws"` + Mailer mailerConfig `json:"mailer"` + Connectors []connectorConfig `json:"connectors"` } ) @@ -174,6 +176,11 @@ func (impl *Implm) Run( return fmt.Errorf("cannot create hashing profile: %w", err) } + defaultConnectorRegistry := connector.NewConnectorRegistry() + for _, connector := range impl.cfg.Connectors { + defaultConnectorRegistry.Register(connector.Name, connector.Config) + } + usrmgrService, err := usrmgr.NewService( ctx, pgClient, @@ -193,9 +200,10 @@ func (impl *Implm) Run( serverHandler, err := server.NewServer( server.Config{ - AllowedOrigins: impl.cfg.Api.Cors.AllowedOrigins, - Probo: proboService, - Usrmgr: usrmgrService, + AllowedOrigins: impl.cfg.Api.Cors.AllowedOrigins, + Probo: proboService, + Usrmgr: usrmgrService, + ConnectorRegistry: defaultConnectorRegistry, Auth: console_v1.AuthConfig{ CookieName: impl.cfg.Auth.Cookie.Name, CookieDomain: impl.cfg.Auth.Cookie.Domain,