forked from zph/bayeux
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the approach to get the Salesforce credentials (#2)
* Update the approch to get the Salesforce Credentials * Update the AuthenticationParameters struct implementation * Address review comments * Resolve lint issues * Resolve lint issues * Correct the module name * Resolve lint issues * Update the example to resolve linting errors
- Loading branch information
1 parent
ede9e8d
commit 3d75fa9
Showing
5 changed files
with
39 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -3,15 +3,13 @@ package bayeux | |
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"strings" | ||
"sync" | ||
"time" | ||
) | ||
|
@@ -32,11 +30,6 @@ type TriggerEvent struct { | |
Successful bool `json:"successful,omitempty"` | ||
} | ||
|
||
func (t TriggerEvent) channel() string { | ||
s := strings.Replace(t.Channel, "/topic/", "", 1) | ||
return s | ||
} | ||
|
||
// Status is the state of success and subscribed channels | ||
type Status struct { | ||
connected bool | ||
|
@@ -81,6 +74,14 @@ type clientIDAndCookies struct { | |
cookies []*http.Cookie | ||
} | ||
|
||
type AuthenticationParameters struct { | ||
ClientID string // consumer key from Salesforce (e.g. 3MVG9pRsdbjsbdjfm1I.fz3f7zBuH4xdKCJcM9B5XLgxXh2AFTmQmr8JMn1vsadjsadjjsadakd_C) | ||
ClientSecret string // consumer secret from Salesforce (e.g. E9FE118633BC7SGDADUHUE81F19C1D4529D09CB7231754AD2F2CA668400619) | ||
Username string // Salesforce user email (e.g. [email protected]) | ||
Password string // Salesforce password | ||
TokenURL string // Salesforce token endpoint (e.g. https://login.salesforce.com/services/oauth2/token) | ||
} | ||
|
||
// Bayeux struct allow for centralized storage of creds, ids, and cookies | ||
type Bayeux struct { | ||
creds Credentials | ||
|
@@ -115,7 +116,7 @@ func (b *Bayeux) call(body string, route string) (resp *http.Response, e error) | |
logger.Printf("Bad bayeuxCall io.EOF: %s\n", err) | ||
logger.Printf("Bad bayeuxCall Response: %+v\n", resp) | ||
} else if err != nil { | ||
e = errors.New(fmt.Sprintf("Unknown error: %s", err)) | ||
e = fmt.Errorf("unknown error: %w", err) | ||
logger.Printf("Bad unrecoverable Call: %s", err) | ||
} | ||
return resp, e | ||
|
@@ -238,39 +239,25 @@ func (b *Bayeux) connect(out chan TriggerEvent) chan TriggerEvent { | |
return out | ||
} | ||
|
||
func GetSalesforceCredentials() Credentials { | ||
clientID := mustGetEnv("SALESFORCE_CONSUMER_KEY") | ||
clientSecret := mustGetEnv("SALESFORCE_CONSUMER_SECRET") | ||
username := mustGetEnv("SALESFORCE_USER") | ||
password := mustGetEnv("SALESFORCE_PASSWORD") | ||
tokenURL := mustGetEnv("SALESFORCE_TOKEN_URL") | ||
func GetSalesforceCredentials(ap AuthenticationParameters) (creds *Credentials, err error) { | ||
params := url.Values{"grant_type": {"password"}, | ||
"client_id": {clientID}, | ||
"client_secret": {clientSecret}, | ||
"username": {username}, | ||
"password": {password}} | ||
res, err := http.PostForm(tokenURL, params) | ||
"client_id": {ap.ClientID}, | ||
"client_secret": {ap.ClientSecret}, | ||
"username": {ap.Username}, | ||
"password": {ap.Password}} | ||
res, err := http.PostForm(ap.TokenURL, params) | ||
if err != nil { | ||
logger.Fatal(err) | ||
} | ||
decoder := json.NewDecoder(res.Body) | ||
var creds Credentials | ||
if err := decoder.Decode(&creds); err == io.EOF { | ||
logger.Fatal(err) | ||
return nil, err | ||
} else if err != nil { | ||
logger.Fatal(err) | ||
return nil, err | ||
} else if creds.AccessToken == "" { | ||
logger.Fatalf("Unable to fetch access token. Check credentials in environmental variables") | ||
} | ||
return creds | ||
} | ||
|
||
func mustGetEnv(s string) string { | ||
r := os.Getenv(s) | ||
if r == "" { | ||
panic(fmt.Sprintf("Could not fetch key %s", s)) | ||
return nil, fmt.Errorf("unable to fetch access token: %w", err) | ||
} | ||
return r | ||
return creds, nil | ||
} | ||
|
||
func (b *Bayeux) Channel(out chan TriggerEvent, r string, creds Credentials, channel string) chan TriggerEvent { | ||
|
This file contains 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 |
---|---|---|
|
@@ -8,8 +8,15 @@ func Example() { | |
out := make(chan TriggerEvent) | ||
replay := "-1" | ||
b := Bayeux{} | ||
creds := GetSalesforceCredentials() | ||
c := b.Channel(out, replay, creds, "channel") | ||
// Create a variable of type AuthenticationParameters and set the values | ||
var ap AuthenticationParameters | ||
ap.ClientID = "3MVG9pRsdbjsbdjfm1I.fz3f7zBuH4xdKCJcM9B5XLgxXh2AFTmQmr8JMn1vsadjsadjjsadakd_C" | ||
ap.ClientSecret = "E9FE118633BC7SGDADUHUE81F19C1D4529D09CB7231754AD2F2CA668400619" | ||
ap.Username = "[email protected]" | ||
ap.Password = "foobar" | ||
ap.TokenURL = "https://login.salesforce.com/services/oauth2/token" | ||
creds, _ := GetSalesforceCredentials(ap) | ||
c := b.Channel(out, replay, *creds, "channel") | ||
for { | ||
select { | ||
case e := <-c: | ||
|
This file contains 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 |
---|---|---|
|
@@ -9,9 +9,15 @@ import ( | |
func Example() { | ||
out := make(chan bay.TriggerEvent) | ||
b := bay.Bayeux{} | ||
creds := bay.GetSalesforceCredentials() | ||
var ap bay.AuthenticationParameters | ||
ap.ClientID = "3MVG9pRsdbjsbdjfm1I.fz3f7zBuH4xdKCJcM9B5XLgxXh2AFTmQmr8JMn1vsadjsadjjsadakd_C" | ||
ap.ClientSecret = "E9FE118633BC7SGDADUHUE81F19C1D4529D09CB7231754AD2F2CA668400619" | ||
ap.Username = "[email protected]" | ||
ap.Password = "foobar" | ||
ap.TokenURL = "https://login.salesforce.com/services/oauth2/token" | ||
creds, _ := bay.GetSalesforceCredentials(ap) | ||
replay := "-1" | ||
c := b.Channel(out, replay, creds, "channel") | ||
c := b.Channel(out, replay, *creds, "channel") | ||
for { | ||
select { | ||
case e := <-c: | ||
|
This file contains 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,3 @@ | ||
module github.com/elastic/bayeux | ||
|
||
go 1.17 |