goth

package module
v0.0.0-...-b904e47 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2013 License: MIT Imports: 7 Imported by: 0

README

Goth

Build Status

Goth is a web authentication system written in Go. With Goth, you get out-of- the-box user sign in, sign up, and sign out functionality to kick off building your web app.

Disclaimer

Not ready for production yet! Use at your own risk. File bugs, pitch in, etc.

Installation

go get github.com/jroes/goth

Usage

The following example mounts goth's authentication handler at /auth/, and registers a simple handler for the root route that greets the user with the user's e-mail address if logged in, or as a guest if not.

package main

import (
	"fmt"
	"net/http"
)

import (
	"github.com/jroes/goth"
)

var authHandler = goth.DefaultAuthHandler

func main() {
	http.Handle("/auth/", authHandler)
	http.HandleFunc("/", helloUserHandler)

	// Please use ListenAndServeTLS in production.
	http.ListenAndServe(":8080", nil)
}

func helloUserHandler(w http.ResponseWriter, r *http.Request) {
	currentUser, ok := authHandler.CurrentUser(r)
	if ok {
		fmt.Fprintf(w, "Hello, %s!", currentUser.Email)
	} else {
		fmt.Fprintf(w, "Hello, guest!")
	}
}

So what does this get you exactly? You get a bunch of routes underneath /auth/:

HTTP Method URL Description
GET http://localhost:8080/auth/sign_up Standard sign up form
POST http://localhost:8080/auth/sign_up Creates User and persists using the defined UserStore (default: UserGobStore, a store persisted in Go's gob format on disk)
GET http://localhost:8080/auth/sign_in Standard sign in form
POST http://localhost:8080/auth/sign_in Creates a session (default: cookie-based through gorilla/sessions)
POST http://localhost:8080/auth/sign_out Expires the session (default: expires the cookie from gorilla/sessions)

Documentation

Overview

Package goth provides an authentication system for Go web apps.

Index

Constants

This section is empty.

Variables

View Source
var DefaultAuthHandler = AuthHandler{
	RoutePath:        "/auth/",
	TemplatePath:     "tmpl/",
	AfterSignupPath:  "/",
	AfterSigninPath:  "/",
	AfterSignoutPath: "/",
	SessionSecret:    "change-me-please",
	SessionStore:     sessions.NewCookieStore([]byte("change-me-please")),
	UserStore:        gobstore.NewUserGobStore("users/"),
}

Functions

This section is empty.

Types

type AuthHandler

type AuthHandler struct {
	// Where to mount URLs for authentication (e.g. signup, signin)
	RoutePath string
	// Where on disk HTML templates are stored for authentication pages.
	TemplatePath string
	// Where to redirect the user after various authentication operations.
	AfterSignupPath  string
	AfterSigninPath  string
	AfterSignoutPath string
	// This should be set to a string of characters used to encrypt and sign
	// sessions. It should be kept private from any source repositories. You could
	// use os.Getenv() for this and store it in an environment variable.
	SessionSecret string
	SessionStore  *sessions.CookieStore
	UserStore     UserStore
}

func (AuthHandler) CurrentUser

func (handler AuthHandler) CurrentUser(r *http.Request) (*User, bool)

CurrentUser retrieves the User object for the currently logged in user based on the request. The first return value is the User object, the second is true if a user is logged in. If a user is not logged in, the first return value will be an empty User, and the second return value will be false.

func (AuthHandler) ServeHTTP

func (handler AuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface to delegate authentication-related routing to the proper handler.

func (AuthHandler) SignInHandler

func (handler AuthHandler) SignInHandler(w http.ResponseWriter, r *http.Request)

SignInHandler validates email and password parameters in an HTTP request against the UserStore. If the provided parameters are valid, a session will be created for the user and an HTTP redirect will be returned to the AfterSigninPath.

func (AuthHandler) SignOutHandler

func (handler AuthHandler) SignOutHandler(w http.ResponseWriter, r *http.Request)

SignOutHandler instructs the browser to clear the session and redirects the client to the AfterSignoutPath.

func (AuthHandler) SignUpHandler

func (handler AuthHandler) SignUpHandler(w http.ResponseWriter, r *http.Request)

SignUpHandler handles both GET and POST requests. With a GET request, it renders a sign up template form that will POST to the same route. With a POST request, it creates a user via the UserStore with the specified email and password parameters. After successfully creating a User, it will redirect to the AfterSignupPath.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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