Add connector config support
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
75
pkg/probod/connector_config.go
Normal file
75
pkg/probod/connector_config.go
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
}
|
||||||
@@ -25,6 +25,7 @@ import (
|
|||||||
|
|
||||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||||
"github.com/getprobo/probo/pkg/awsconfig"
|
"github.com/getprobo/probo/pkg/awsconfig"
|
||||||
|
"github.com/getprobo/probo/pkg/connector"
|
||||||
"github.com/getprobo/probo/pkg/coredata"
|
"github.com/getprobo/probo/pkg/coredata"
|
||||||
"github.com/getprobo/probo/pkg/crypto/passwdhash"
|
"github.com/getprobo/probo/pkg/crypto/passwdhash"
|
||||||
"github.com/getprobo/probo/pkg/mailer"
|
"github.com/getprobo/probo/pkg/mailer"
|
||||||
@@ -48,12 +49,13 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
config struct {
|
config struct {
|
||||||
Hostname string `json:"hostname"`
|
Hostname string `json:"hostname"`
|
||||||
Pg pgConfig `json:"pg"`
|
Pg pgConfig `json:"pg"`
|
||||||
Api apiConfig `json:"api"`
|
Api apiConfig `json:"api"`
|
||||||
Auth authConfig `json:"auth"`
|
Auth authConfig `json:"auth"`
|
||||||
AWS awsConfig `json:"aws"`
|
AWS awsConfig `json:"aws"`
|
||||||
Mailer mailerConfig `json:"mailer"`
|
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)
|
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(
|
usrmgrService, err := usrmgr.NewService(
|
||||||
ctx,
|
ctx,
|
||||||
pgClient,
|
pgClient,
|
||||||
@@ -193,9 +200,10 @@ func (impl *Implm) Run(
|
|||||||
|
|
||||||
serverHandler, err := server.NewServer(
|
serverHandler, err := server.NewServer(
|
||||||
server.Config{
|
server.Config{
|
||||||
AllowedOrigins: impl.cfg.Api.Cors.AllowedOrigins,
|
AllowedOrigins: impl.cfg.Api.Cors.AllowedOrigins,
|
||||||
Probo: proboService,
|
Probo: proboService,
|
||||||
Usrmgr: usrmgrService,
|
Usrmgr: usrmgrService,
|
||||||
|
ConnectorRegistry: defaultConnectorRegistry,
|
||||||
Auth: console_v1.AuthConfig{
|
Auth: console_v1.AuthConfig{
|
||||||
CookieName: impl.cfg.Auth.Cookie.Name,
|
CookieName: impl.cfg.Auth.Cookie.Name,
|
||||||
CookieDomain: impl.cfg.Auth.Cookie.Domain,
|
CookieDomain: impl.cfg.Auth.Cookie.Domain,
|
||||||
|
|||||||
Reference in New Issue
Block a user