WithSecret wraps RouteHandler with secret handling
(h RouteHandler)
| 45 | |
| 46 | // WithSecret wraps RouteHandler with secret handling |
| 47 | func (r *Router) WithSecret(h RouteHandler) RouteHandler { |
| 48 | if len(r.config.Secret) == 0 { |
| 49 | return h |
| 50 | } |
| 51 | |
| 52 | authHeader := fmt.Appendf(nil, "Bearer %s", r.config.Secret) |
| 53 | |
| 54 | return func(reqID string, rw ResponseWriter, req *http.Request) *Error { |
| 55 | if subtle.ConstantTimeCompare([]byte(req.Header.Get(httpheaders.Authorization)), authHeader) == 1 { |
| 56 | return h(reqID, rw, req) |
| 57 | } else { |
| 58 | return NewError(newInvalidSecretError(), errCategorySecurity) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // WithPanic recovers panic and converts it to normal error |
| 64 | func (r *Router) WithPanic(h RouteHandler) RouteHandler { |