eauth

package module
v0.0.0-...-42b2d81 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2014 License: MIT Imports: 16 Imported by: 0

README

EAuth

Poor man's OAuth

Death to passwords.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = &DefaultSender{}

Default is an initialized instance of the DefaultSender No need to place this in init, there's no initialization behavior needed

Functions

func CreateLink(c Config, user User) string

Links can be reused within the expiration, but I'm okay with that

func IsValid

func IsValid(c Config, user User, then time.Time, given string) bool

func IsValidSession

func IsValidSession(m SessionManager, key string) bool

IsValidSession checks if a session key exists in the given manager.

func RandomKey

func RandomKey() (string, error)

For 144 bit sessions, we'll need to generate 18 random bytes. These will be encoded in URL safe base 64, for a length of 24 chars.

func SaltedHMAC

func SaltedHMAC(salt, secret, data []byte) []byte

The validity of the reset link is determined by * The HMAC using the server's secret key * The unix timestamp in the message body * That the associated session does not have an assigned IP

func Send

func Send(c SMTPConfig, to []string, subject, body string) error

Send will send an email using the default Sender implementation.

func SetCookie

func SetCookie(w http.ResponseWriter, config CookieConfig, session Session)

Include the cookie on the response The cookie's name is taken from the cookie configuration and its value is the given session key.

Types

type Addresses

type Addresses []string

func (Addresses) String

func (a Addresses) String() string

type Config

type Config struct {
	Domain    string         `json:"domain"`
	Secret    string         `json:"secret"`
	Https     bool           `json:"https"`
	Cookie    CookieConfig   `json:"cookie"`
	SMTP      SMTPConfig     `json:"smtp"`
	Databases DatabaseConfig `json:"database"`
}

TODO A mechanism should be in place to rotate secret keys TODO Multiple domains?

func Parse

func Parse() (Config, error)

By default, the parser will look for a file called settings.json in current directory.

func ParseFile

func ParseFile(filename string) (Config, error)

type CookieConfig

type CookieConfig struct {
	Age      time.Duration `json:"age"`
	Domain   string        `json:"domain"`
	HttpOnly bool          `json:"http_only"`
	Name     string        `json:"name"`
	Path     string        `json:"path"`
	Secure   bool          `json:"secure"`
}

Cookie names are valid tokens as defined by RFC 2616 section 2.2: http://tools.ietf.org/html/rfc2616#section-2.2 TL;DR: Any non-control or non-separator character.

type DatabaseConfig

type DatabaseConfig struct {
	Driver   string `json:"driver"`
	Host     string `json:"host"`
	Port     int64  `json:"port"`
	Name     string `json:"name"`
	User     string `json:"user"`
	Password string `json:"password"`
}

func (DatabaseConfig) Credentials

func (db DatabaseConfig) Credentials() string

Return a string of credentials approriate for Go's sql.Open() func

type DefaultSender

type DefaultSender struct{}

DefaultSender implements the Email Sender interface

func (*DefaultSender) Send

func (ds *DefaultSender) Send(c SMTPConfig, to Addresses, subject, body string) error

Send will send an email on the DefaultSender

type Email

type Email struct {
	From    string
	To      Addresses
	Subject string
	Header  map[string]string
	Body    string
}

TODO Use the Values type for Header?

func (Email) String

func (email Email) String() string

TODO Prevent email lines from being over 78 characters?

type KeyFunc

type KeyFunc func() (string, error)

type SMTPConfig

type SMTPConfig struct {
	Port     int64  `json:"port"`
	User     string `json:"user"`
	Password string `json:"password"`
	Host     string `json:"host"`
	From     string `json:"from"`
	Alias    string `json:"alias"`
}

func (SMTPConfig) FromAddress

func (c SMTPConfig) FromAddress() string

func (SMTPConfig) HostWithPort

func (c SMTPConfig) HostWithPort() string

type Sender

type Sender interface {
	Send(c SMTPConfig, to []string, subject, body string) error
}

type Server

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

func NewServer

func NewServer(c Config, u UserManager, s SessionManager) *Server

func (*Server) Authenticate

func (s *Server) Authenticate(w http.ResponseWriter, r *http.Request)

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

func (*Server) LoginRequired

func (s *Server) LoginRequired(h http.HandlerFunc) http.HandlerFunc

func (*Server) SendEmail

func (s *Server) SendEmail(w http.ResponseWriter, r *http.Request)

Send email will send a auth link to the given email address if the given POST data contains a valid user email.

type Session

type Session struct {
	Key     string
	UserId  int64
	IP      string
	Expires time.Time
}

Includes options for making sessions even more secure: * Single sessions per user * IP address fixation Session does not include data.

func NewSession

func NewSession(m SessionManager, uid int64, c CookieConfig) (Session, error)

Session keys become the cookie's value. US-ASCII is safe except for control characters, commas, semicolons and backslash. URL-encoded base64 is safe and is used here.

type SessionManager

type SessionManager interface {
	Save(session Session) error
	Delete(key string) error
	Get(key string) Session
}

SessionManager is the persistance interface for sessions.

type User

type User struct {
	Id    int64
	Email string
	Token string
}

User is the server's user struct. Sessions are attached to users. Id is included so urls do not need to include the email in the link url. Tokens are refreshed everytime a new session is created in order to prevent replay attacks with the given link URL.

func (User) String

func (u User) String() string

String returns a string representation of the user id and email

type UserManager

type UserManager interface {
	Save(user User) error
	Delete(user User) error
	UpdateToken(user User, token string) error
	Get(id int64) User
	GetEmail(email string) User
}

UserManager is the persistance interface for users. TODO Save should return a User or it's impossible to return manager- created attributes.

type UserTest

type UserTest func(u User) bool

UserTest is given a user and returns a bool - user checks use this type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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