Skip to content

Commit

Permalink
feat(server): add optional basic auth feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewhrit committed Jul 22, 2024
1 parent 6840957 commit b66a7be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Cfg struct {
// Web
Headless bool `env:"HEADLESS" envDefault:"false" json:"headless"` // Enable website
Analytics string `env:"ANALYTICS" envDefault:"" json:"analytics"` // <script> tag for analytics (leave blank to disable)
Username string `env:"USERNAME" envDefault:"" json:"username"` // Basic Auth username. Required to enable Basic Auth
Password string `env:"PASSWORD" envDefault:"" json:"password"` // Basic Auth password. Required to enable Basic Auth

// Document
IDLength int `env:"ID_LENGTH" envDefault:"8" json:"id_length"`
Expand Down
7 changes: 7 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ func (s *Server) MountMiddleware() {
AllowCredentials: false,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))

// Basic Auth
if s.Config.Username != "" && s.Config.Password != "" {
s.Router.Use(middleware.BasicAuth("spacebin", map[string]string{
s.Config.Username: s.Config.Password,
}))
}
}

func (s *Server) RegisterHeaders() {
Expand Down

0 comments on commit b66a7be

Please sign in to comment.