diff --git a/README.md b/README.md index 45bb528..7dbd9e0 100644 --- a/README.md +++ b/README.md @@ -105,3 +105,13 @@ Each JSON configuration file for the gateways can specify detailed settings for ] } ``` + +## Authentication +Basic authentication can be enabled using the `--basic-auth` flag. The username and password should be set through environment variables `GATEWAY_USERNAME` and `GATEWAY_PASSWORD`, respectively. + +### Running the Application +To run the application with authentication: + +``` +DEBUG=true GATEWAY_USERNAME=myuser GATEWAY_PASSWORD=mypass go run . --config config.json --basic-auth +``` diff --git a/main.go b/main.go index a9c28f3..470e544 100644 --- a/main.go +++ b/main.go @@ -58,6 +58,11 @@ func main() { Usage: "Load configuration from environment variable named GATEWAY_CONFIG.", Value: false, }, + &cli.BoolFlag{ + Name: "basic-auth", + Usage: "Enable basic authentication.", + Value: false, + }, }, Action: func(cc *cli.Context) error { configPath := resolveConfigPath(cc.String("config"), cc.Bool("env")) @@ -73,6 +78,18 @@ func main() { r.Use(httplog.RequestLogger(logger)) r.Use(middleware.Recoverer) r.Use(middleware.Heartbeat("/health")) + // Add basic auth middleware + if cc.Bool("basic-auth") { + username := os.Getenv("GATEWAY_USERNAME") + password := os.Getenv("GATEWAY_PASSWORD") + if username == "" || password == "" { + return errors.New("both GATEWAY_USERNAME and GATEWAY_PASSWORD environment variables must be set for basic authentication") + } + r.Use(middleware.BasicAuth("API Realm", map[string]string{ + username: password, + })) + } + server := &http.Server{ Addr: fmt.Sprintf(":%d", config.Port), Handler: r,