auth

package module
v0.0.0-...-9dafac5 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2018 License: MIT Imports: 14 Imported by: 2

README

Auth

Auth is a package that provides OAuth endpoints for social logins and issues a JWT to be used for subsequent API calls.

Supported:

  • Google
  • Facebook
  • GitHub

Security

Client - front-end web application, ie. the browser that the user controls Server - our API server that requires authentications for its endpoints Provider - the OAuth2 service that provides us with authorization to the user's data

CSRF

In order to prevent CSRF, or at least mitigate it, a few measures have been implemented.

  • Using proper CORS headers we prevent API requests from other websites using browsers (does not prevent native apps from making requests).
  • By checking both the Origin header and the Referrer header, making sure it is the same as the server's host. This prevents users from being redirected into a POST action from another website.

License

Released under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Providers = map[string]ProviderFunc{
	"google":   Google,
	"facebook": Facebook,
	"github":   GitHub,
}

Functions

func FromContext

func FromContext(ctx context.Context) (int64, map[string]*http.Client)

func GenerateSecret

func GenerateSecret(n int) []byte

GenerateSecret returns a byte slice of length n of cryptographically secure random data

Types

type Auth

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

func New

func New(userStore UserStore) *Auth

func (*Auth) AddProvider

func (a *Auth) AddProvider(id, clientID, clientSecret, redirectURL string, scopes []string)

func (*Auth) Auth

func (a *Auth) Auth(w http.ResponseWriter, r *http.Request)

func (*Auth) Clients

func (a *Auth) Clients(userID int64) (map[string]*http.Client, error)

func (*Auth) Middleware

func (a *Auth) Middleware(next http.Handler) http.Handler

Middleware provides authentication middleware for a http.HandlerFunc

func (*Auth) SetCORS

func (a *Auth) SetCORS(cors string)

func (*Auth) Token

func (a *Auth) Token(w http.ResponseWriter, r *http.Request)

func (*Auth) Validate

func (a *Auth) Validate(w http.ResponseWriter, r *http.Request) (int64, error)

type Provider

type Provider struct {
	Name string
	*oauth2.Config
	User UserFunc
}

func Facebook

func Facebook(clientID, clientSecret, redirectURL string, scopes []string) *Provider

func GitHub

func GitHub(clientID, clientSecret, redirectURL string, scopes []string) *Provider

func Google

func Google(clientID, clientSecret, redirectURL string, scopes []string) *Provider

type ProviderFunc

type ProviderFunc func(string, string, string, []string) *Provider

type ProviderItem

type ProviderItem struct {
	ID   string
	Name string
	URL  string
}

ProviderItem is the response for the List request

type ProviderList

type ProviderList []ProviderItem

func (ProviderList) Len

func (s ProviderList) Len() int

func (ProviderList) Less

func (s ProviderList) Less(i, j int) bool

func (ProviderList) Swap

func (s ProviderList) Swap(i, j int)

type User

type User struct {
	Email     string
	FirstName string
	LastName  string
	Locale    string
	Timezone  string
}

type UserClaims

type UserClaims struct {
	User
	jwt.StandardClaims
}

type UserFunc

type UserFunc func(*http.Client) (*User, error)

type UserStore

type UserStore interface {
	Get(string) (int64, bool)
	Set(*User) (int64, bool)
	GetTokens(int64) (map[string]string, error)
	SetToken(int64, string, string) error
}

UserStore handles user logins with the database

Jump to

Keyboard shortcuts

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