Added connect with md5 password
This commit is contained in:
@@ -6,4 +6,12 @@ Experimental PostgreSQL client library for Go
|
|||||||
Testing
|
Testing
|
||||||
-------
|
-------
|
||||||
|
|
||||||
To setup the test environment run the test_setup.sql script as a user that can create users and databases.
|
To setup the test environment run the test_setup.sql script as a user that can
|
||||||
|
create users and databases. To successfully run the connection tests for various
|
||||||
|
means of authentication you must include the following in your pg_hba.conf.
|
||||||
|
|
||||||
|
local pgx_test pgx_none trust
|
||||||
|
local pgx_test pgx_pw password
|
||||||
|
local pgx_test pgx_md5 md5
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package pqx
|
package pqx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/md5"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -189,6 +191,10 @@ func (c *conn) rxAuthenticationX(r *messageReader) (err error) {
|
|||||||
case 0: // AuthenticationOk
|
case 0: // AuthenticationOk
|
||||||
case 3: // AuthenticationCleartextPassword
|
case 3: // AuthenticationCleartextPassword
|
||||||
c.txPasswordMessage(c.options["password"])
|
c.txPasswordMessage(c.options["password"])
|
||||||
|
case 5: // AuthenticationMD5Password
|
||||||
|
salt := r.readByteString(4)
|
||||||
|
digestedPassword := "md5" + hexMD5(hexMD5(c.options["password"]+c.options["user"])+salt)
|
||||||
|
c.txPasswordMessage(digestedPassword)
|
||||||
default:
|
default:
|
||||||
err = errors.New("Received unknown authentication message")
|
err = errors.New("Received unknown authentication message")
|
||||||
}
|
}
|
||||||
@@ -196,6 +202,12 @@ func (c *conn) rxAuthenticationX(r *messageReader) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hexMD5(s string) string {
|
||||||
|
hash := md5.New()
|
||||||
|
io.WriteString(hash, s)
|
||||||
|
return hex.EncodeToString(hash.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
func (c *conn) rxParameterStatus(r *messageReader) {
|
func (c *conn) rxParameterStatus(r *messageReader) {
|
||||||
key := r.readString()
|
key := r.readString()
|
||||||
value := r.readString()
|
value := r.readString()
|
||||||
|
|||||||
+13
-1
@@ -47,7 +47,7 @@ func TestConnectWithInvalidUser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectWithPassword(t *testing.T) {
|
func TestConnectWithPlainTextPassword(t *testing.T) {
|
||||||
conn, err := Connect(map[string]string{"socket": "/private/tmp/.s.PGSQL.5432", "user": "pgx_pw", "password": "secret", "database": "pgx_test"})
|
conn, err := Connect(map[string]string{"socket": "/private/tmp/.s.PGSQL.5432", "user": "pgx_pw", "password": "secret", "database": "pgx_test"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection: " + err.Error())
|
t.Fatal("Unable to establish connection: " + err.Error())
|
||||||
@@ -59,6 +59,18 @@ func TestConnectWithPassword(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConnectWithMD5Password(t *testing.T) {
|
||||||
|
conn, err := Connect(map[string]string{"socket": "/private/tmp/.s.PGSQL.5432", "user": "pgx_md5", "password": "secret", "database": "pgx_test"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Unable to establish connection: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
err = conn.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Unable to close connection")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestQuery(t *testing.T) {
|
func TestQuery(t *testing.T) {
|
||||||
conn, err := Connect(map[string]string{"socket": "/private/tmp/.s.PGSQL.5432", "user": "pgx_none", "database": "pgx_test"})
|
conn, err := Connect(map[string]string{"socket": "/private/tmp/.s.PGSQL.5432", "user": "pgx_none", "database": "pgx_test"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
drop database if exists pgx_test;
|
drop database if exists pgx_test;
|
||||||
drop user if exists pgx_none;
|
drop user if exists pgx_none;
|
||||||
drop user if exists pgx_pw;
|
drop user if exists pgx_pw;
|
||||||
|
drop user if exists pgx_md5;
|
||||||
|
|
||||||
create user pgx_none;
|
create user pgx_none;
|
||||||
create user pgx_pw password 'secret';
|
create user pgx_pw password 'secret';
|
||||||
|
create user pgx_md5 password 'secret';
|
||||||
create database pgx_test;
|
create database pgx_test;
|
||||||
|
|||||||
Reference in New Issue
Block a user