oauth2

package
v0.0.0-...-a46f264 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package oauth2 provides an handler for handling OAuth2 authentication flows and the implementation of several OAuth2 providers.

Index

Constants

View Source
const (
	// DefaultCookieName is the name of the stored cookie
	DefaultCookieName = "browser_lter_session"

	// DefaultLifespan is the duration a token and cookie is valid
	DefaultLifespan = 48 * time.Hour

	// DefaultJWTIssuer is the default issuer of the JWT token
	DefaultJWTIssuer = "BrowserLTER"
)

Variables

View Source
var (

	// ErrTokenInvalid denotes that a could not be validated.
	ErrTokenInvalid = errors.New("token is invalid")
)

Functions

This section is empty.

Types

type Authenticator

type Authenticator interface {
	// Validate returns an authenticated User if a valid user session is found.
	Validate(context.Context, *http.Request) (*browser.User, error)

	// Authorize will create a new user session for authenticated users.
	Authorize(context.Context, http.ResponseWriter, *browser.User) error

	// Expire will logout the authenticated User.
	Expire(http.ResponseWriter)
}

Authenticator represents a service for authenticating users.

type Cookie struct {
	// Secret used for JWT generation/validation.
	Secret string
	// Cookie used for storing JWT token in a secure manner.
	Cookie *securecookie.SecureCookie
}

Cookie is an Authenticator using HTTP cookies and JWT tokens.

func (*Cookie) Authorize

func (c *Cookie) Authorize(ctx context.Context, w http.ResponseWriter, u *browser.User) error

func (*Cookie) Expire

func (c *Cookie) Expire(w http.ResponseWriter)

func (*Cookie) Validate

func (c *Cookie) Validate(ctx context.Context, r *http.Request) (*browser.User, error)

Validate validates the JWT token stored in the cookie and return the user information. It will not validate the user against the user service.

type Github

type Github struct {
	ClientID string
	Secret   string
}

Github is an OAuth2 provider for signing in using Github accounts.

func (*Github) Config

func (g *Github) Config() *oauth2.Config

Config is the Github OAuth2 configuration.

func (*Github) Name

func (g *Github) Name() string

Name returns the name of the provider.

func (*Github) User

func (g *Github) User(ctx context.Context, token *oauth2.Token) (*browser.User, error)

User returns an browser.User with information from Github.

type Google

type Google struct {
	ClientID    string
	Secret      string
	RedirectURL string
	Nonce       string
}

Google is an OAuth2 provider for signing in using Google accounts.

func (*Google) Config

func (g *Google) Config() *oauth2.Config

Config is the Google OAuth2 configuration.

func (*Google) Name

func (g *Google) Name() string

Name returns the name of the provider.

func (*Google) User

func (g *Google) User(ctx context.Context, token *oauth2.Token) (*browser.User, error)

User returns an browser.User with information from Google.

type Handler

type Handler struct {
	Next  http.Handler
	State string
	Nonce string
	Auth  Authenticator
	Users browser.UserService
	// contains filtered or unexported fields
}

Handler handles OAuth2 authorization flows and different account aspects.

func (*Handler) Register

func (h *Handler) Register(p Provider)

Register registers all the routes for the given provider.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Microsoft

type Microsoft struct {
	Provider    string
	ClientID    string
	Secret      string
	RedirectURL string
	Nonce       string
}

Microsoft is an OAuth2 provider for signing in using azure AD.

func (*Microsoft) Config

func (m *Microsoft) Config() *oauth2.Config

Config is the Microsoft OAuth2 configuration.

func (*Microsoft) Name

func (m *Microsoft) Name() string

Name returns the name of provider.

func (*Microsoft) User

func (m *Microsoft) User(ctx context.Context, token *oauth2.Token) (*browser.User, error)

User returns an browser.User with information from Azure AD.

type Provider

type Provider interface {
	// Name returns the name of the provider.
	Name() string
	// Config returns the OAuth2 config of the provider.
	Config() *oauth2.Config
	// User returns user information from the provider.
	User(context.Context, *oauth2.Token) (*browser.User, error)
}

Provider are the common parameters all OAuth2 providers should implement.

Jump to

Keyboard shortcuts

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