oauth2

package module
v0.0.0-...-51b0090 Latest Latest
Warning

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

Go to latest
Published: May 12, 2015 License: Apache-2.0 Imports: 8 Imported by: 0

README

oauth2 wercker status

Allows your Martini application to support user login via an OAuth 2.0 backend. Requires sessions middleware. Google, Facebook, LinkedIn and Github sign-in are currently supported. Once endpoints are provided, this middleware can work with any OAuth 2.0 backend.

Usage

See example_test.go for a sample.

If a route requires login, you can add oauth2.LoginRequired to the handler chain. If user is not logged, they will be automatically redirected to the login path.

m.Get("/login-required", oauth2.LoginRequired, func() ...)

Auth flow

  • /login will redirect user to the OAuth 2.0 provider's permissions dialog. If there is a next query param provided, user is redirected to the next page afterwards.
  • If user agrees to connect, OAuth 2.0 provider will redirect to /oauth2callback to let your app to make the handshake. You need to register /oauth2callback as a Redirect URL in your application settings.
  • /logout will log the user out. If there is a next query param provided, user is redirected to the next page afterwards.

You can customize the login, logout, oauth2callback and error paths:

oauth2.PathLogin = "/oauth2login"
oauth2.PathLogout = "/oauth2logout"
...

Authors

Documentation

Overview

Package oauth2 contains Martini handlers to provide user login via an OAuth 2.0 backend.

Index

Constants

This section is empty.

Variables

View Source
var (
	// PathLogin is the path to handle OAuth 2.0 logins.
	PathLogin = "/login"
	// PathLogout is the path to handle OAuth 2.0 logouts.
	PathLogout = "/logout"
	// PathCallback is the path to handle callback from OAuth 2.0 backend
	// to exchange credentials.
	PathCallback = "/oauth2callback"
	// PathError is the path to handle error cases.
	PathError = "/oauth2error"
)
View Source
var LoginRequired = func() martini.Handler {
	return func(s sessions.Session, c martini.Context, w http.ResponseWriter, r *http.Request) {
		token := unmarshallToken(s)
		if token == nil || token.Expired() {
			next := url.QueryEscape(r.URL.RequestURI())
			http.Redirect(w, r, PathLogin+"?next="+next, http.StatusFound)
		}
	}
}()

Handler that redirects user to the login page if user is not logged in. Sample usage: m.Get("/login-required", oauth2.LoginRequired, func() ... {})

Functions

func Facebook

func Facebook(conf *oauth2.Config) martini.Handler

Facebook returns a new Facebook OAuth 2.0 backend endpoint.

func Github

func Github(conf *oauth2.Config) martini.Handler

Github returns a new Github OAuth 2.0 backend endpoint.

func Google

func Google(conf *oauth2.Config) martini.Handler

Google returns a new Google OAuth 2.0 backend endpoint.

func LinkedIn

func LinkedIn(conf *oauth2.Config) martini.Handler

LinkedIn returns a new LinkedIn OAuth 2.0 backend endpoint.

func NewOAuth2Provider

func NewOAuth2Provider(conf *oauth2.Config) martini.Handler

NewOAuth2Provider returns a generic OAuth 2.0 backend endpoint.

Types

type Tokens

type Tokens interface {
	Access() string
	Refresh() string
	Expired() bool
	ExpiryTime() time.Time
	Extra(key string) interface{}
	String() string
}

Tokens represents a container that contains user's OAuth 2.0 access and refresh tokens.

Jump to

Keyboard shortcuts

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