I have modify the middleware to the below but it only work if the handler
function "/protected" is in the main function.
I have many handlers and do not want to put all of them in the main.go.
You suggest i define a struct and use member function of the struct, can
you elaborate more about for me or the format.
func ValidateToken(srv *server.Server) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
notAuth := []string{"/oauth2/token", "/credentials"} //List of endpoints
that doesn't require auth
requestPath := r.URL.Path //current request path
//check if request does not need authentication, serve the request if it
doesn't need it
for _, value := range notAuth {
if value == requestPath {
next.ServeHTTP(w, r)
return
}
}
_, err := srv.ValidationBearerToken(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
next.ServeHTTP(w, r)
})
}
}
On Thursday, April 4, 2019 at 10:46:08 AM UTC+3, [email protected] wrote:
>
> changing and using "router.Use(getTokenMW(server))" is validating all the
> routes. How can i exclude routes
> /oauth2/token and /credentials. I read that populate but i dont get the
> idea.
> Any clew about how to go about this?
>
> On Wednesday, April 3, 2019 at 6:47:12 PM UTC+3, Burak Serdar wrote:
>>
>> On Wed, Apr 3, 2019 at 8:35 AM <[email protected]> wrote:
>>
>> > type Route struct {
>> > Name string
>> > Method string
>> > Pattern string
>> > HandlerFunc http.HandlerFunc
>> > }
>> >
>> > type Routes []Route
>> >
>> > func NewRouter() *mux.Router {
>> > router := mux.NewRouter().StrictSlash(true)
>> > for _, route := range routes {
>> > var handler http.Handler
>> > handler = route.HandlerFunc
>> > handler = Logger(handler, route.Name)
>> >
>> > router.
>> > Methods(route.Method).
>> > Path(route.Pattern).
>> > Name(route.Name).
>> > Handler(handler)
>> > }
>> >
>> > return router
>> > }
>> >
>> > func Index(w http.ResponseWriter, r *http.Request) {
>> > fmt.Fprintf(w, "Hello World!")
>> > }
>> >
>> > var routes = Routes{
>> > {
>> > "Index",
>> > "GET",
>> > "/",
>> > Index,
>> > },
>> >
>> > {
>> > "protecteduri",
>> > strings.ToUpper("Get"),
>> > "/protected",
>> > protecteduri,
>> > },
>> > }
>> >
>> > My question is how do i apply the "validateToken" function (middleware)
>> to the routes in the router.go?
>> > The function is to validate the access token in the request message
>> before calling the handler functions.
>> >
>> Have you looked at the gorilla/mux documentation about middlewares?
>> There are examples there: https://godoc.org/github.com/gorilla/mux
>>
>> You need to change the validateToken func:
>>
>> func getTokenMW(srv *server.Server) func(http.Handler) http.Handler {
>> return func(next http.Handler) http.Handler {
>> return http.HandlerFunc(func(w http.ResponseWriter, r
>> *http.Request) {
>> }
>> }
>> }
>>
>> Then:
>> router.Use(getTokenMW(server))
>>
>> Or, you can define a struct, put the server pointer in it, and use a
>> member function of that struct as the middleware.
>>
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.