gopgext/auth

import "gopgext/auth"

Package auth hooks into client authentication and password changes: to delay or reject connections (like auth_delay), or enforce password rules (like passwordcheck).

Callbacks are registered while the library loads (in main):

func main() {
	auth.OnPasswordCheck(func(p *auth.Password) error {
		if p.Type == auth.PasswordPlain && len(p.Password) < 12 {
			return pg.Errorf(pg.CodeInvalidParameterValue, "password is too short")
		}
		return nil
	})
}

OnAuthenticate needs the library in shared_preload_libraries (it runs before session_preload_libraries are loaded); OnPasswordCheck works once the library is loaded in the session (LOAD, session_preload_libraries…).

Index#

Functions#

func OnAuthenticate#

func OnAuthenticate(f func(c *Connection) error)

OnAuthenticate registers f, called after each client’s authentication (ClientAuthentication_hook), successful or not. An error f returns rejects the connection (as a FATAL error). It runs before the backend connects to its database: there is no transaction, so f can’t run SQL. Call it while the library loads, in shared_preload_libraries.

func OnPasswordCheck#

func OnPasswordCheck(f func(p *Password) error)

OnPasswordCheck registers f, called when CREATE ROLE or ALTER ROLE sets a password (check_password_hook). An error f returns rejects it. Call it while the library loads.

Types#

type Connection#

type Connection struct {
	// User and Database are the role and database the client asked for;
	// Application is its application_name ("" if not given).
	User, Database, Application string
	// RemoteHost is the client's address ("[local]" for a Unix socket) and
	// RemotePort its port ("" for a Unix socket).
	RemoteHost, RemotePort string
	// Method is the authentication method of the pg_hba.conf line used
	// ("trust", "scram-sha-256", "md5", "password"...).
	Method string
	Status Status
}

Connection is a client connection being authenticated.

type Password#

type Password struct {
	Role string
	// Password is as given: plain text or encrypted (see Type). Clients
	// like psql's \password encrypt it before sending it.
	Password string
	Type     PasswordType
	// ValidUntil is the VALID UNTIL time; nil if not given or infinity.
	ValidUntil *time.Time
}

Password is a password set by CREATE ROLE or ALTER ROLE.

func (*Password) Matches#

func (p *Password) Matches(plain string) bool

Matches reports whether plain is the password, plain or encrypted (PostgreSQL’s plain_crypt_verify): to check an encrypted password against guesses.

type PasswordType#

type PasswordType int

PasswordType is the form a password is given in.

const (
	// PasswordPlain is a plain text password.
	PasswordPlain PasswordType = iota
	// PasswordMD5 is an MD5-encrypted password ("md5...").
	PasswordMD5
	// PasswordSCRAM is a SCRAM-SHA-256 verifier ("SCRAM-SHA-256$...").
	PasswordSCRAM
)

type Status#

type Status int

Status is the outcome of a client’s authentication.

const (
	// StatusOK is a successful authentication.
	StatusOK Status = iota
	// StatusError is a failed one (wrong password, no matching pg_hba.conf
	// line...).
	StatusError
	// StatusEOF is a client that disconnected while asked for a password
	// (as psql does before prompting for one).
	StatusEOF
)