Skip to content

Commit 468bd1e

Browse files
committed
address comments
Signed-off-by: Dave Lee <[email protected]>
1 parent 3e51245 commit 468bd1e

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

docs/api/middleware/keyauth.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ curl --header "Authorization: Bearer my-super-secret-key" http://localhost:3000
219219
| SuccessHandler | `fiber.Handler` | SuccessHandler defines a function which is executed for a valid key. | `nil` |
220220
| ErrorHandler | `fiber.ErrorHandler` | ErrorHandler defines a function which is executed for an invalid key. | `401 Invalid or expired key` |
221221
| KeyLookup | `string` | KeyLookup is a string in the form of "`<source>:<name>`" that is used to extract key from the request. | "header:Authorization" |
222-
| AdditionalKeyLookups | `[]string` | If additional fallback sources of keys are required, they can be specified here in order of precedence | []string{} (empty) |
222+
| FallbackKeyLookups | `[]string` | If additional fallback sources of keys are required, they can be specified here in order of precedence | []string{} (empty) |
223223
| AuthScheme | `string` | AuthScheme to be used in the Authorization header. | "Bearer" |
224224
| Validator | `func(*fiber.Ctx, string) (bool, error)` | Validator is a function to validate the key. | A function for key validation |
225225
| ContextKey | `interface{}` | Context key to store the bearer token from the token into context. | "token" |
@@ -238,6 +238,7 @@ var ConfigDefault = Config{
238238
return c.Status(fiber.StatusUnauthorized).SendString("Invalid or expired API Key")
239239
},
240240
KeyLookup: "header:" + fiber.HeaderAuthorization,
241+
FallbackKeyLookups: []string{},
241242
AuthScheme: "Bearer",
242243
ContextKey: "token",
243244
}

middleware/keyauth/config.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ type Config struct {
3232
// - "cookie:<name>"
3333
KeyLookup string
3434

35-
// AdditionalKeyLookups is a slice of strings, containing secondary sources of keys if KeyLookup does not find one
35+
// FallbackKeyLookups is a slice of strings, containing secondary sources of keys if KeyLookup does not find one
3636
// Each element should be a value used in KeyLookup
37-
AdditionalKeyLookups []string
37+
FallbackKeyLookups []string
3838

3939
// AuthScheme to be used in the Authorization header.
4040
// Optional. Default value "Bearer".
@@ -88,8 +88,8 @@ func configDefault(config ...Config) Config {
8888
cfg.AuthScheme = ConfigDefault.AuthScheme
8989
}
9090
}
91-
if cfg.AdditionalKeyLookups == nil {
92-
cfg.AdditionalKeyLookups = []string{}
91+
if cfg.FallbackKeyLookups == nil {
92+
cfg.FallbackKeyLookups = []string{}
9393
}
9494
if cfg.Validator == nil {
9595
panic("fiber: keyauth middleware requires a validator function")

middleware/keyauth/keyauth.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ func New(config ...Config) fiber.Handler {
3434
if err != nil {
3535
panic(fmt.Errorf("error creating middleware: invalid keyauth Config.KeyLookup: %w", err))
3636
}
37-
if len(cfg.AdditionalKeyLookups) > 0 {
37+
if len(cfg.FallbackKeyLookups) > 0 {
3838
subExtractors := map[string]extractorFunc{cfg.KeyLookup: extractor}
39-
for _, keyLookup := range cfg.AdditionalKeyLookups {
39+
for _, keyLookup := range cfg.FallbackKeyLookups {
4040
subExtractors[keyLookup], err = parseSingleExtractor(keyLookup, cfg.AuthScheme)
4141
if err != nil {
42-
panic(fmt.Errorf("error creating middleware: invalid keyauth Config.AdditionalKeyLookups[%s]: %w", keyLookup, err))
42+
panic(fmt.Errorf("error creating middleware: invalid keyauth Config.FallbackKeyLookups[%s]: %w", keyLookup, err))
4343
}
4444
}
4545
extractor = func(c *fiber.Ctx) (string, error) {

middleware/keyauth/keyauth_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ func TestMultipleKeyLookup(t *testing.T) {
140140
// setup the fiber endpoint
141141
app := fiber.New()
142142
authMiddleware := New(Config{
143-
KeyLookup: "header:key",
144-
AdditionalKeyLookups: []string{"cookie:key", "query:key"},
143+
KeyLookup: "header:key",
144+
FallbackKeyLookups: []string{"cookie:key", "query:key"},
145145
Validator: func(c *fiber.Ctx, key string) (bool, error) {
146146
if key == CorrectKey {
147147
return true, nil

0 commit comments

Comments
 (0)