gofiber/keyauth

Parameters are not accessible from within keyauth

DavZim opened this issue · 4 comments

While preparing the tests for #90 , I found that the c.AllParams() or c.Params(param) do not return any values from within keyauth.

To replicate this issue, consider this example

package main

import (
  "fmt"

  "github.com/gofiber/fiber/v2"
  "github.com/gofiber/keyauth/v2"
)

var (
  APIKey = "password1"
)

func main() {
  app := fiber.New()

  app.Use(keyauth.New(keyauth.Config{
    KeyLookup:  "param:access_token",
    Validator:  func(c *fiber.Ctx, key string) (bool, error) {
      if key == APIKey {
        return true, nil
      }
      return false, keyauth.ErrMissingOrMalformedAPIKey
    },
  }))

  app.Get("/:access_token", func(c *fiber.Ctx) error {
    return c.SendString(fmt.Sprintf("Found Parameters: %+v\n", c.AllParams()))
  })

  app.Listen(":3000")
}

When accessing the app with curl http://localhost:3000/password1 it returns 200 - missing or malformed API Key (not expected as the access_token is given the correct key).

When I remove the keyauth part and query the API again with the above command, I receive Found Parameters: map[access_token:password1] (expected).

While debugging this, I injected the following print statement in func keyFromParam

fmt.Printf("Found Parameters: '%v'", c.AllParams())

and for the above request, it printed: Found Parameters: 'map[]', meaning there is no key found when extracting the password, hence this issue.

This is the normal expected behavior, the keyauth middleware runs on a route without parameters "/"

How would I then specify and access the param values for authentication?

app.Use("/:accessToken", keyauthMiddleware)

Got it. I created test cases for this in #90