auth

package module
v0.0.0-...-60a0086 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2019 License: MIT Imports: 29 Imported by: 0

README

nginxauth

Build Status Coverage Status

A Go http server or middleware that can be used as an authentication backend with NGINX or directly in a Go web server Capabilities included:

  1. Session management (through Redis by default)
  2. User authentication and hashing (OpenLDAP or SQL database)
  3. Email notification

Documentation

Overview

This file taken with some modification from authboss github.com/go-authboss

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeFromString

func DecodeFromString(token string) ([]byte, error)

func GetInfo

func GetInfo(info map[string]interface{}, name string) interface{}

GetInfo will return the named info as an interface{}

func GetInfoString

func GetInfoString(info map[string]interface{}, name string) string

GetInfoString will return the named info as a string

func GetInfoStrings

func GetInfoStrings(info map[string]interface{}, name string) []string

GetInfoStrings will return the named info as an array of strings

Types

type AuthError

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

AuthError struct holds detailed auth error info

func (*AuthError) Error

func (a *AuthError) Error() string

func (*AuthError) Trace

func (a *AuthError) Trace() string

type AuthStorer

type AuthStorer interface {
	GetSession(w http.ResponseWriter, r *http.Request) (*LoginSession, error)
	GetBasicAuth(w http.ResponseWriter, r *http.Request) (*LoginSession, error)
	OAuthLogin(w http.ResponseWriter, r *http.Request) (string, error)
	Login(w http.ResponseWriter, r *http.Request) (*LoginSession, error)
	Register(w http.ResponseWriter, r *http.Request, email string, templates TemplateNames, emailSubject string, info map[string]interface{}) error
	RequestPasswordReset(w http.ResponseWriter, r *http.Request, email string, templates TemplateNames, emailSubject string, info map[string]interface{}) error
	Logout(w http.ResponseWriter, r *http.Request) error
	CreateProfile(w http.ResponseWriter, r *http.Request) (*LoginSession, error)
	VerifyEmail(w http.ResponseWriter, r *http.Request, emailVerificationCode, templateName, emailSubject string) (string, *User, error)
	VerifyPasswordReset(w http.ResponseWriter, r *http.Request, emailVerificationCode string) (string, *User, error)
	CreateSecondaryEmail(w http.ResponseWriter, r *http.Request, templateName, emailSubject string) error
	SetPrimaryEmail(w http.ResponseWriter, r *http.Request, templateName, emailSubject string) error
	UpdatePassword(w http.ResponseWriter, r *http.Request) (*LoginSession, error)
}

AuthStorer interface provides the necessary functionality to get and store authentication information

func NewAuthStore

func NewAuthStore(b Backender, mailer Mailer, customPrefix, cookieDomain string, cookieKey []byte) AuthStorer

NewAuthStore is used to create an AuthStorer for most authentication needs

type Backender

type Backender interface {
	Clone() Backender
	// contains filtered or unexported methods
}

Backender interface contains all the methods needed to read and write users, sessions and logins

func NewBackend

func NewBackend(u UserBackender, s SessionBackender) Backender

NewBackend returns a Backender from a UserBackender, LoginBackender and SessionBackender

func NewBackendMemory

func NewBackendMemory(c Crypter) Backender

NewBackendMemory creates a memory-backed Backender

func NewBackendMongo

func NewBackendMongo(m mgo.Sessioner, c Crypter) Backender

NewBackendMongo creates a MongoDB-based Backender

type CookieStorer

type CookieStorer interface {
	Get(w http.ResponseWriter, r *http.Request, key string, result interface{}) error
	Put(w http.ResponseWriter, r *http.Request, key string, value interface{}) error
	PutWithExpire(w http.ResponseWriter, r *http.Request, key string, expireMins int, value interface{}) error
	Delete(w http.ResponseWriter, key string)
}

CookieStorer interface provides the necessary methods for handling cookies

type Crypter

type Crypter interface {
	HashEquals(token, tokenHash string) error
	Hash(token string) (string, error)
}

Crypter interface is used to store the password hash and to compare two password hashes together for equality

type CryptoHashStore

type CryptoHashStore struct {
	Crypter
}

func (*CryptoHashStore) Hash

func (c *CryptoHashStore) Hash(token string) (string, error)

func (*CryptoHashStore) HashEquals

func (c *CryptoHashStore) HashEquals(token, tokenHash string) error

type Emailer

type Emailer struct {
	TemplateCache *template.Template
	Sender        sender
}

Emailer struct contains parsed glob of email templates a Sender interface to send emails

func (*Emailer) SendMessage

func (e *Emailer) SendMessage(to, templateName, emailSubject string, data interface{}) error

SendMessage prepares an email with the provided template and passes it to Send for mailing

type LoginSession

type LoginSession struct {
	UserID        string                 `bson:"userID"        json:"userID"`
	Email         string                 `bson:"email"         json:"email"`
	Info          map[string]interface{} `bson:"info"          json:"info"`
	SessionHash   string                 `bson:"_id"           json:"sessionHash"`
	CSRFToken     string                 `bson:"csrfToken"     json:"csrfToken"`
	RenewTimeUTC  time.Time              `bson:"renewTimeUTC"  json:"renewTimeUTC"`
	ExpireTimeUTC time.Time              `bson:"expireTimeUTC" json:"expireTimeUTC"`
}

LoginSession is the struct which holds session information

func (*LoginSession) GetInfo

func (l *LoginSession) GetInfo(name string) interface{}

GetInfo will return the named info as an interface{}

func (*LoginSession) GetInfoString

func (l *LoginSession) GetInfoString(name string) string

GetInfoString will return the named info as a string

func (*LoginSession) GetInfoStrings

func (l *LoginSession) GetInfoStrings(name string) []string

GetInfoStrings will return the named info as an array of strings

type Mailer

type Mailer interface {
	SendMessage(to, templateName, emailSubject string, data interface{}) error
}

Mailer interface includes method needed to send communication to users on account updates

type SendGridSender

type SendGridSender struct {
	APIKey               string
	EmailFromDisplayName string
	EmailFromAddress     string
}

func (*SendGridSender) Send

func (s *SendGridSender) Send(to, subject, body string) error

Send mails the provided email body to recipient at "to" with subject "subject"

type SessionBackender

type SessionBackender interface {
	// contains filtered or unexported methods
}

SessionBackender interface holds methods for session management

func NewBackendRedisSession

func NewBackendRedisSession(server string, port int, password string, maxIdle, maxConnections int, keyPrefix string) SessionBackender

NewBackendRedisSession returns a SessionBackender for Redis

type SmtpSender

type SmtpSender struct {
	SMTPServer           string
	SMTPPort             int
	SMTPFromEmail        string
	SMTPPassword         string
	EmailFromDisplayName string
}

func (*SmtpSender) Send

func (s *SmtpSender) Send(to, subject, body string) error

Send mails the provided email body to recipient at "to" with subject "subject"

type TemplateNames

type TemplateNames struct {
	Success string
	Failure string
}

TemplateNames contains the names of the html email templates to be used on Success and/or Failure conditions

type User

type User struct {
	UserID string                 `json:"userID"`
	Email  string                 `json:"email"`
	Info   map[string]interface{} `json:"info"`
}

User is the struct which holds user information

func (*User) GetInfo

func (u *User) GetInfo(name string) interface{}

GetInfo will return the named info as an interface{}

func (*User) GetInfoString

func (u *User) GetInfoString(name string) string

GetInfoString will return the named info as a string

func (*User) GetInfoStrings

func (u *User) GetInfoStrings(name string) []string

GetInfoStrings will return the named info as an array of strings

type UserBackender

type UserBackender interface {
	// contains filtered or unexported methods
}

UserBackender interface holds methods for user management

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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