Add settins connector view
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -19,14 +19,16 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
)
|
||||
|
||||
type (
|
||||
ProtocolType string
|
||||
|
||||
Connector interface {
|
||||
Initiate(ctx context.Context, connectorID string, organizationID string, r *http.Request) (string, error)
|
||||
Complete(ctx context.Context, connectorID string, organizationID string, r *http.Request) (Connection, error)
|
||||
Initiate(ctx context.Context, connectorID string, organizationID gid.GID, r *http.Request) (string, error)
|
||||
Complete(ctx context.Context, connectorID string, organizationID gid.GID, r *http.Request) (Connection, error)
|
||||
}
|
||||
|
||||
Connection interface {
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/getprobo/probo/pkg/statelesstoken"
|
||||
)
|
||||
|
||||
@@ -65,8 +66,8 @@ var (
|
||||
OAuth2TokenTTL = 10 * time.Minute
|
||||
)
|
||||
|
||||
func (c *OAuth2Connector) Initiate(ctx context.Context, connectorID string, organizationID string, r *http.Request) (string, error) {
|
||||
stateData := OAuth2State{OrganizationID: organizationID, ConnectorID: connectorID}
|
||||
func (c *OAuth2Connector) Initiate(ctx context.Context, connectorID string, organizationID gid.GID, r *http.Request) (string, error) {
|
||||
stateData := OAuth2State{OrganizationID: organizationID.String(), ConnectorID: connectorID}
|
||||
state, err := statelesstoken.NewToken(c.ClientSecret, OAuth2TokenType, OAuth2TokenTTL, stateData)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot create state token: %w", err)
|
||||
@@ -78,7 +79,7 @@ func (c *OAuth2Connector) Initiate(ctx context.Context, connectorID string, orga
|
||||
}
|
||||
|
||||
redirectQuery := url.Values{}
|
||||
redirectQuery.Set("organization_id", organizationID)
|
||||
redirectQuery.Set("organization_id", organizationID.String())
|
||||
redirectQuery.Set("connector_id", connectorID)
|
||||
|
||||
redirectURI.RawQuery = redirectQuery.Encode()
|
||||
@@ -100,7 +101,7 @@ func (c *OAuth2Connector) Initiate(ctx context.Context, connectorID string, orga
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func (c *OAuth2Connector) Complete(ctx context.Context, connectorID string, organizationID string, r *http.Request) (Connection, error) {
|
||||
func (c *OAuth2Connector) Complete(ctx context.Context, connectorID string, organizationID gid.GID, r *http.Request) (Connection, error) {
|
||||
code := r.URL.Query().Get("code")
|
||||
if code == "" {
|
||||
return nil, fmt.Errorf("no code in request")
|
||||
@@ -116,7 +117,7 @@ func (c *OAuth2Connector) Complete(ctx context.Context, connectorID string, orga
|
||||
return nil, fmt.Errorf("cannot validate state token: %w", err)
|
||||
}
|
||||
|
||||
if payload.Data.OrganizationID != organizationID {
|
||||
if payload.Data.OrganizationID != organizationID.String() {
|
||||
return nil, fmt.Errorf("invalid organization ID")
|
||||
}
|
||||
|
||||
@@ -130,7 +131,7 @@ func (c *OAuth2Connector) Complete(ctx context.Context, connectorID string, orga
|
||||
}
|
||||
|
||||
redirectQuery := url.Values{}
|
||||
redirectQuery.Set("organization_id", organizationID)
|
||||
redirectQuery.Set("organization_id", organizationID.String())
|
||||
redirectQuery.Set("connector_id", connectorID)
|
||||
|
||||
redirectURI.RawQuery = redirectQuery.Encode()
|
||||
|
||||
@@ -19,6 +19,8 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -54,7 +56,7 @@ func (cr *ConnectorRegistry) Get(connectorID string) (Connector, error) {
|
||||
return connector, nil
|
||||
}
|
||||
|
||||
func (cr *ConnectorRegistry) Initiate(ctx context.Context, connectorID string, organizationID string, r *http.Request) (string, error) {
|
||||
func (cr *ConnectorRegistry) Initiate(ctx context.Context, connectorID string, organizationID gid.GID, r *http.Request) (string, error) {
|
||||
connector, err := cr.Get(connectorID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot initiate connector: %w", err)
|
||||
@@ -63,7 +65,7 @@ func (cr *ConnectorRegistry) Initiate(ctx context.Context, connectorID string, o
|
||||
return connector.Initiate(ctx, connectorID, organizationID, r)
|
||||
}
|
||||
|
||||
func (cr *ConnectorRegistry) Complete(ctx context.Context, connectorID string, organizationID string, r *http.Request) (Connection, error) {
|
||||
func (cr *ConnectorRegistry) Complete(ctx context.Context, connectorID string, organizationID gid.GID, r *http.Request) (Connection, error) {
|
||||
connector, err := cr.Get(connectorID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot complete connector: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user