Skip to content

Commit

Permalink
added v2 endpoints for device backwards compatibility (#195)
Browse files Browse the repository at this point in the history
* added v2 endpoints for device backwards compatibility

* prep for release
  • Loading branch information
kristinapathak authored Dec 6, 2021
1 parent f7cc21e commit abd48d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [v0.6.1]
- Added v2 compatible device endpoint for older devices connecting. [#195](https://github.com/xmidt-org/talaria/pull/195)

## [v0.6.0]
- Updated api version in url to v3 to indicate breaking changes in response codes when an invalid auth is sent. This change was made in an earlier release (v0.5.13). [#194](https://github.com/xmidt-org/talaria/pull/194)

Expand Down Expand Up @@ -122,7 +125,8 @@ Switching to new build process
## [v0.1.1] Tue Mar 28 2017 Weston Schmidt - 0.1.1
- initial creation

[Unreleased]: https://github.com/xmidt-org/talaria/compare/v0.6.0...HEAD
[Unreleased]: https://github.com/xmidt-org/talaria/compare/v0.6.1...HEAD
[v0.6.1]: https://github.com/xmidt-org/talaria/compare/v0.6.0...v0.6.1
[v0.6.0]: https://github.com/xmidt-org/talaria/compare/v0.5.13...v0.6.0
[v0.5.13]: https://github.com/xmidt-org/talaria/compare/v0.5.12...v0.5.13
[v0.5.12]: https://github.com/xmidt-org/talaria/compare/v0.5.11...v0.5.12
Expand Down
31 changes: 23 additions & 8 deletions primaryHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ const (

// TODO: Should this change for talaria 2.0?
version = "v3"

// TODO: This should be configurable at some point
poolSize = 1000
v2 = "v2"

DefaultKeyID = "current"

Expand Down Expand Up @@ -212,6 +210,9 @@ func NewPrimaryHandler(logger log.Logger, manager device.Manager, v *viper.Viper
}

authConstructor = basculehttp.NewConstructor(authConstructorOptions...)
authConstructorLegacy := basculehttp.NewConstructor(append([]basculehttp.COption{
basculehttp.WithCErrorHTTPResponseFunc(basculehttp.LegacyOnErrorHTTPResponse),
}, authConstructorOptions...)...)

authEnforcer = basculehttp.NewEnforcer(
basculehttp.WithELogger(getLogger),
Expand All @@ -221,6 +222,20 @@ func NewPrimaryHandler(logger log.Logger, manager device.Manager, v *viper.Viper
)

authChain := alice.New(setLogger(logger), authConstructor, authEnforcer, basculehttp.NewListenerDecorator(listener))
authChainV2 := alice.New(setLogger(logger), authConstructorLegacy, authEnforcer, basculehttp.NewListenerDecorator(listener))

versionCompatibleAuth := alice.New(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(r http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
if vars != nil {
if vars["version"] == v2 {
authChainV2.Then(next).ServeHTTP(r, req)
return
}
}
authChain.Then(next).ServeHTTP(r, req)
})
})

apiHandler.Handle("/device/send",
alice.New(
Expand Down Expand Up @@ -269,17 +284,17 @@ func NewPrimaryHandler(logger log.Logger, manager device.Manager, v *viper.Viper
)
}

// the secured variant of the device connect handler
// the secured variant of the device connect handler - compatible with v2 and v3
apiHandler.Handle(
"/device",
fmt.Sprintf("%s/{version:%s|%s}/device", baseURI, v2, version),
deviceConnectChain.
Extend(authChain).
Extend(versionCompatibleAuth).
Append(DeviceMetadataMiddleware).
Then(connectHandler),
).HeadersRegexp("Authorization", ".*")

apiHandler.Handle(
"/device",
r.Handle(
fmt.Sprintf("%s/{version:%s|%s}/device", baseURI, v2, version),
deviceConnectChain.
Append(DeviceMetadataMiddleware).
Then(connectHandler),
Expand Down

0 comments on commit abd48d9

Please sign in to comment.