https://github.com/iwpnd/fiber-key-auth
fiber api key authentication middleware
https://github.com/iwpnd/fiber-key-auth
api gofiber golang security
Last synced: 7 months ago
JSON representation
fiber api key authentication middleware
- Host: GitHub
- URL: https://github.com/iwpnd/fiber-key-auth
- Owner: iwpnd
- Created: 2021-10-02T13:24:34.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-05-04T14:06:52.000Z (over 1 year ago)
- Last Synced: 2025-03-15T15:39:39.343Z (7 months ago)
- Topics: api, gofiber, golang, security
- Language: Go
- Homepage:
- Size: 1.91 MB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# About
On deployment inject API keys authorized to use your service. Every call to a private
endpoint of your service has to include a `header['x-api-key']` attribute that is
validated against the API keys (starting with: `API_KEY_`) in your environment.
If it is present, a request is authorized. If it is not fiber returns `401 Unauthorized`.
Use this either as a middleware the usage.## Built With
- [fiber](https://github.com/gofiber/fiber/v2)
## Getting Started
### Installation
```sh
go get github.com/iwpnd/fiber-key-auth
```## Usage
As Middleware:
```go
package mainimport (
"os""github.com/iwpnd/fiber-key-auth"
"github.com/gofiber/fiber/v2"
)os.Setenv("API_KEY_TEST", "valid")
func main() {
app := fiber.New()app.Use(keyauth.New())
// or optionally with structured error message
// app.Use(keyauth.New(WithStructuredErrorMsg()))app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World 👋!")
})app.Listen(":3000")
}
```Now try to access your `/` route.
```bash
curl localhost:3000>> "no api key" # or {"message": "no api key"}
curl localhost:3000 -H "x-api-key: invalid"
>> "invalid api key" # or {"message": "invalid api key"}
curl localhost:3000 -H "x-api-key: valid"
>> "Hello, World 👋!"
```## License
Distributed under the MIT License. See `LICENSE` for more information.
## Contact
Benjamin Ramser - [@imwithpanda](https://twitter.com/imwithpanda) - ahoi@iwpnd.pw
Project Link: [https://github.com/iwpnd/fiber-key-auth](https://github.com/iwpnd/fiber-key-auth)