Skip to content

Commit 3d75fa9

Browse files
authored
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
1 parent ede9e8d commit 3d75fa9

File tree

5 files changed

+39
-36
lines changed

5 files changed

+39
-36
lines changed

bayeux.go

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ package bayeux
33
import (
44
"bytes"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"io"
98
"io/ioutil"
109
"log"
1110
"net/http"
1211
"net/url"
1312
"os"
14-
"strings"
1513
"sync"
1614
"time"
1715
)
@@ -32,11 +30,6 @@ type TriggerEvent struct {
3230
Successful bool `json:"successful,omitempty"`
3331
}
3432

35-
func (t TriggerEvent) channel() string {
36-
s := strings.Replace(t.Channel, "/topic/", "", 1)
37-
return s
38-
}
39-
4033
// Status is the state of success and subscribed channels
4134
type Status struct {
4235
connected bool
@@ -81,6 +74,14 @@ type clientIDAndCookies struct {
8174
cookies []*http.Cookie
8275
}
8376

77+
type AuthenticationParameters struct {
78+
ClientID string // consumer key from Salesforce (e.g. 3MVG9pRsdbjsbdjfm1I.fz3f7zBuH4xdKCJcM9B5XLgxXh2AFTmQmr8JMn1vsadjsadjjsadakd_C)
79+
ClientSecret string // consumer secret from Salesforce (e.g. E9FE118633BC7SGDADUHUE81F19C1D4529D09CB7231754AD2F2CA668400619)
80+
Username string // Salesforce user email (e.g. [email protected])
81+
Password string // Salesforce password
82+
TokenURL string // Salesforce token endpoint (e.g. https://login.salesforce.com/services/oauth2/token)
83+
}
84+
8485
// Bayeux struct allow for centralized storage of creds, ids, and cookies
8586
type Bayeux struct {
8687
creds Credentials
@@ -115,7 +116,7 @@ func (b *Bayeux) call(body string, route string) (resp *http.Response, e error)
115116
logger.Printf("Bad bayeuxCall io.EOF: %s\n", err)
116117
logger.Printf("Bad bayeuxCall Response: %+v\n", resp)
117118
} else if err != nil {
118-
e = errors.New(fmt.Sprintf("Unknown error: %s", err))
119+
e = fmt.Errorf("unknown error: %w", err)
119120
logger.Printf("Bad unrecoverable Call: %s", err)
120121
}
121122
return resp, e
@@ -238,39 +239,25 @@ func (b *Bayeux) connect(out chan TriggerEvent) chan TriggerEvent {
238239
return out
239240
}
240241

241-
func GetSalesforceCredentials() Credentials {
242-
clientID := mustGetEnv("SALESFORCE_CONSUMER_KEY")
243-
clientSecret := mustGetEnv("SALESFORCE_CONSUMER_SECRET")
244-
username := mustGetEnv("SALESFORCE_USER")
245-
password := mustGetEnv("SALESFORCE_PASSWORD")
246-
tokenURL := mustGetEnv("SALESFORCE_TOKEN_URL")
242+
func GetSalesforceCredentials(ap AuthenticationParameters) (creds *Credentials, err error) {
247243
params := url.Values{"grant_type": {"password"},
248-
"client_id": {clientID},
249-
"client_secret": {clientSecret},
250-
"username": {username},
251-
"password": {password}}
252-
res, err := http.PostForm(tokenURL, params)
244+
"client_id": {ap.ClientID},
245+
"client_secret": {ap.ClientSecret},
246+
"username": {ap.Username},
247+
"password": {ap.Password}}
248+
res, err := http.PostForm(ap.TokenURL, params)
253249
if err != nil {
254250
logger.Fatal(err)
255251
}
256252
decoder := json.NewDecoder(res.Body)
257-
var creds Credentials
258253
if err := decoder.Decode(&creds); err == io.EOF {
259-
logger.Fatal(err)
254+
return nil, err
260255
} else if err != nil {
261-
logger.Fatal(err)
256+
return nil, err
262257
} else if creds.AccessToken == "" {
263-
logger.Fatalf("Unable to fetch access token. Check credentials in environmental variables")
264-
}
265-
return creds
266-
}
267-
268-
func mustGetEnv(s string) string {
269-
r := os.Getenv(s)
270-
if r == "" {
271-
panic(fmt.Sprintf("Could not fetch key %s", s))
258+
return nil, fmt.Errorf("unable to fetch access token: %w", err)
272259
}
273-
return r
260+
return creds, nil
274261
}
275262

276263
func (b *Bayeux) Channel(out chan TriggerEvent, r string, creds Credentials, channel string) chan TriggerEvent {

example_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ func Example() {
88
out := make(chan TriggerEvent)
99
replay := "-1"
1010
b := Bayeux{}
11-
creds := GetSalesforceCredentials()
12-
c := b.Channel(out, replay, creds, "channel")
11+
// Create a variable of type AuthenticationParameters and set the values
12+
var ap AuthenticationParameters
13+
ap.ClientID = "3MVG9pRsdbjsbdjfm1I.fz3f7zBuH4xdKCJcM9B5XLgxXh2AFTmQmr8JMn1vsadjsadjjsadakd_C"
14+
ap.ClientSecret = "E9FE118633BC7SGDADUHUE81F19C1D4529D09CB7231754AD2F2CA668400619"
15+
ap.Username = "[email protected]"
16+
ap.Password = "foobar"
17+
ap.TokenURL = "https://login.salesforce.com/services/oauth2/token"
18+
creds, _ := GetSalesforceCredentials(ap)
19+
c := b.Channel(out, replay, *creds, "channel")
1320
for {
1421
select {
1522
case e := <-c:

examples/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ import (
99
func Example() {
1010
out := make(chan bay.TriggerEvent)
1111
b := bay.Bayeux{}
12-
creds := bay.GetSalesforceCredentials()
12+
var ap bay.AuthenticationParameters
13+
ap.ClientID = "3MVG9pRsdbjsbdjfm1I.fz3f7zBuH4xdKCJcM9B5XLgxXh2AFTmQmr8JMn1vsadjsadjjsadakd_C"
14+
ap.ClientSecret = "E9FE118633BC7SGDADUHUE81F19C1D4529D09CB7231754AD2F2CA668400619"
15+
ap.Username = "[email protected]"
16+
ap.Password = "foobar"
17+
ap.TokenURL = "https://login.salesforce.com/services/oauth2/token"
18+
creds, _ := bay.GetSalesforceCredentials(ap)
1319
replay := "-1"
14-
c := b.Channel(out, replay, creds, "channel")
20+
c := b.Channel(out, replay, *creds, "channel")
1521
for {
1622
select {
1723
case e := <-c:

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/elastic/bayeux
2+
3+
go 1.17

go.sum

Whitespace-only changes.

0 commit comments

Comments
 (0)