Skip to content

Commit

Permalink
Merge pull request #70 from Comcast/hotfix/device-mapping
Browse files Browse the repository at this point in the history
Refactored the device connection to use the same chain with only a va…
  • Loading branch information
johnabass authored Oct 30, 2018
2 parents 6ba2115 + 6f71bf5 commit 744661c
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions src/talaria/primaryHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,34 +126,31 @@ func NewPrimaryHandler(logger log.Logger, manager device.Manager, v *viper.Viper
Registry: manager,
})).Methods("GET")

// the connect handler decorated for authorization
apiHandler.Handle(
"/device",
authorizationDecoratorDevice(
// TODO: add other constructors
device.UseID.FromHeader(&device.ConnectHandler{
Logger: logger,
Connector: manager,
}),
),
).HeadersRegexp("Authorization", ".*")

// the connect handler is not decorated for authorization
deviceDecorator := alice.New(
xcontext.Populate(
0,
logginghttp.SetLogger(
logger,
logginghttp.Header(device.DeviceNameHeader, device.DeviceNameHeader),
logginghttp.RequestInfo,
var (
// the basic decorator chain all device connect handlers use
deviceConnectChain = alice.New(
xcontext.Populate(
0,
logginghttp.SetLogger(
logger,
logginghttp.Header(device.DeviceNameHeader, device.DeviceNameHeader),
logginghttp.RequestInfo,
),
),
),
controlConstructor,
device.UseID.FromHeader,
controlConstructor,
device.UseID.FromHeader,
)

connectHandler = &device.ConnectHandler{
Logger: logger,
Connector: manager,
}
)

if a != nil && e != nil {
deviceDecorator.Append(
// if a service discovery environment was configured, append the hash filter to enforce
// device hashing
deviceConnectChain.Append(
xfilter.NewConstructor(
xfilter.WithFilters(
servicehttp.NewHashFilter(a, &xhttp.Error{Code: http.StatusGone}, e.IsRegistered),
Expand All @@ -162,13 +159,16 @@ func NewPrimaryHandler(logger log.Logger, manager device.Manager, v *viper.Viper
)
}

// the secured variant of the device connect handler
apiHandler.Handle(
"/device",
deviceConnectChain.Append(authorizationDecoratorDevice).Then(connectHandler),
).HeadersRegexp("Authorization", ".*")

// fail open if no authorization header is sent
apiHandler.Handle(
"/device",
deviceDecorator.Then(
&device.ConnectHandler{
Logger: logger,
Connector: manager,
}),
deviceConnectChain.Then(connectHandler),
)

apiHandler.Handle(
Expand Down

0 comments on commit 744661c

Please sign in to comment.