id

package module
v0.0.0-...-1c0eac8 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2016 License: Apache-2.0 Imports: 7 Imported by: 0

README

id (Golang)

GoDoc

Sessionless, passwords was authentication.

How

JSON web tokens + OAuth identity providers.

All you need to do is this:

package main

import (
    "net/http"

    "github.com/bentranter/id"
    "github.com/bentranter/id/providers/facebook"
)

func main() {
    provider := facebook.New("<your-client-id>", "<your-client-secret>", "<your-client-callback-url>")

    http.Handle("/auth/facebook/authorize", id.Authorize(provider))
    http.Handle("/auth/facebook/callback", id.Callback(provider, "<your-redirect-url>"))

    http.ListenAndServe(":3000", nil)
}

It works with Gorilla's Mux, Pat, Julien Schmidt's HttpRouter, and of course the standard net/http package.

Looking for a more "normal" password flow?

Checkout Password

Documentation

Overview

Package id is an authentication package that is sessionless, and passwordless.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrTokenInvalid means the token wasn't valid based
	// on the value of its signature.
	ErrTokenInvalid = errors.New("Invalid token")
	// ErrCannotParseToken means that the token could not
	//be parsed.
	ErrCannotParseToken = errors.New("Cannot parse token")
	// ErrInvalidSigningMethod means that the method used
	// to sign the token was not the expected method, or
	// is an invalid method.
	ErrInvalidSigningMethod = errors.New("Invalid signing method")
	// ErrNoSigningKey means that a signing key doesn't
	// exist.
	ErrNoSigningKey = errors.New("No signing key")
)

Functions

func AuthInit

func AuthInit(key []byte)

AuthInit overrides the randomly generated signing key. Useful for users who want to use the same key across server restarts, so users don't lose their session.

func Authorize

func Authorize(p Provider) http.Handler

Authorize builds the auth url and redirects a user to it.

func Callback

func Callback(p Provider, redirectURL string) http.Handler

Callback handles the callback part of the flow.

func ExpireCookie

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

ExpireCookie sets the expiry on the cookie. It will not send the request.

func GenToken

func GenToken(user *User) (*http.Cookie, error)

GenToken generates a new JSON web token from a user.

func HTTPRouterAuthorize

func HTTPRouterAuthorize(p Provider) httprouter.Handle

HTTPRouterAuthorize is the same thing as the regular `Authorize`, but for Julien Schmidt's HttpRouter.

This should be moved into it's own package.

func HTTPRouterCallback

func HTTPRouterCallback(p Provider, redirectURL string) httprouter.Handle

HTTPRouterCallback is the thing as `Callback` but for Julien Schmidt's HttpRouter

func Middleware

func Middleware(handlers ...Handler) http.Handler

Middleware executes all our middleware.

func Verified

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

Verified is just a simple check to make sure that a user is authenticated.

func Verify

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

Verify checks to make sure there is a cookie with a valid JWT.

Types

type Handler

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

Handler is the function definition for our middleware.

type Provider

type Provider interface {
	BuildAuthURL(state string) string
	GetCodeURL(r *http.Request) string
	GetToken(code string) (*oauth2.Token, error)
	GetIdentity(*oauth2.Token) (*User, error)
}

Provider implements all the functions we need.

type User

type User struct {
	Email string
	ID    string
	Name  string
}

User contains the bare minimum info we need to identify someone from a provider.

Directories

Path Synopsis
providers

Jump to

Keyboard shortcuts

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