googleauth

package module
v0.0.0-...-01814cc Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2015 License: MIT Imports: 12 Imported by: 0

README

googleauth

Package googleauth provides OAuth sign-in using Google+.

See docs at http://hkjn.me/googleauth.

Documentation

Overview

Package googleauth provides OAuth sign-in using Google+.

Minimum setup needed:

  1. Specify client G+ credentials with SetCredentials()
  2. Specify a gating function with SetGatingFunc()
  3. Register a HTTP GET route at /connect for ConnectHandler
  4. Wrap any HTTP routes that should be authenticated with RequireLogin()

This will send users accessing the resources under authentication to a simple page with a G+ button, and if SetGatingFunc accepts that G+ id, the user is redirected to the original URL.

The login page can be changed by setting a different LoginTmpl.

If more control is desired, IsLoggedIn, LogIn and Connect can be used directly, but with the steps above it's not necessary.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Name of the top-level login template.
	TemplateName = "login"
	// Template to use for the login redirect.
	LoginTmpl = template.Must(template.New(TemplateName).Parse(tmpl))
)

Functions

func Connect

func Connect(w http.ResponseWriter, r *http.Request) error

Connect finishes the connection process, exchanging the one-time authorization code for an access token and storing it in the session.

func ConnectHandler

func ConnectHandler(w http.ResponseWriter, r *http.Request)

ConnectHandler finishes the connection process, exchanging the one-time authorization code for an access token and storing it in the session.

ConnectHandler redirects to the request "referer" on successful login.

ConnectHandler returns HTTP 401 (Unauthorized) if the user does not have access.

func IsLoggedIn

func IsLoggedIn(r *http.Request) (bool, error)

IsLoggedIn returns true if the user is signed in.

func RequireLogin

func RequireLogin(fn http.HandlerFunc) http.HandlerFunc

RequireLogin returns a wrapped HandlerFunc that enforces Google+ login.

If the user is logged in, the specified HandlerFunc is called, otherwise the login page defined by LoginTmpl is served.

RequireLogin returns HTTP 500 (Internal Server Error) if the template fails to render or the package has an internal error.

func SetCredentials

func SetCredentials(clientId, clientSecret string)

SetCredential specifies the client G+ credentials.

func SetGatingFunc

func SetGatingFunc(fn func(gplusId string) bool)

SetGatingFunc sets a function to check if user with given G+ id is allowed access.

Types

type LoginInfo

type LoginInfo struct {
	ClientId   string // id of client
	StateToken string // state token
}

LoginInfo represents the user's login info.

func LogIn

func LogIn(w http.ResponseWriter, r *http.Request) (*LoginInfo, error)

LogIn returns the user's login info, starting the auth process.

LogIn generates a state token, which along with the client id should be returned to the user where the front-end library can exchange them for a one-time authorization code. That one-time authorization code is then passed in to /connect, which finishes the auth process.

Jump to

Keyboard shortcuts

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