Skip to content

Commit

Permalink
fix: Use the AuthData from the incoming request for DeviceProfileClient
Browse files Browse the repository at this point in the history
fixes #631

Signed-off-by: Leonard Goodell <leonard.goodell@intel.com>
  • Loading branch information
Leonard Goodell committed Nov 7, 2023
1 parent d5af9c6 commit 1ad20d6
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions internal/handler/profile.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright © 2017-2018 VMware, Inc. All Rights Reserved.
* Copyright © 2023 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -24,8 +25,6 @@ import (
"net/http"

"github.com/edgexfoundry/edgex-ui-go/internal/container"
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v3/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v3/bootstrap/secret"
client "github.com/edgexfoundry/go-mod-core-contracts/v3/clients/http"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/common"
Expand All @@ -38,7 +37,6 @@ import (
func (rh *ResourceHandler) AddProfileYamlContent(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var profile dtos.DeviceProfile
jwtSecretProvider := secret.NewJWTSecretProvider(bootstrapContainer.SecretProviderExtFrom(rh.dic.Get))

data, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand All @@ -53,7 +51,7 @@ func (rh *ResourceHandler) AddProfileYamlContent(w http.ResponseWriter, r *http.

config := container.ConfigurationFrom(rh.dic.Get)
url := fmt.Sprintf("%s://%s:%d", config.Clients[metadataSvcName].Protocol, config.Clients[metadataSvcName].Host, config.Clients[metadataSvcName].Port)
c := client.NewDeviceProfileClient(url, jwtSecretProvider, false)
c := client.NewDeviceProfileClient(url, newProfileAuthInjector(r), false)

profiles := []requests.DeviceProfileRequest{
{
Expand All @@ -75,11 +73,10 @@ func (rh *ResourceHandler) FindProfileAndConvertToYamlByName(w http.ResponseWrit
defer r.Body.Close()
vars := mux.Vars(r)
profileName := vars["name"]
jwtSecretProvider := secret.NewJWTSecretProvider(bootstrapContainer.SecretProviderExtFrom(rh.dic.Get))

config := container.ConfigurationFrom(rh.dic.Get)
url := fmt.Sprintf("%s://%s:%d", config.Clients[metadataSvcName].Protocol, config.Clients[metadataSvcName].Host, config.Clients[metadataSvcName].Port)
c := client.NewDeviceProfileClient(url, jwtSecretProvider, false)
c := client.NewDeviceProfileClient(url, newProfileAuthInjector(r), false)
var resp responses.DeviceProfileResponse
var err error
if resp, err = c.DeviceProfileByName(context.Background(), profileName); err != nil {
Expand All @@ -97,7 +94,6 @@ func (rh *ResourceHandler) FindProfileAndConvertToYamlByName(w http.ResponseWrit
func (rh *ResourceHandler) UpdateProfileYamlContent(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var profile dtos.DeviceProfile
jwtSecretProvider := secret.NewJWTSecretProvider(bootstrapContainer.SecretProviderExtFrom(rh.dic.Get))

data, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand All @@ -112,7 +108,7 @@ func (rh *ResourceHandler) UpdateProfileYamlContent(w http.ResponseWriter, r *ht

config := container.ConfigurationFrom(rh.dic.Get)
url := fmt.Sprintf("%s://%s:%d", config.Clients[metadataSvcName].Protocol, config.Clients[metadataSvcName].Host, config.Clients[metadataSvcName].Port)
c := client.NewDeviceProfileClient(url, jwtSecretProvider, false)
c := client.NewDeviceProfileClient(url, newProfileAuthInjector(r), false)
profiles := []requests.DeviceProfileRequest{
{
BaseRequest: common.NewBaseRequest(),
Expand All @@ -129,3 +125,25 @@ func (rh *ResourceHandler) UpdateProfileYamlContent(w http.ResponseWriter, r *ht

w.Write(result)
}

// This AuthenticationInjector simply injects the Auth Data from the incoming request to the out going request
type profileAuthInjector struct {
authData string
}

func newProfileAuthInjector(request *http.Request) *profileAuthInjector {
p := profileAuthInjector{
authData: request.Header.Get("Authorization"),
}

return &p
}

func (p *profileAuthInjector) AddAuthenticationData(request *http.Request) error {
if len(p.authData) == 0 {
return nil
}

request.Header.Add("Authorization", p.authData)
return nil
}

0 comments on commit 1ad20d6

Please sign in to comment.