oauth2

package
v0.6.0 Latest Latest
Warning

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

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

Documentation

Overview

Package oauth2 provides handlers for OAuth2 login and callback requests.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrContextMissingToken is returned when the context is missing the token value
	ErrContextMissingToken = errors.New("oauth2: context missing token")

	// ErrContextMissingStateValue is returned when the context is missing the state value
	ErrContextMissingStateValue = errors.New("oauth2: context missing state value")

	// ErrInvalidState is returned when the state parameter is invalid
	ErrInvalidState = errors.New("oauth2: invalid oauth2 state parameter")

	// ErrFailedToGenerateToken is returned when a token cannot be generated
	ErrFailedToGenerateToken = errors.New("failed to generate token")

	// ErrMissingCodeOrState is returned when the request is missing the code or state query string parameter
	ErrMissingCodeOrState = errors.New("oauth2: request missing code or state")

	// ErrContextMissingErrorValue is returned when the context does not have an error value
	ErrContextMissingErrorValue = fmt.Errorf("context missing error value")
)
View Source
var DefaultFailureHandler = http.HandlerFunc(failureHandler)

DefaultFailureHandler responds with a 400 status code and message parsed from the context

Functions

func CallbackHandler

func CallbackHandler(config *oauth2.Config, success, failure http.Handler) http.Handler

CallbackHandler parses the auth code + state and compares it to the state value from the context

func ErrorFromContext

func ErrorFromContext(ctx context.Context) error

ErrorFromContext returns the error value from the ctx or an error that the context was missing an error value

func LoginHandler

func LoginHandler(config *oauth2.Config, failure http.Handler) http.Handler

LoginHandler reads the state value from the context and redirects requests to the AuthURL with that state value

func RedirectFromContext

func RedirectFromContext(ctx context.Context) string

RedirectFromContext returns the redirect value from the ctx

func StateFromContext

func StateFromContext(ctx context.Context) (string, error)

StateFromContext returns the state value from the ctx

func StateHandler

func StateHandler(config sessions.CookieConfig, success http.Handler) http.Handler

StateHandler checks for a state cookie, if found, adds to context; if missing, a random generated value is added to the context and to a (short-lived) state cookie issued to the requester - this implements OAuth 2 RFC 6749 10.12 CSRF Protection

func TokenFromContext

func TokenFromContext(ctx context.Context) (*oauth2.Token, error)

TokenFromContext returns the Token from the ctx

func WithError

func WithError(ctx context.Context, err error) context.Context

WithError returns a copy of context that stores the given error value

func WithRedirectURL

func WithRedirectURL(ctx context.Context, redirect string) context.Context

WithRedirectURL returns a copy of ctx that stores the redirect value

func WithState

func WithState(ctx context.Context, state string) context.Context

WithState returns a copy of ctx that stores the state value

func WithToken

func WithToken(ctx context.Context, token *oauth2.Token) context.Context

WithToken returns a copy of ctx that stores the Token

Types

This section is empty.

Jump to

Keyboard shortcuts

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