-
Notifications
You must be signed in to change notification settings - Fork 25
feat(goTo): add postAuth and modules #2947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
immdipu
wants to merge
2
commits into
main
Choose a base branch
from
goto-postAuth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package gotoconn | ||
|
|
||
| import ( | ||
| "context" | ||
| "strconv" | ||
|
|
||
| "github.com/amp-labs/connectors/common" | ||
| "github.com/amp-labs/connectors/common/urlbuilder" | ||
| ) | ||
|
|
||
| func (c *Connector) GetPostAuthInfo(ctx context.Context) (*common.PostAuthInfo, error) { | ||
| accountKey, err := c.retrieveAccountKey(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| c.accountKey = accountKey | ||
|
|
||
| catalogVars := map[string]string{ | ||
| "accountKey": accountKey, | ||
| } | ||
|
|
||
| return &common.PostAuthInfo{ | ||
| CatalogVars: &catalogVars, | ||
| }, nil | ||
| } | ||
|
immdipu marked this conversation as resolved.
|
||
|
|
||
| func (c *Connector) retrieveAccountKey(ctx context.Context) (string, error) { | ||
| url, err := c.getMeURL() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| resp, err := c.JSONHTTPClient().Get(ctx, url.String()) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| data, err := common.UnmarshalJSON[map[string]any](resp) | ||
| if err != nil { | ||
| return "", common.ErrFailedToUnmarshalBody | ||
| } | ||
|
|
||
| if data == nil { | ||
|
immdipu marked this conversation as resolved.
|
||
| return "", common.ErrMissingExpectedValues | ||
| } | ||
|
|
||
| rawAccountKey, ok := (*data)["accountKey"] | ||
| if !ok { | ||
| return "", common.ErrMissingExpectedValues | ||
| } | ||
|
|
||
| switch v := rawAccountKey.(type) { | ||
| case string: | ||
| return v, nil | ||
| case float64: | ||
| return strconv.FormatInt(int64(v), 10), nil | ||
| default: | ||
| return "", common.ErrMissingExpectedValues | ||
| } | ||
|
immdipu marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func (c *Connector) getMeURL() (*urlbuilder.URL, error) { | ||
| return urlbuilder.New(c.ProviderInfo().BaseURL, "/admin/rest/v1/me") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package gotoconn | ||
|
|
||
| type AuthMetadataVars struct { | ||
| AccountKey string | ||
| } | ||
|
|
||
| // NewAuthMetadataVars parses map into the model. | ||
| func NewAuthMetadataVars(dictionary map[string]string) *AuthMetadataVars { | ||
| return &AuthMetadataVars{ | ||
| AccountKey: dictionary["accountKey"], | ||
| } | ||
| } | ||
|
|
||
| func (v AuthMetadataVars) AsMap() *map[string]string { | ||
| return &map[string]string{ | ||
| "accountKey": v.AccountKey, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Package gotoconn implements the GoTo connector. | ||
| // | ||
| // The package is named "gotoconn" instead of "goto" because "goto" is a | ||
| // reserved keyword in Go and cannot be used as a package identifier. The | ||
| // "conn" suffix is short for "connector". | ||
| package gotoconn | ||
|
|
||
| import ( | ||
| "github.com/amp-labs/connectors/common" | ||
| "github.com/amp-labs/connectors/internal/components" | ||
| "github.com/amp-labs/connectors/providers" | ||
| ) | ||
|
|
||
| type Connector struct { | ||
| // Basic connector | ||
| *components.Connector | ||
|
|
||
| // Require authenticated client & account | ||
| common.RequireAuthenticatedClient | ||
| common.PostAuthInfo | ||
|
|
||
| accountKey string | ||
| } | ||
|
|
||
| func NewConnector(params common.ConnectorParams) (*Connector, error) { | ||
| conn, err := components.Initialize(providers.GoTo, params, constructor) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| authMetadata := NewAuthMetadataVars(params.Metadata) | ||
|
|
||
| conn.accountKey = authMetadata.AccountKey | ||
|
|
||
| return conn, nil | ||
| } | ||
|
|
||
| func constructor(base *components.Connector) (*Connector, error) { | ||
| return &Connector{Connector: base}, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
| "os/signal" | ||
| "syscall" | ||
|
|
||
| "github.com/amp-labs/connectors/providers/goto" | ||
| connTest "github.com/amp-labs/connectors/test/goto" | ||
| "github.com/amp-labs/connectors/test/utils" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Handle Ctrl-C gracefully. | ||
| ctx, done := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
| defer done() | ||
|
|
||
| // Set up slog logging. | ||
| utils.SetupLogging() | ||
|
|
||
| conn := connTest.GetGoToConnector(ctx) | ||
|
|
||
| info, err := conn.GetPostAuthInfo(ctx) | ||
| if err != nil || info.CatalogVars == nil { | ||
| utils.Fail("error obtaining auth info", "error", err) | ||
| } | ||
|
|
||
| accountKey := gotoconn.NewAuthMetadataVars(*info.CatalogVars).AccountKey | ||
|
|
||
| // Log the retrieved account key. | ||
| slog.Info("retrieved auth metadata", "account key", accountKey) | ||
| } | ||
|
immdipu marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package goto_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
|
|
||
| "github.com/amp-labs/connectors/common" | ||
| "github.com/amp-labs/connectors/common/scanning/credscanning" | ||
| "github.com/amp-labs/connectors/providers" | ||
| "github.com/amp-labs/connectors/providers/goto" | ||
| "github.com/amp-labs/connectors/test/utils" | ||
| "golang.org/x/oauth2" | ||
| ) | ||
|
|
||
| func GetGoToConnector(ctx context.Context) *gotoconn.Connector { | ||
| filePath := credscanning.LoadPath(providers.GoTo) | ||
| reader := utils.MustCreateProvCredJSON(filePath, true) | ||
|
|
||
| client, err := common.NewOAuthHTTPClient(ctx, | ||
| common.WithOAuthClient(http.DefaultClient), | ||
| common.WithOAuthConfig(getConfig(reader)), | ||
| common.WithOAuthToken(reader.GetOauthToken()), | ||
| ) | ||
| if err != nil { | ||
| utils.Fail(err.Error()) | ||
| } | ||
|
|
||
| conn, err := gotoconn.NewConnector(common.ConnectorParams{ | ||
| AuthenticatedClient: client, | ||
| }) | ||
| if err != nil { | ||
| utils.Fail("create goto connector", "error: ", err) | ||
|
immdipu marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return conn | ||
| } | ||
|
|
||
| func getConfig(reader *credscanning.ProviderCredentials) *oauth2.Config { | ||
| cfg := &oauth2.Config{ | ||
| ClientID: reader.Get(credscanning.Fields.ClientId), | ||
| ClientSecret: reader.Get(credscanning.Fields.ClientSecret), | ||
| RedirectURL: "https://dev-api.withampersand.com/callbacks/v1/oauth", | ||
| Endpoint: oauth2.Endpoint{ | ||
| AuthURL: "https://authentication.logmeininc.com/oauth/authorize", | ||
| TokenURL: "https://authentication.logmeininc.com/oauth/token", | ||
| AuthStyle: oauth2.AuthStyleInParams, | ||
| }, | ||
| } | ||
|
|
||
| return cfg | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.