Skip to content

Commit

Permalink
[WIRE-510] Pass organization header while creating token (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjtokenring authored Sep 12, 2024
1 parent ea7cfdc commit ec26d92
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
9 changes: 7 additions & 2 deletions cli/device/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func (r listResult) Data() interface{} {
return r.devices
}

func cleanStrings(serial string) string {
serial = strings.Trim(serial, "\n")
return strings.Trim(serial, " ")
}

func (r listResult) String() string {
if len(r.devices) == 0 {
return "No devices found."
Expand All @@ -100,11 +105,11 @@ func (r listResult) String() string {
t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber", "Status", "Tags")
for _, device := range r.devices {
t.AddRow(
device.Name,
cleanStrings(device.Name),
device.ID,
device.Board,
device.FQBN,
device.Serial,
cleanStrings(device.Serial),
dereferenceString(device.Status),
strings.Join(device.Tags, ","),
)
Expand Down
8 changes: 4 additions & 4 deletions internal/iot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,15 +560,15 @@ func (cl *Client) TemplateApply(ctx context.Context, id, thingId, prefix, device
return dev, nil
}

func (cl *Client) setup(client, secret, organization string) error {
func (cl *Client) setup(client, secret, organizationId string) error {
baseURL := GetArduinoAPIBaseURL()

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

config := iotclient.NewConfiguration()
if organization != "" {
config.AddDefaultHeader("X-Organization", organization)
if organizationId != "" {
config.AddDefaultHeader("X-Organization", organizationId)
}
config.Servers = iotclient.ServerConfigurations{
{
Expand Down
5 changes: 4 additions & 1 deletion internal/iot/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ func GetArduinoAPIBaseURL() string {
}

// Build a new token source to forge api JWT tokens based on provided credentials
func NewUserTokenSource(client, secret, baseURL string) oauth2.TokenSource {
func NewUserTokenSource(client, secret, baseURL, organizationId string) oauth2.TokenSource {
// We need to pass the additional "audience" var to request an access token.
additionalValues := url.Values{}
additionalValues.Add("audience", "https://api2.arduino.cc/iot")
if organizationId != "" {
additionalValues.Add("organization_id", organizationId)
}
// Set up OAuth2 configuration.
config := cc.Config{
ClientID: client,
Expand Down
2 changes: 1 addition & 1 deletion internal/ota-api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type OtaApiClient struct {

func NewClient(credentials *config.Credentials) *OtaApiClient {
host := iot.GetArduinoAPIBaseURL()
tokenSource := iot.NewUserTokenSource(credentials.Client, credentials.Secret, host)
tokenSource := iot.NewUserTokenSource(credentials.Client, credentials.Secret, host, credentials.Organization)
return &OtaApiClient{
client: &http.Client{},
src: tokenSource,
Expand Down
2 changes: 1 addition & 1 deletion internal/storage-api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func getArduinoAPIBaseURL() string {
func NewClient(credentials *config.Credentials) *StorageApiClient {
host := getArduinoAPIBaseURL()
iothost := iot.GetArduinoAPIBaseURL()
tokenSource := iot.NewUserTokenSource(credentials.Client, credentials.Secret, iothost)
tokenSource := iot.NewUserTokenSource(credentials.Client, credentials.Secret, iothost, credentials.Organization)
return &StorageApiClient{
client: &http.Client{},
src: tokenSource,
Expand Down

0 comments on commit ec26d92

Please sign in to comment.