Documentation ¶
Index ¶
- Constants
- Variables
- func Assert(condition bool, message interface{})
- func Assertf(condition bool, format string, args ...interface{})
- func CreateSessionToken(uid int, macKey []byte) ([]byte, error)
- func HashAndSaltPassword(passwd, salt []byte) ([]byte, error)
- func Memcmp(a, b []byte) bool
- func ReadBody(req *http.Request) (body []byte, err error)
- func ValidateMAC(message, messageMAC, key []byte) bool
- type Config
- type Credentials
- type Server
- func (serv *Server) AuthRoute(res http.ResponseWriter, req *http.Request)
- func (serv *Server) Close() (e error)
- func (serv *Server) Config() Config
- func (serv *Server) GetRoute(res http.ResponseWriter, req *http.Request)
- func (serv *Server) InitDB() (err error)
- func (serv *Server) ListenAndServe() error
- func (serv *Server) LoginRoute(res http.ResponseWriter, req *http.Request)
- func (serv *Server) LoginUser(creds Credentials) (jwt []byte, err error)
- func (serv *Server) LookupRoute(res http.ResponseWriter, req *http.Request)
- func (serv *Server) LookupUser(user string) (uid int, err error)
- func (serv *Server) MsgFetch(yourName string, myUid int, since string) ([]byte, error)
- func (serv *Server) ParseChallenge(user string, response []byte) error
- func (serv *Server) RegisterRoute(res http.ResponseWriter, req *http.Request)
- func (serv *Server) RegisterUser(creds Credentials) (err error)
- func (serv *Server) ResetDB() (err error)
- func (serv *Server) SendMsg(message []byte, receiver string, sender int) (err error)
- func (serv *Server) SendRoute(res http.ResponseWriter, req *http.Request)
- func (serv *Server) ServeHTTP(res http.ResponseWriter, req *http.Request)
- func (serv *Server) SetChallenge(user string, challenge []byte) ([]byte, error)
- func (serv *Server) TestRoute(res http.ResponseWriter, req *http.Request)
- func (serv *Server) VerifyUser(req *http.Request) (Session, error)
- type Session
Constants ¶
const DefaultHttpAddr string = "0.0.0.0:80"
const DefaultHttpsAddr string = "0.0.0.0:443"
Variables ¶
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.") )
var ( KeyHashIterations int = 250000 KeyHashLength int = 32 SaltLength int = 16 UsernameMaxLength = 16 ChallengeLength = 16 )
var ( ErrNoSuchUser error = errors.New("No such user") ErrMsgUnsent error = errors.New("Failed to send message") ErrInvalidResponse error = errors.New("Invalid response to challenge") )
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.") )
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 CreateSessionToken ¶
CreateSessionToken creates a JWT token that is sent to the client at login to represent a session.
func HashAndSaltPassword ¶
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 ¶
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 ValidateMAC ¶
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 ¶
LoadConfig loads a toml-formatted configuration file at the location confPath, and returns a new Config structure to represent it.
type Credentials ¶
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 ¶
NewServer creates a new Server structure, with the configuration specified in a toml configuration file at location confPath.
func NewServerWithConf ¶
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) Config ¶
Config returns a copy of the underlying Config structure for a particular Server instance.
func (*Server) ListenAndServe ¶
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) ParseChallenge ¶
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) 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 ¶
type Session ¶
Session represents session data stored in a JWT. Session only contains the uid of the currently logged-in user.
func UnwrapSessionToken ¶
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.