Skip to content

Commit b66a7be

Browse files
committed
feat(server): add optional basic auth feature
1 parent 6840957 commit b66a7be

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

internal/config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type Cfg struct {
3131
// Web
3232
Headless bool `env:"HEADLESS" envDefault:"false" json:"headless"` // Enable website
3333
Analytics string `env:"ANALYTICS" envDefault:"" json:"analytics"` // <script> tag for analytics (leave blank to disable)
34+
Username string `env:"USERNAME" envDefault:"" json:"username"` // Basic Auth username. Required to enable Basic Auth
35+
Password string `env:"PASSWORD" envDefault:"" json:"password"` // Basic Auth password. Required to enable Basic Auth
3436

3537
// Document
3638
IDLength int `env:"ID_LENGTH" envDefault:"8" json:"id_length"`

internal/server/server.go

+7
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ func (s *Server) MountMiddleware() {
107107
AllowCredentials: false,
108108
MaxAge: 300, // Maximum value not ignored by any of major browsers
109109
}))
110+
111+
// Basic Auth
112+
if s.Config.Username != "" && s.Config.Password != "" {
113+
s.Router.Use(middleware.BasicAuth("spacebin", map[string]string{
114+
s.Config.Username: s.Config.Password,
115+
}))
116+
}
110117
}
111118

112119
func (s *Server) RegisterHeaders() {

0 commit comments

Comments
 (0)