-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from githubsands/primaryHandler
Create primaryHandler object inplace of the duplicate caduceusHandler that handles the primary routes - authorization and notification(notify) of the webhooks
- Loading branch information
Showing
5 changed files
with
218 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/Comcast/webpa-common/secure" | ||
"github.com/Comcast/webpa-common/secure/handler" | ||
"github.com/Comcast/webpa-common/secure/key" | ||
"github.com/SermoDigital/jose/jwt" | ||
"github.com/go-kit/kit/log" | ||
"github.com/gorilla/mux" | ||
"github.com/justinas/alice" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
baseURI = "api" | ||
version = "v3" | ||
) | ||
|
||
type JWTValidator struct { | ||
// JWTKeys is used to create the key.Resolver for JWT verification keys | ||
Keys key.ResolverFactory | ||
|
||
// Custom is an optional configuration section that defines | ||
// custom rules for validation over and above the standard RFC rules. | ||
Custom secure.JWTValidatorFactory | ||
} | ||
|
||
func NewPrimaryHandler(l log.Logger, v *viper.Viper, sw *ServerHandler) (*mux.Router, error) { | ||
var ( | ||
router = mux.NewRouter() | ||
) | ||
|
||
validator, err := getValidator(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
authHandler := handler.AuthorizationHandler{ | ||
HeaderName: "Authorization", | ||
ForbiddenStatusCode: 403, | ||
Validator: validator, | ||
Logger: l, | ||
} | ||
|
||
authorizationDecorator := alice.New(authHandler.Decorate) | ||
|
||
return configServerRouter(router, authorizationDecorator, sw), nil | ||
} | ||
|
||
func configServerRouter(router *mux.Router, primaryHandler alice.Chain, serverWrapper *ServerHandler) *mux.Router { | ||
var singleContentType = func(r *http.Request, _ *mux.RouteMatch) bool { | ||
return len(r.Header["Content-Type"]) == 1 //require single specification for Content-Type Header | ||
} | ||
|
||
router.Handle("/"+fmt.Sprintf("%s/%s", baseURI, version)+"/notify", primaryHandler.Then(serverWrapper)).Methods("POST").HeadersRegexp("Content-Type", "application/msgpack").MatcherFunc(singleContentType) | ||
|
||
return router | ||
} | ||
|
||
func getValidator(v *viper.Viper) (validator secure.Validator, err error) { | ||
var jwtVals []JWTValidator | ||
|
||
v.UnmarshalKey("jwtValidators", &jwtVals) | ||
|
||
// if a JWTKeys section was supplied, configure a JWS validator | ||
// and append it to the chain of validators | ||
validators := make(secure.Validators, 0, len(jwtVals)) | ||
|
||
for _, validatorDescriptor := range jwtVals { | ||
var keyResolver key.Resolver | ||
keyResolver, err = validatorDescriptor.Keys.NewResolver() | ||
if err != nil { | ||
validator = validators | ||
return | ||
} | ||
|
||
validators = append( | ||
validators, | ||
secure.JWSValidator{ | ||
DefaultKeyId: DEFAULT_KEY_ID, | ||
Resolver: keyResolver, | ||
JWTValidators: []*jwt.Validator{validatorDescriptor.Custom.New()}, | ||
}, | ||
) | ||
} | ||
|
||
// TODO: This should really be part of the unmarshalled validators somehow | ||
basicAuth := v.GetStringSlice("authHeader") | ||
for _, authValue := range basicAuth { | ||
validators = append( | ||
validators, | ||
secure.ExactMatchValidator(authValue), | ||
) | ||
} | ||
|
||
validator = validators | ||
|
||
return | ||
} |
Oops, something went wrong.