Skip to content

Commit ec26d92

Browse files
authored
[WIRE-510] Pass organization header while creating token (#164)
1 parent ea7cfdc commit ec26d92

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

cli/device/list.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func (r listResult) Data() interface{} {
9292
return r.devices
9393
}
9494

95+
func cleanStrings(serial string) string {
96+
serial = strings.Trim(serial, "\n")
97+
return strings.Trim(serial, " ")
98+
}
99+
95100
func (r listResult) String() string {
96101
if len(r.devices) == 0 {
97102
return "No devices found."
@@ -100,11 +105,11 @@ func (r listResult) String() string {
100105
t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber", "Status", "Tags")
101106
for _, device := range r.devices {
102107
t.AddRow(
103-
device.Name,
108+
cleanStrings(device.Name),
104109
device.ID,
105110
device.Board,
106111
device.FQBN,
107-
device.Serial,
112+
cleanStrings(device.Serial),
108113
dereferenceString(device.Status),
109114
strings.Join(device.Tags, ","),
110115
)

internal/iot/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,15 +560,15 @@ func (cl *Client) TemplateApply(ctx context.Context, id, thingId, prefix, device
560560
return dev, nil
561561
}
562562

563-
func (cl *Client) setup(client, secret, organization string) error {
563+
func (cl *Client) setup(client, secret, organizationId string) error {
564564
baseURL := GetArduinoAPIBaseURL()
565565

566566
// Configure a token source given the user's credentials.
567-
cl.token = NewUserTokenSource(client, secret, baseURL)
567+
cl.token = NewUserTokenSource(client, secret, baseURL, organizationId)
568568

569569
config := iotclient.NewConfiguration()
570-
if organization != "" {
571-
config.AddDefaultHeader("X-Organization", organization)
570+
if organizationId != "" {
571+
config.AddDefaultHeader("X-Organization", organizationId)
572572
}
573573
config.Servers = iotclient.ServerConfigurations{
574574
{

internal/iot/token.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ func GetArduinoAPIBaseURL() string {
3939
}
4040

4141
// Build a new token source to forge api JWT tokens based on provided credentials
42-
func NewUserTokenSource(client, secret, baseURL string) oauth2.TokenSource {
42+
func NewUserTokenSource(client, secret, baseURL, organizationId string) oauth2.TokenSource {
4343
// We need to pass the additional "audience" var to request an access token.
4444
additionalValues := url.Values{}
4545
additionalValues.Add("audience", "https://api2.arduino.cc/iot")
46+
if organizationId != "" {
47+
additionalValues.Add("organization_id", organizationId)
48+
}
4649
// Set up OAuth2 configuration.
4750
config := cc.Config{
4851
ClientID: client,

internal/ota-api/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type OtaApiClient struct {
4949

5050
func NewClient(credentials *config.Credentials) *OtaApiClient {
5151
host := iot.GetArduinoAPIBaseURL()
52-
tokenSource := iot.NewUserTokenSource(credentials.Client, credentials.Secret, host)
52+
tokenSource := iot.NewUserTokenSource(credentials.Client, credentials.Secret, host, credentials.Organization)
5353
return &OtaApiClient{
5454
client: &http.Client{},
5555
src: tokenSource,

internal/storage-api/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func getArduinoAPIBaseURL() string {
5757
func NewClient(credentials *config.Credentials) *StorageApiClient {
5858
host := getArduinoAPIBaseURL()
5959
iothost := iot.GetArduinoAPIBaseURL()
60-
tokenSource := iot.NewUserTokenSource(credentials.Client, credentials.Secret, iothost)
60+
tokenSource := iot.NewUserTokenSource(credentials.Client, credentials.Secret, iothost, credentials.Organization)
6161
return &StorageApiClient{
6262
client: &http.Client{},
6363
src: tokenSource,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy