Skip to content

Commit 3e7faf6

Browse files
authored
Merge pull request #13 from sygmaprotocol/mmuftic/basic-auth
feature: add basic auth
2 parents 266f82f + 2a293c5 commit 3e7faf6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Diff for: README.md

+10
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,13 @@ Each JSON configuration file for the gateways can specify detailed settings for
105105
]
106106
}
107107
```
108+
109+
## Authentication
110+
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.
111+
112+
### Running the Application
113+
To run the application with authentication:
114+
115+
```
116+
DEBUG=true GATEWAY_USERNAME=myuser GATEWAY_PASSWORD=mypass go run . --config config.json --basic-auth
117+
```

Diff for: main.go

+17
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ func main() {
5858
Usage: "Load configuration from environment variable named GATEWAY_CONFIG.",
5959
Value: false,
6060
},
61+
&cli.BoolFlag{
62+
Name: "basic-auth",
63+
Usage: "Enable basic authentication.",
64+
Value: false,
65+
},
6166
},
6267
Action: func(cc *cli.Context) error {
6368
configPath := resolveConfigPath(cc.String("config"), cc.Bool("env"))
@@ -73,6 +78,18 @@ func main() {
7378
r.Use(httplog.RequestLogger(logger))
7479
r.Use(middleware.Recoverer)
7580
r.Use(middleware.Heartbeat("/health"))
81+
// Add basic auth middleware
82+
if cc.Bool("basic-auth") {
83+
username := os.Getenv("GATEWAY_USERNAME")
84+
password := os.Getenv("GATEWAY_PASSWORD")
85+
if username == "" || password == "" {
86+
return errors.New("both GATEWAY_USERNAME and GATEWAY_PASSWORD environment variables must be set for basic authentication")
87+
}
88+
r.Use(middleware.BasicAuth("API Realm", map[string]string{
89+
username: password,
90+
}))
91+
}
92+
7693
server := &http.Server{
7794
Addr: fmt.Sprintf(":%d", config.Port),
7895
Handler: r,

0 commit comments

Comments
 (0)