Implement HTTP basic authentication

This commit is contained in:
Dan Sosedoff 2014-10-29 19:45:12 -05:00
parent ad0bc43eeb
commit 44b429b459
2 changed files with 20 additions and 10 deletions

View File

@ -68,16 +68,18 @@ Usage:
pgweb [OPTIONS]
Application Options:
-v, --version Print version
-d, --debug Enable debugging mode (false)
--url= Database connection string
--host= Server hostname or IP (localhost)
--port= Server port (5432)
--user= Database user (postgres)
--pass= Password for user
--db= Database name (postgres)
--ssl= SSL option (disable)
--listen= HTTP server listen port (8080)
-v, --version Print version
-d, --debug Enable debugging mode (false)
--url= Database connection string
--host= Server hostname or IP (localhost)
--port= Server port (5432)
--user= Database user (postgres)
--pass= Password for user
--db= Database name (postgres)
--ssl= SSL option (disable)
--listen= HTTP server listen port (8080)
--auth-user= HTTP basic auth user
--auth-pass= HTTP basic auth password
```
## Compile from source

View File

@ -23,6 +23,8 @@ var options struct {
DbName string `long:"db" description:"Database name" default:"postgres"`
Ssl string `long:"ssl" description:"SSL option" default:"disable"`
HttpPort uint `long:"listen" description:"HTTP server listen port" default:"8080"`
AuthUser string `long:"auth-user" description:"HTTP basic auth user"`
AuthPass string `long:"auth-pass" description:"HTTP basic auth password"`
}
var dbClient *Client
@ -96,6 +98,12 @@ func initOptions() {
func startServer() {
router := gin.Default()
// Enable HTTP basic authentication only if both user and password are set
if options.AuthUser != "" && options.AuthPass != "" {
auth := map[string]string{options.AuthUser: options.AuthPass}
router.Use(gin.BasicAuth(auth))
}
router.GET("/", API_Home)
router.GET("/databases", API_GetDatabases)
router.GET("/info", API_Info)