{"id":"7b7ccc353cce8004","repo":"gofiber/fiber","slug":"fiber-keyauth-middleware-requires-a-validator-fun","errorCode":null,"errorMessage":"fiber: keyauth middleware requires a validator function","messagePattern":"fiber: keyauth middleware requires a validator function","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"middleware/keyauth/config.go","lineNumber":105,"sourceCode":"}\n\n// ConfigDefault is the default config\nvar ConfigDefault = Config{\n\tSuccessHandler: func(c fiber.Ctx) error {\n\t\treturn c.Next()\n\t},\n\tErrorHandler: func(c fiber.Ctx, _ error) error {\n\t\treturn c.Status(fiber.StatusUnauthorized).SendString(ErrMissingOrMalformedAPIKey.Error())\n\t},\n\tRealm:     \"Restricted\",\n\tExtractor: extractors.FromAuthHeader(\"Bearer\"),\n}\n\n// configDefault is a helper function to set default values\nfunc configDefault(config ...Config) Config {\n\t// Return default config if nothing provided\n\tif len(config) < 1 {\n\t\tpanic(\"fiber: keyauth middleware requires a validator function\")\n\t}\n\tcfg := config[0]\n\n\t// Require a validator function\n\tif cfg.Validator == nil {\n\t\tpanic(\"fiber: keyauth middleware requires a validator function\")\n\t}\n\n\t// Set default values\n\tif cfg.Extractor.Extract == nil {\n\t\tcfg.Extractor = ConfigDefault.Extractor\n\t}\n\tif cfg.Realm == \"\" {\n\t\tcfg.Realm = ConfigDefault.Realm\n\t}\n\tif cfg.SuccessHandler == nil {\n\t\tcfg.SuccessHandler = ConfigDefault.SuccessHandler\n\t}","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/middleware/keyauth/config.go#L87-L123","documentation":"keyauth middleware authenticates requests by validating an extracted API key via a caller-supplied Validator function; without config there is no Validator to call. configDefault (keyauth/config.go:103-106) panics when New() is called with zero variadic arguments so the middleware cannot be mounted in a state that would accept every key (or reject every key) without explicit policy.","triggerScenarios":"Calling keyauth.New() with no arguments — keyauth.New() instead of keyauth.New(keyauth.Config{Validator: ...}).","commonSituations":"Refactoring that temporarily removes the config argument, or scaffolding code that wires the middleware before implementing the validator.","solutions":["Pass a Config with a Validator: keyauth.New(keyauth.Config{Validator: func(c fiber.Ctx, key string) (bool, error) { ... }}).","If you are not ready to validate keys, remove the middleware from the router until the validator is implemented."],"exampleFix":"// before\napp.Use(keyauth.New())\n\n// after\napp.Use(keyauth.New(keyauth.Config{\n    Validator: func(c fiber.Ctx, key string) (bool, error) {\n        return key == os.Getenv(\"API_KEY\"), nil\n    },\n}))","handlingStrategy":"type-guard","validationCode":"func requireKeyAuthConfig(cfg ...keyauth.Config) (keyauth.Config, error) {\n    if len(cfg) == 0 {\n        return keyauth.Config{}, errors.New(\"keyauth.New requires a Config with a Validator\")\n    }\n    return cfg[0], nil\n}\n\nc, err := requireKeyAuthConfig()\nif err != nil { log.Fatal(err) }","typeGuard":"// Type guard: ensure the variadic config is present before calling New.\nfunc hasKeyAuthConfig(cfg ...keyauth.Config) bool {\n    return len(cfg) > 0\n}","tryCatchPattern":null,"preventionTips":["Never call keyauth.New() with no arguments.","Wire the Validator before adding the middleware to the router."],"tags":["keyauth","authentication","config","validator","panic"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}