mirror of
https://github.com/Pacerino/CaddyProxyManager.git
synced 2026-08-04 11:15:08 -04:00
Introduce per-host plugin support: Caddy module discovery, module config endpoints, plugin schemas (basicauth, cloudflare) and the host plugins API. On the frontend, add the Plugins page, plugin/host plugin dialogs and schema-driven form fields.
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package schema
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func init() {
|
|
Register(&Schema{
|
|
ModuleID: "http.handlers.authentication",
|
|
Title: "HTTP Basic Authentication",
|
|
Description: "Protect this host with HTTP basic auth. Enter a plaintext password; CPM stores only the bcrypt hash Caddy needs.",
|
|
Scopes: []Scope{ScopeHost},
|
|
Fields: []Field{
|
|
{
|
|
Key: "username",
|
|
Label: "Username",
|
|
Type: FieldString,
|
|
Required: true,
|
|
},
|
|
{
|
|
Key: "password",
|
|
Label: "Password",
|
|
Description: "Plaintext password. It is bcrypt-hashed before being stored.",
|
|
Type: FieldSecret,
|
|
Required: true,
|
|
},
|
|
},
|
|
buildHandler: func(values map[string]any) (map[string]any, error) {
|
|
username, _ := values["username"].(string)
|
|
password, _ := values["password"].(string)
|
|
if username == "" || password == "" {
|
|
return nil, fmt.Errorf("username and password are required")
|
|
}
|
|
|
|
// Caddy's http_basic expects the account password as a
|
|
// base64-encoded bcrypt hash.
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
encoded := base64.StdEncoding.EncodeToString(hash)
|
|
|
|
return map[string]any{
|
|
"handler": "authentication",
|
|
"providers": map[string]any{
|
|
"http_basic": map[string]any{
|
|
"accounts": []any{
|
|
map[string]any{
|
|
"username": username,
|
|
"password": encoded,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}, nil
|
|
},
|
|
})
|
|
}
|