server

package
v0.0.0-...-ebab861 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2019 License: Zlib Imports: 20 Imported by: 1

Documentation

Index

Constants

View Source
const DefaultHttpAddr string = "0.0.0.0:80"
View Source
const DefaultHttpsAddr string = "0.0.0.0:443"

Variables

View Source
var (
	ErrInvalidCredentials error = errors.New("Invalid username or password.")
	ErrUsernameTaken      error = errors.New("Username already taken.")
	ErrRegistrationFailed error = errors.New("Failed to register user.")
	ErrNotLoggedIn        error = errors.New("Please log in.")
)
View Source
var (
	KeyHashIterations int = 250000
	KeyHashLength     int = 32
	SaltLength        int = 16
	UsernameMaxLength     = 16
	ChallengeLength       = 16
)
View Source
var (
	ErrNoSuchUser      error = errors.New("No such user")
	ErrMsgUnsent       error = errors.New("Failed to send message")
	ErrInvalidResponse error = errors.New("Invalid response to challenge")
)
View Source
var (
	InvalidMACError   error = errors.New("Computed MAC does not match the provided MAC")
	MalformedJWTError error = errors.New("The JWT is missing fields or corrupt")
	ErrSessionExpired error = errors.New("Session expired.")
)
View Source
var (
	KeyHashAlgo func() hash.Hash = sha256.New
)

Functions

func Assert

func Assert(condition bool, message interface{})

Assert checks if condition is false, and exits the program if it is. Assert logs message to the standard logger before exiting if the assertion fails.

func Assertf

func Assertf(condition bool, format string, args ...interface{})

Assertf is equivalent to Assert, but with a format string

func CreateSessionToken

func CreateSessionToken(uid int, macKey []byte) ([]byte, error)

CreateSessionToken creates a JWT token that is sent to the client at login to represent a session.

func HashAndSaltPassword

func HashAndSaltPassword(passwd, salt []byte) ([]byte, error)

HashAndSaltPassword takes a password and salt, and creates a hash of the password concatenated with the salt using pbkdf2.

The number of iterations is defined by KeyHashIterations. The hash function used is defined by KeyHashAlgo. The length of the key that is created is defined by KeyHashLength.

func Memcmp

func Memcmp(a, b []byte) bool

Memcmp returns true if the first n bytes of two slices are equal and false otherwise, where n is Min(len(a), len(b)).

func ReadBody

func ReadBody(req *http.Request) (body []byte, err error)

ReadBody reads the body of an http.Request into a []byte

func ValidateMAC

func ValidateMAC(message, messageMAC, key []byte) bool

ValidateMAC computes a SHA256 HMAC tag for message, and compares it with messageMAC. The the tags match, ValidateMAC returns true, else it returns false.

Types

type Config

type Config struct {
	HttpAddr  string
	HttpsAddr string
	CertFile  string
	KeyFile   string
	LogFile   string
	SQLUser   string
	SQLPass   string
	SQLDb     string
}

Config contains configuration data for use by Server.

func LoadConfig

func LoadConfig(confPath string) (Config, error)

LoadConfig loads a toml-formatted configuration file at the location confPath, and returns a new Config structure to represent it.

type Credentials

type Credentials struct {
	Username string
	Password string
}

Credentials represents a userername and password combination

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a type that represents a Mercury Chat Server.

func NewServer

func NewServer(confPath string) (*Server, error)

NewServer creates a new Server structure, with the configuration specified in a toml configuration file at location confPath.

func NewServerWithConf

func NewServerWithConf(conf Config) (*Server, error)

NewServerWithConf creates a new Server structure using the settings defined by the Config structure.

func (*Server) AuthRoute

func (serv *Server) AuthRoute(res http.ResponseWriter, req *http.Request)

func (*Server) Close

func (serv *Server) Close() (e error)

Closes resources allocated by the server

func (*Server) Config

func (serv *Server) Config() Config

Config returns a copy of the underlying Config structure for a particular Server instance.

func (*Server) GetRoute

func (serv *Server) GetRoute(res http.ResponseWriter, req *http.Request)

func (*Server) InitDB

func (serv *Server) InitDB() (err error)

func (*Server) ListenAndServe

func (serv *Server) ListenAndServe() error

ListenAndServe is similar to go's http.ListenAndServe and https.ListenAndServeTLS functions. This starts the Mercury Server, and handles incoming connections. This is a blocking function, and should be started as a goroutine if it needs to run in the background. If it fails to bind one of the sockets, it will return with an error.

func (*Server) LoginRoute

func (serv *Server) LoginRoute(res http.ResponseWriter, req *http.Request)

func (*Server) LoginUser

func (serv *Server) LoginUser(creds Credentials) (jwt []byte, err error)

LoginUser creates a JWT session token if the credentials creds are valid

func (*Server) LookupRoute

func (serv *Server) LookupRoute(res http.ResponseWriter, req *http.Request)

func (*Server) LookupUser

func (serv *Server) LookupUser(user string) (uid int, err error)

func (*Server) MsgFetch

func (serv *Server) MsgFetch(yourName string, myUid int, since string) ([]byte, error)

func (*Server) ParseChallenge

func (serv *Server) ParseChallenge(user string, response []byte) error

func (*Server) RegisterRoute

func (serv *Server) RegisterRoute(res http.ResponseWriter, req *http.Request)

func (*Server) RegisterUser

func (serv *Server) RegisterUser(creds Credentials) (err error)

RegisterUser attempts to creates a user in the with the credentials creds

func (*Server) ResetDB

func (serv *Server) ResetDB() (err error)

func (*Server) SendMsg

func (serv *Server) SendMsg(message []byte, receiver string, sender int) (err error)

func (*Server) SendRoute

func (serv *Server) SendRoute(res http.ResponseWriter, req *http.Request)

func (*Server) ServeHTTP

func (serv *Server) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP generates an HTTP response to an HTTP request. See the go http.Handler interface for more information.

func (*Server) SetChallenge

func (serv *Server) SetChallenge(user string, challenge []byte) ([]byte, error)

func (*Server) TestRoute

func (serv *Server) TestRoute(res http.ResponseWriter, req *http.Request)

func (*Server) VerifyUser

func (serv *Server) VerifyUser(req *http.Request) (Session, error)

VerifyUser extracts the Session data from a request.

type Session

type Session struct {
	Uid     int
	Expires []byte
}

Session represents session data stored in a JWT. Session only contains the uid of the currently logged-in user.

func UnwrapSessionToken

func UnwrapSessionToken(jwt, macKey []byte) (Session, error)

UnwrapSessionToken verifies a JWT, and returns its payload if the integrity check passes. The session token's payload simply contains the uid of the currently logged in user.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL