Skip to content

Commit

Permalink
Update the approach to get the Salesforce credentials (#2)
Browse files Browse the repository at this point in the history
* 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
yug-rajani authored Apr 6, 2022
1 parent ede9e8d commit 3d75fa9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
51 changes: 19 additions & 32 deletions bayeux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package bayeux
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 9 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 8 additions & 2 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/elastic/bayeux

go 1.17
Empty file added go.sum
Empty file.

0 comments on commit 3d75fa9

Please sign in to comment.