Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
update Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
DavZim committed Dec 23, 2022
1 parent f6c3c4c commit 279f4b7
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,24 @@ import (
"github.com/gofiber/keyauth/v2"
)

var (
APIKey = "correct horse battery staple"
)

func validateAPIKey(c *fiber.Ctx, key string) (bool, error) {
if key == c.Locals("ContextKey") {
if key == APIKey {
return true, nil
}
return false, keyauth.ErrMissingOrMalformedAPIKey
}

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


// note that the keyauth middleware needs to be defined before the routes are defined!
app.Use(keyauth.New(keyauth.Config{
KeyLookup: "cookie:access_token",
Validator: validateAPIKey,
ContextKey: "my-super-secret-key-123"
}))

app.Get("/", func(c *fiber.Ctx) error {
Expand All @@ -53,7 +57,7 @@ func main() {
curl http://localhost:3000
#> missing or malformed API Key

curl --cookie "access_token=my-super-secret-key-123" http://localhost:3000
curl --cookie "access_token=correct horse battery staple" http://localhost:3000
#> Successfully authenticated!

curl --cookie "access_token=Clearly A Wrong Key" http://localhost:3000
Expand All @@ -74,9 +78,12 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/keyauth/v2"
)
var (
APIKey = "correct horse battery staple"
)

func validateAPIKey(c *fiber.Ctx, key string) (bool, error) {
if key == c.Locals("ContextKey") {
if key == APIKey {
return true, nil
}
return false, keyauth.ErrMissingOrMalformedAPIKey
Expand All @@ -95,7 +102,6 @@ func main() {
Filter: authFilter,
KeyLookup: "cookie:access_token",
Validator: validateAPIKey,
ContextKey: "my-super-secret-key-123"
}))

app.Get("/", func(c *fiber.Ctx) error {
Expand All @@ -120,10 +126,10 @@ curl http://localhost:3000
#> Welcome

# /authenticated needs to be authenticated
curl --cookie "access_token=my-super-secret-key-123" http://localhost:3000/authenticated
curl --cookie "access_token=correct horse battery staple" http://localhost:3000/authenticated
#> Successfully authenticated!

# /auth2 needs to be authenticated too
curl --cookie "access_token=my-super-secret-key-123" http://localhost:3000/auth2
curl --cookie "access_token=correct horse battery staple" http://localhost:3000/auth2
#> Successfully authenticated 2!
```

0 comments on commit 279f4b7

Please sign in to comment.