gothic

package
v1.72.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package gothic wraps common behaviour when using goth. This makes it quick, and easy, to get up and running with goth. Of course, if you want complete control over how things flow, in regards to the authentication process, feel free and use Goth directly.

See https://github.com/bgdsh/goth/blob/master/examples/main.go to see this in action.

Index

Constants

View Source
const ProviderParamKey key = iota

ProviderParamKey can be used as a key in context when passing in a provider

View Source
const SessionName = "_gothic_session"

SessionName is the key used to access the session store.

Variables

View Source
var CompleteUserAuth = func(c echo.Context) (goth.User, error) {

	providerName, err := GetProviderName(c)
	if err != nil {
		return goth.User{}, err
	}

	provider, err := goth.GetProvider(providerName)
	if err != nil {
		return goth.User{}, err
	}

	value, err := GetFromSession(providerName, c)
	if err != nil {
		return goth.User{}, err
	}
	defer Logout(c)
	sess, err := provider.UnmarshalSession(value)
	if err != nil {
		return goth.User{}, err
	}

	err = validateState(c, sess)
	if err != nil {
		return goth.User{}, err
	}

	user, err := provider.FetchUser(sess)
	if err == nil {

		return user, err
	}

	params := c.QueryParams()
	if params.Encode() == "" && c.Request().Method == "POST" {
		params, err = c.FormParams()
		if err != nil {
			return goth.User{}, err
		}
	}

	_, err = sess.Authorize(provider, params)
	if err != nil {
		return goth.User{}, err
	}

	err = StoreInSession(providerName, sess.Marshal(), c)

	if err != nil {
		return goth.User{}, err
	}

	gu, err := provider.FetchUser(sess)
	return gu, err
}

CompleteUserAuth does what it says on the tin. It completes the authentication process and fetches all of the basic information about the user from the provider.

It expects to be able to get the name of the provider from the query parameters as either "provider" or ":provider".

See https://github.com/bgdsh/goth/examples/main.go to see this in action.

View Source
var GetProviderName = getProviderName

GetProviderName is a function used to get the name of a provider for a given request. By default, this provider is fetched from the URL query string. If you provide it in a different way, assign your own function to this variable that returns the provider name for your request.

View Source
var GetState = func(c echo.Context) string {
	if c.QueryParams().Encode() == "" && c.Request().Method == http.MethodPost {
		return c.FormValue("state")
	}
	return c.QueryParam("state")
}

GetState gets the state returned by the provider during the callback. This is used to prevent CSRF attacks, see http://tools.ietf.org/html/rfc6749#section-10.12

View Source
var SetState = func(c echo.Context) string {
	state := c.QueryParam("state")
	if len(state) > 0 {
		return state
	}

	nonceBytes := make([]byte, 64)
	_, err := io.ReadFull(rand.Reader, nonceBytes)
	if err != nil {
		panic("gothic: source of randomness unavailable: " + err.Error())
	}
	return base64.URLEncoding.EncodeToString(nonceBytes)
}

SetState sets the state string associated with the given request. If no state string is associated with the request, one will be generated. This state is sent to the provider and can be retrieved during the callback.

Functions

func BeginAuthHandler

func BeginAuthHandler(c echo.Context) error

BeginAuthHandler is a convenience handler for starting the authentication process. It expects to be able to get the name of the provider from the query parameters as either "provider" or ":provider".

BeginAuthHandler will redirect the user to the appropriate authentication end-point for the requested provider.

See https://github.com/bgdsh/goth/examples/main.go to see this in action.

func GetAuthURL

func GetAuthURL(c echo.Context) (string, error)

GetAuthURL starts the authentication process with the requested provided. It will return a URL that should be used to send users to.

It expects to be able to get the name of the provider from the query parameters as either "provider" or ":provider".

I would recommend using the BeginAuthHandler instead of doing all of these steps yourself, but that's entirely up to you.

func GetContextWithProvider added in v1.72.0

func GetContextWithProvider(req *http.Request, provider string) *http.Request

GetContextWithProvider returns a new request context containing the provider

func GetFromSession added in v1.45.4

func GetFromSession(key string, c echo.Context) (string, error)

GetFromSession retrieves a previously-stored value from the session. If no value has previously been stored at the specified key, it will return an error.

func Logout

func Logout(c echo.Context) error

Logout invalidates a user session.

func StoreInSession added in v1.45.4

func StoreInSession(key string, value string, c echo.Context) error

StoreInSession stores a specified key/value pair in the session.

Types

This section is empty.

Jump to

Keyboard shortcuts

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