changeset 415:405bdb9c6a77

Fix for wamos/issue96 (Login Behavior: names with spaces don't work)
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 15 Aug 2018 18:44:48 +0200
parents c1047fd04a3a
children e5c07bce28d7
files pkg/auth/opendb.go
diffstat 1 files changed, 16 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/auth/opendb.go	Wed Aug 15 17:30:50 2018 +0200
+++ b/pkg/auth/opendb.go	Wed Aug 15 18:44:48 2018 +0200
@@ -2,35 +2,29 @@
 
 import (
 	"database/sql"
-	"fmt"
-	"strings"
+
+	"github.com/jackc/pgx"
+	"github.com/jackc/pgx/stdlib"
 
 	"gemma.intevation.de/gemma/pkg/config"
-
-	_ "github.com/jackc/pgx/stdlib"
 )
 
-const driver = "pgx"
+func OpenDB(user, password string) (*sql.DB, error) {
 
-// dbQuote quotes strings to be able to contain whitespace
-// and backslashes in database DSN strings.
-var dbQuote = strings.NewReplacer(`\`, `\\`, `'`, `\'`).Replace
+	// To ease SSL config ride a bit on parsing.
+	cc, err := pgx.ParseConnectionString("sslmode=" + config.DBSSLMode())
+	if err != nil {
+		return nil, err
+	}
 
-// dbDSN creates a data source name suitable for sql.Open on
-// PostgreSQL databases.
-func dbDSN(host string, port uint, dbname, user, password string, sslmode string) string {
-	return fmt.Sprintf("host=%s port=%d dbname=%s user=%s password=%s sslmode=%s",
-		dbQuote(host), port, dbQuote(dbname),
-		dbQuote(user), dbQuote(password), sslmode)
-}
+	// Do the rest manually to allow whitespace in user/password.
+	cc.Host = config.DBHost()
+	cc.Port = uint16(config.DBPort())
+	cc.User = user
+	cc.Password = password
+	cc.Database = config.DBName()
 
-func OpenDB(user, password string) (*sql.DB, error) {
-	dsn := dbDSN(
-		config.DBHost(), config.DBPort(),
-		config.DBName(),
-		user, password,
-		config.DBSSLMode())
-	return sql.Open(driver, dsn)
+	return stdlib.OpenDB(cc), nil
 }
 
 const allRoles = `