mirror of
https://github.com/labstack/echo.git
synced 2025-01-22 03:09:04 +02:00
412823eabb
Signed-off-by: Vishal Rana <vr@labstack.com>
55 lines
1.0 KiB
Markdown
55 lines
1.0 KiB
Markdown
+++
|
|
title = "Basic Auth Middleware"
|
|
description = "Basic auth middleware for Echo"
|
|
[menu.main]
|
|
name = "Basic Auth"
|
|
parent = "middleware"
|
|
weight = 5
|
|
+++
|
|
|
|
Basic auth middleware provides an HTTP basic authentication.
|
|
|
|
- For valid credentials it calls the next handler.
|
|
- For invalid credentials, it sends "401 - Unauthorized" response.
|
|
- For missing or invalid `Authorization` header, it sends "400 - Bad Request" response.
|
|
|
|
*Usage*
|
|
|
|
```go
|
|
e.Use(middleware.BasicAuth(func(username, password string) bool {
|
|
if username == "joe" && password == "secret" {
|
|
return true
|
|
}
|
|
return false
|
|
}))
|
|
```
|
|
|
|
## Custom Configuration
|
|
|
|
*Usage*
|
|
|
|
```go
|
|
e.Use(middleware.BasicAuthWithConfig(middleware.BasicAuthConfig{}}))
|
|
```
|
|
|
|
## Configuration
|
|
|
|
```go
|
|
BasicAuthConfig struct {
|
|
// Skipper defines a function to skip middleware.
|
|
Skipper Skipper
|
|
|
|
// Validator is a function to validate BasicAuth credentials.
|
|
// Required.
|
|
Validator BasicAuthValidator
|
|
}
|
|
```
|
|
|
|
*Default Configuration*
|
|
|
|
```go
|
|
DefaultBasicAuthConfig = BasicAuthConfig{
|
|
Skipper: defaultSkipper,
|
|
}
|
|
```
|