mirror of
https://github.com/labstack/echo.git
synced 2026-05-16 09:48:24 +02:00
412823eabb
Signed-off-by: Vishal Rana <vr@labstack.com>
70 lines
1.4 KiB
Markdown
70 lines
1.4 KiB
Markdown
+++
|
|
title = "Key Auth Middleware"
|
|
description = "Key auth middleware for Echo"
|
|
[menu.main]
|
|
name = "Key Auth"
|
|
parent = "middleware"
|
|
weight = 5
|
|
+++
|
|
|
|
Key auth middleware provides a key based authentication.
|
|
|
|
- For valid key it calls the next handler.
|
|
- For invalid key, it sends "401 - Unauthorized" response.
|
|
- For missing key, it sends "400 - Bad Request" response.
|
|
|
|
*Usage*
|
|
|
|
```go
|
|
e.Use(middleware.KeyAuth(func(key string) bool {
|
|
return key == "valid-key"
|
|
}))
|
|
```
|
|
|
|
## Custom Configuration
|
|
|
|
*Usage*
|
|
|
|
```go
|
|
e := echo.New()
|
|
e.Use(middleware.KeyAuthWithConfig(middleware.KeyAuthConfig{
|
|
KeyLookup: "query:api-key",
|
|
}))
|
|
```
|
|
|
|
## Configuration
|
|
|
|
```go
|
|
// KeyAuthConfig defines the config for KeyAuth middleware.
|
|
KeyAuthConfig struct {
|
|
// Skipper defines a function to skip middleware.
|
|
Skipper Skipper
|
|
|
|
// KeyLookup is a string in the form of "<source>:<name>" that is used
|
|
// to extract key from the request.
|
|
// Optional. Default value "header:Authorization".
|
|
// Possible values:
|
|
// - "header:<name>"
|
|
// - "query:<name>"
|
|
KeyLookup string `json:"key_lookup"`
|
|
|
|
// AuthScheme to be used in the Authorization header.
|
|
// Optional. Default value "Bearer".
|
|
AuthScheme string
|
|
|
|
// Validator is a function to validate key.
|
|
// Required.
|
|
Validator KeyAuthValidator
|
|
}
|
|
```
|
|
|
|
*Default Configuration*
|
|
|
|
```go
|
|
DefaultKeyAuthConfig = KeyAuthConfig{
|
|
Skipper: defaultSkipper,
|
|
KeyLookup: "header:" + echo.HeaderAuthorization,
|
|
AuthScheme: "Bearer",
|
|
}
|
|
```
|