google

package
v0.7.10 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package google provides Google OAuth2 login and callback handlers.

Index

Constants

View Source
const (
	ProviderName = "GOOGLE"
)

Variables

View Source
var (
	// ErrServerError returns a generic server error
	ErrServerError = errors.New("server error")

	// ErrContextMissingGoogleUser  is returned when the Google user is missing from the context
	ErrContextMissingGoogleUser = errors.New("context missing google user")

	// ErrFailedConstructingEndpointURL is returned when URL is invalid and unable to be parsed
	ErrFailedConstructingEndpointURL = errors.New("error constructing URL")

	// ErrUnableToGetGoogleUser when the user cannot be retrieved from Google
	ErrUnableToGetGoogleUser = errors.New("unable to get google user")

	// ErrCannotValidateGoogleUser when the Google user is invalid
	ErrCannotValidateGoogleUser = errors.New("could not validate google user")

	// 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 handles Google redirection URI requests and adds the Google access token and Userinfo to the ctx

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 handles Google login requests by reading the state value from the ctx and redirecting requests to the AuthURL with that state value

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 UserFromContext

func UserFromContext(ctx context.Context) (*google.Userinfo, error)

UserFromContext returns the Google Userinfo from the ctx

func VerifyClientToken

func VerifyClientToken(ctx context.Context, token *oauth2.Token, config *oauth2.Config, email string) (err error)

VerifyClientToken checks the client token and returns an error if it is invalid

func WithError

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

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

func WithUser

func WithUser(ctx context.Context, user *google.Userinfo) context.Context

WithUser returns a copy of ctx that stores the Google Userinfo

Types

type ProviderConfig

type ProviderConfig struct {
	// ClientID is the public identifier for the Google oauth2 client
	ClientID string `json:"clientId" koanf:"clientId" jsonschema:"required"`
	// ClientSecret is the secret for the Google oauth2 client
	ClientSecret string `json:"clientSecret" koanf:"clientSecret" jsonschema:"required"`
	// ClientEndpoint is the endpoint for the Google oauth2 client
	ClientEndpoint string `json:"clientEndpoint" koanf:"clientEndpoint" default:"http://localhost:17608"`
	// Scopes are the scopes that the Google oauth2 client will request
	Scopes []string `json:"scopes" koanf:"scopes" jsonschema:"required"`
	// RedirectURL is the URL that the Google oauth2 client will redirect to after authentication with Google
	RedirectURL string `json:"redirectUrl" koanf:"redirectUrl" jsonschema:"required" default:"/v1/google/callback"`
}

ProviderConfig represents the configuration settings for a Google Oauth Provider

Jump to

Keyboard shortcuts

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