attar

package module
v0.0.0-...-76350f2 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2015 License: BSD-2-Clause Imports: 6 Imported by: 2

README

THIS PACKAGE IS DEPRECATED.

Please, use the reincarnation of this package. Gelada.

Attar

Go pakage for http session auth.

Pkg for use with gorilla/mux router.

Get Attar

go get github.com/iu0v1/attar

Usage

package main

import (
    "html/template"
    "net/http"

    "github.com/iu0v1/attar"
    "github.com/gorilla/mux"
)

// main page
var mainPage = template.Must(template.New("").Parse(`
    <html><head></head><body><center>
    <h1 style="padding-top:15%;">HELLO!</h1>
    </form></center></body>
    </html>`))

func mainPageHandler(res http.ResponseWriter, req *http.Request) {
    mainPage.Execute(res, nil)
}

// login page
var loginPage = template.Must(template.New("").Parse(`
    <html><head></head><body>
    <center>
    <form id="login_form" action="/login" method="POST" style="padding-top:15%;">
    <p>user::qwerty</p>
    <input type="text" name="login" placeholder="Login" autofocus><br>
    <input type="password" placeholder="Password" name="password"><br>
    <input type="submit" value="LOGIN">
    </form></center></body>
    </html>`))

func loginPageHandler(res http.ResponseWriter, req *http.Request) {
    loginPage.Execute(res, nil)
}

// auth provider function
func checkAuth(u, p string) bool {
    if u == "user" && p == "qwerty" {
        return true
    }
    return false
}

func main() {

    a := attar.New()

    a.SetAuthProvider(checkAuth)
    a.SetLoginRoute("/login")
    a.SetCookieSessionKeys(
        []byte("261AD9502C583BDQQQQQQQQQQQQQQQQQ"),
        []byte("RRRRRRRRRRRRRRR3FC5C7B3D6E4DDAFF"),
    )

    // set options, with session & cookie lifetime == 30 sec
    options := &attar.AttarOptions{
        Path:                       "/",
        MaxAge:                     30,
        HttpOnly:                   true,
        SessionName:                "test-session",
        SessionLifeTime:            30,
        SessionBindUseragent:       true,
        SessionBindUserHost:        true,
        LoginFormUserFieldName:     "login",
        LoginFormPasswordFieldName: "password",
    }
    a.SetAttarOptions(options)

    // create mux router
    router := mux.NewRouter()
    router.HandleFunc("/", mainPageHandler)
    router.HandleFunc("/login", loginPageHandler).Methods("GET")
    // set attar.AuthHandler as handler func
    // for check login POST data
    router.HandleFunc("/login", a.AuthHandler).Methods("POST")

    // set auth proxy function
    http.Handle("/", a.GlobalAuthProxy(router))

    // start net/httm server at 8080 port
    if err := http.ListenAndServe("127.0.0.1:8082", nil); err != nil {
        panic(err)
    }
}
User AuthProvider

User functon must take 'user' and 'password' arguments, and return true (if user auth successfully) or false (if auth data false).

// user code
func checkAuth(u, p string) bool {
    if u == "user" && p == "qwerty" {
        return true
    }
    return false
}

// and define it
a := attar.New()
a.SetAuthProvider(checkAuth)

Also attar include pre-define simple AuthProvider:

// users list based on map[user]password
userList := map[string]string{
    "user":  "qwerty",
    "admin": "asdfgh",
}

a := attar.New()
a.SetAuthProvider(a.SimpleAuthProvider(userList))

Attar can create new sessions by keys:

import (
    "github.com/SpiritOfStallman/attar"
)

func main() {
    ...
    a := attar.New()
    a.SetCookieSessionKeys(
        []byte("261AD9502C583BDQQQQQQQQQQQQQQQQQ"),
        []byte("RRRRRRRRRRRRRRR3FC5C7B3D6E4DDAFF"),
    )
    ...
}

And can use existing 'gorilla/sessions' CookieStore:

import (
    "github.com/gorilla/sessions"
    "github.com/SpiritOfStallman/attar"
)

func main() {
    ...
    gorillaSessions := sessions.NewCookieStore(
        []byte("261AD9502C583BD7D8AA03083598653B"),
        []byte("E9F6FDFAC2772D33FC5C7B3D6E4DDAFF"),
    )
    ..
    a := attar.New()
    a.SetGorillaCookieStore(gorillaSessions)
    ...
}

DOC

For more information refer to pkg doc.

Documentation

Overview

Attar package provide simple way to get http user auth (via sessions and cookie).

It use part of great Gorilla web toolkit, 'gorilla/sessions' package (http://github.com/gorilla/sessions).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attar

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

func New

func New() *Attar

Return Attar struct with default options.

By default contain pre-set keys to 'gorilla/sessions' NewCookieStore func (provide in *Attar.CookieSessionKeys). It is not secure. Keys must be changed!

For more information about NewCookieStore() refer to http://www.gorillatoolkit.org/pkg/sessions#NewCookieStore.

func (*Attar) AuthHandler

func (a *Attar) AuthHandler(res http.ResponseWriter, req *http.Request)

Auth handler, for grub login form data, and init cookie session.

func (*Attar) GlobalAuthProxy

func (a *Attar) GlobalAuthProxy(next http.Handler) http.HandlerFunc

Function for check auth session.

func (*Attar) SetAttarOptions

func (a *Attar) SetAttarOptions(o *AttarOptions)

Set attar options (*AttarOptions).

func (*Attar) SetAuthProvider

func (a *Attar) SetAuthProvider(f authProvider)

Method for set "auth provider" function, and user verification.

User functon must take 'user' and 'password' arguments, and return true (if user auth successfully) or false (if auth data false).

As alternative use preset attar auth provider functions (like attar.SimpleAuthProvider)

Example of auth provider function:

// user code
func checkAuth(u, p string) bool {
	if u == "user" && p == "qwerty" {
		return true
	}
	return false
}

And define it:

// user code
a := attar.New()
a.SetAuthProvider(checkAuth)

func (*Attar) SetCookieSessionKeys

func (a *Attar) SetCookieSessionKeys(authKey, encryptionKey []byte)

Set 'gorilla/sessions' session cookie keys.

Attention! Conflict with attar.SetGorillaCookieStore.

For more information about NewCookieStore() refer to http://www.gorillatoolkit.org/pkg/sessions#NewCookieStore.

func (*Attar) SetGorillaCookieStore

func (a *Attar) SetGorillaCookieStore(c *sessions.CookieStore)

Set pre-define 'gorilla/sessions' CookieStore as attar CookieStore.

Attention! Conflict with attar.SetCookieSessionKeys.

Example:

import (
	"github.com/gorilla/sessions"
	"github.com/SpiritOfStallman/attar"
)

func main() {
	..
	gorillaSessions := sessions.NewCookieStore(
		[]byte("261AD9502C583BD7D8AA03083598653B"),
		[]byte("E9F6FDFAC2772D33FC5C7B3D6E4DDAFF"),
	)
	..
	a := attar.New()
	a.SetGorillaCookieStore(gorillaSessions)
	..
}

func (*Attar) SetLoginRoute

func (a *Attar) SetLoginRoute(r string)

Get path for login redirect.

func (*Attar) SimpleAuthProvider

func (a *Attar) SimpleAuthProvider(userlist map[string]string) authProvider

User auth provider function, for simple user/password check.

Example of usage:

// users list based on map[user]password
userList := map[string]string{
	"user":  "qwerty",
	"admin": "asdfgh",
}

a := attar.New()
a.SetAuthProvider(a.SimpleAuthProvider(userList))

type AttarOptions

type AttarOptions struct {
	// 'gorilla/sessions' section:
	// description see on http://www.gorillatoolkit.org/pkg/sessions#Options
	// or source on github
	Path     string
	Domain   string
	MaxAge   int
	Secure   bool
	HttpOnly bool

	// attar section:
	// name of cookie browser session
	SessionName     string // default: "attar-session"
	SessionLifeTime int    // default: 86400; in sec

	// bind browser useragent to cookie
	SessionBindUseragent bool

	// bind user IP addr to cookie
	SessionBindUserHost bool

	// html field names, to retrieve
	// user name and password from
	// login form
	LoginFormUserFieldName     string // default: "login"
	LoginFormPasswordFieldName string // default: "password"
}

Primary attar options (except for basic settings also accommodates a 'gorilla/sessions' options (http://www.gorillatoolkit.org/pkg/sessions#Options)).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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